diff --git a/json11.cpp b/json11.cpp index e0ae291..6c33dc3 100644 --- a/json11.cpp +++ b/json11.cpp @@ -169,6 +169,7 @@ protected: }; class JsonDouble final : public Value { + string as_string() const { return std::to_string(m_value); } bool bool_value() const override { return m_value != 0; } double number_value() const override { return static_cast(m_value); } int64_ int64_value() const override { return static_cast(m_value); } @@ -180,6 +181,7 @@ public: }; class JsonInt64 final : public Value { + string as_string() const { return std::to_string(m_value); } bool bool_value() const override { return m_value != 0; } double number_value() const override { return static_cast(m_value); } int64_ int64_value() const override { return static_cast(m_value); } @@ -191,6 +193,7 @@ public: }; class JsonUInt64 final : public Value { + string as_string() const { return std::to_string(m_value); } bool bool_value() const override { return m_value != 0; } double number_value() const override { return static_cast(m_value); } int64_ int64_value() const override { return static_cast(m_value); } @@ -208,6 +211,7 @@ public: }; class JsonString final : public Value { + string as_string() const { return m_value; } const string &string_value() const override { return m_value; } bool bool_value() const override { return m_value != "" && m_value != "0"; } double number_value() const override { size_t pos = 0; double v = std::stod(m_value, &pos); if (pos < m_value.length()) { v = 0; } return v; } @@ -291,6 +295,7 @@ double Json::number_value() const { return m_ptr->number_v int64_ Json::int64_value() const { return m_ptr->int64_value(); } uint64_ Json::uint64_value() const { return m_ptr->uint64_value(); } bool Json::bool_value() const { return m_ptr->bool_value(); } +string Json::as_string() const { return m_ptr->as_string(); } const string & Json::string_value() const { return m_ptr->string_value(); } const vector & Json::array_items() const { return m_ptr->array_items(); } const map & Json::object_items() const { return m_ptr->object_items(); } @@ -301,6 +306,7 @@ double JsonValue::number_value() const { return int64_ JsonValue::int64_value() const { return 0; } uint64_ JsonValue::uint64_value() const { return 0; } bool JsonValue::bool_value() const { return false; } +string JsonValue::as_string() const { return statics().empty_string; } const string & JsonValue::string_value() const { return statics().empty_string; } const vector & JsonValue::array_items() const { return statics().empty_vector; } const map & JsonValue::object_items() const { return statics().empty_map; } diff --git a/json11.hpp b/json11.hpp index 37d75e4..c852922 100644 --- a/json11.hpp +++ b/json11.hpp @@ -142,13 +142,17 @@ public: bool is_array() const { return type() == ARRAY; } bool is_object() const { return type() == OBJECT; } - // Return the enclosed value if this is a number, 0 otherwise. Note that json11 does not + // Return the enclosed value if this is a number, parse a string if it's a string, + // return 0 if neither a number nor a parseable string. Note that json11 does not // distinguish between integer and non-integer numbers - number_value() and int64_value() // can both be applied to a NUMBER-typed object. double number_value() const; int64_ int64_value() const; uint64_ uint64_value() const; + // Return the enclosed string if this is a string, "" otherwise. + std::string as_string() const; + // Return the enclosed value if this is a boolean, false otherwise. bool bool_value() const; // Return the enclosed string if this is a string, "" otherwise. @@ -235,6 +239,7 @@ protected: virtual uint64_ uint64_value() const; virtual bool bool_value() const; virtual const std::string &string_value() const; + virtual std::string as_string() const; virtual const Json::array &array_items() const; virtual const Json &operator[](size_t i) const; virtual const Json::object &object_items() const;