diff --git a/include/json/reader.h b/include/json/reader.h index 251d7c6..728b35e 100644 --- a/include/json/reader.h +++ b/include/json/reader.h @@ -110,7 +110,7 @@ public: * during parsing. * \deprecated Use getFormattedErrorMessages() instead (typo fix). */ - JSONCPP_DEPRECATED("Use getFormattedErrorMessages instead") + JSONCPP_DEPRECATED("Use getFormattedErrorMessages() instead.") std::string getFormatedErrorMessages() const; /** \brief Returns a user friendly string that list errors in the parsed @@ -279,8 +279,6 @@ public: /** \brief Build a CharReader implementation. - \deprecated This is experimental and will be altered before the next release. - Usage: \code using namespace Json; diff --git a/include/json/value.h b/include/json/value.h index 836b663..d91ccc4 100644 --- a/include/json/value.h +++ b/include/json/value.h @@ -667,16 +667,22 @@ public: /// Value. Value key() const; - /// Return the index of the referenced Value. -1 if it is not an arrayValue. + /// Return the index of the referenced Value, or -1 if it is not an arrayValue. UInt index() const; + /// Return the member name of the referenced Value, or "" if it is not an + /// objectValue. + /// \note Avoid `c_str()` on result, as embedded zeroes are possible. + std::string name() const; + /// Return the member name of the referenced Value. "" if it is not an /// objectValue. /// \deprecated This cannot be used for UTF-8 strings, since there can be embedded nulls. + JSONCPP_DEPRECATED("Use `key = name();` instead.") char const* memberName() const; /// Return the member name of the referenced Value, or NULL if it is not an /// objectValue. - /// Better version than memberName(). Allows embedded nulls. + /// \note Better version than memberName(). Allows embedded nulls. char const* memberName(char const** end) const; protected: diff --git a/include/json/writer.h b/include/json/writer.h index f6fcc9c..f5f0a38 100644 --- a/include/json/writer.h +++ b/include/json/writer.h @@ -132,7 +132,7 @@ public: }; /** \brief Abstract class for writers. - * \deprecated Use StreamWriter. + * \deprecated Use StreamWriter. (And really, this is an implementation detail.) */ class JSON_API Writer { public: @@ -151,6 +151,7 @@ public: * \deprecated Use StreamWriterBuilder. */ class JSON_API FastWriter : public Writer { + public: FastWriter(); virtual ~FastWriter() {} diff --git a/src/lib_json/json_valueiterator.inl b/src/lib_json/json_valueiterator.inl index 1e9a247..d01d3c0 100644 --- a/src/lib_json/json_valueiterator.inl +++ b/src/lib_json/json_valueiterator.inl @@ -92,6 +92,14 @@ UInt ValueIteratorBase::index() const { return Value::UInt(-1); } +std::string ValueIteratorBase::name() const { + char const* key; + char const* end; + key = memberName(&end); + if (!key) return std::string(); + return std::string(key, end); +} + char const* ValueIteratorBase::memberName() const { const char* name = (*current_).first.data(); return name ? name : ""; diff --git a/src/test_lib_json/main.cpp b/src/test_lib_json/main.cpp index 2b6584e..71e9491 100644 --- a/src/test_lib_json/main.cpp +++ b/src/test_lib_json/main.cpp @@ -2282,6 +2282,42 @@ JSONTEST_FIXTURE(IteratorTest, distance) { JSONTEST_ASSERT_STRING_EQUAL("b", str); } +JSONTEST_FIXTURE(IteratorTest, names) { + Json::Value json; + json["k1"] = "a"; + json["k2"] = "b"; + Json::ValueIterator it = json.begin(); + JSONTEST_ASSERT(it != json.end()); + JSONTEST_ASSERT_EQUAL(Json::Value("k1"), it.key()); + JSONTEST_ASSERT_STRING_EQUAL("k1", it.name()); + JSONTEST_ASSERT_EQUAL(-1, it.index()); + ++it; + JSONTEST_ASSERT(it != json.end()); + JSONTEST_ASSERT_EQUAL(Json::Value("k2"), it.key()); + JSONTEST_ASSERT_STRING_EQUAL("k2", it.name()); + JSONTEST_ASSERT_EQUAL(-1, it.index()); + ++it; + JSONTEST_ASSERT(it == json.end()); +} + +JSONTEST_FIXTURE(IteratorTest, indexes) { + Json::Value json; + json[0] = "a"; + json[1] = "b"; + Json::ValueIterator it = json.begin(); + JSONTEST_ASSERT(it != json.end()); + JSONTEST_ASSERT_EQUAL(Json::Value(Json::ArrayIndex(0)), it.key()); + JSONTEST_ASSERT_STRING_EQUAL("", it.name()); + JSONTEST_ASSERT_EQUAL(0, it.index()); + ++it; + JSONTEST_ASSERT(it != json.end()); + JSONTEST_ASSERT_EQUAL(Json::Value(Json::ArrayIndex(1)), it.key()); + JSONTEST_ASSERT_STRING_EQUAL("", it.name()); + JSONTEST_ASSERT_EQUAL(1, it.index()); + ++it; + JSONTEST_ASSERT(it == json.end()); +} + int main(int argc, const char* argv[]) { JsonTest::Runner runner; JSONTEST_REGISTER_FIXTURE(runner, ValueTest, checkNormalizeFloatingPointStr); @@ -2346,6 +2382,8 @@ int main(int argc, const char* argv[]) { JSONTEST_REGISTER_FIXTURE(runner, BuilderTest, settings); JSONTEST_REGISTER_FIXTURE(runner, IteratorTest, distance); + JSONTEST_REGISTER_FIXTURE(runner, IteratorTest, names); + JSONTEST_REGISTER_FIXTURE(runner, IteratorTest, indexes); return runner.runCommandLine(argc, argv); }