Skip to content

Commit ed495ed

Browse files
committed
prefer ValueIterator::name() to ::memberName()
in case of embedded nulls
1 parent 3c0a383 commit ed495ed

File tree

5 files changed

+57
-6
lines changed

5 files changed

+57
-6
lines changed

Diff for: include/json/reader.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class JSON_API Reader {
110110
* during parsing.
111111
* \deprecated Use getFormattedErrorMessages() instead (typo fix).
112112
*/
113-
JSONCPP_DEPRECATED("Use getFormattedErrorMessages instead")
113+
JSONCPP_DEPRECATED("Use getFormattedErrorMessages() instead.")
114114
std::string getFormatedErrorMessages() const;
115115

116116
/** \brief Returns a user friendly string that list errors in the parsed
@@ -279,8 +279,6 @@ class JSON_API CharReader {
279279

280280
/** \brief Build a CharReader implementation.
281281
282-
\deprecated This is experimental and will be altered before the next release.
283-
284282
Usage:
285283
\code
286284
using namespace Json;

Diff for: include/json/value.h

+8-2
Original file line numberDiff line numberDiff line change
@@ -667,16 +667,22 @@ class JSON_API ValueIteratorBase {
667667
/// Value.
668668
Value key() const;
669669

670-
/// Return the index of the referenced Value. -1 if it is not an arrayValue.
670+
/// Return the index of the referenced Value, or -1 if it is not an arrayValue.
671671
UInt index() const;
672672

673+
/// Return the member name of the referenced Value, or "" if it is not an
674+
/// objectValue.
675+
/// \note Avoid `c_str()` on result, as embedded zeroes are possible.
676+
std::string name() const;
677+
673678
/// Return the member name of the referenced Value. "" if it is not an
674679
/// objectValue.
675680
/// \deprecated This cannot be used for UTF-8 strings, since there can be embedded nulls.
681+
JSONCPP_DEPRECATED("Use `key = name();` instead.")
676682
char const* memberName() const;
677683
/// Return the member name of the referenced Value, or NULL if it is not an
678684
/// objectValue.
679-
/// Better version than memberName(). Allows embedded nulls.
685+
/// \note Better version than memberName(). Allows embedded nulls.
680686
char const* memberName(char const** end) const;
681687

682688
protected:

Diff for: include/json/writer.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ class JSON_API StreamWriterBuilder : public StreamWriter::Factory {
132132
};
133133

134134
/** \brief Abstract class for writers.
135-
* \deprecated Use StreamWriter.
135+
* \deprecated Use StreamWriter. (And really, this is an implementation detail.)
136136
*/
137137
class JSON_API Writer {
138138
public:
@@ -151,6 +151,7 @@ class JSON_API Writer {
151151
* \deprecated Use StreamWriterBuilder.
152152
*/
153153
class JSON_API FastWriter : public Writer {
154+
154155
public:
155156
FastWriter();
156157
virtual ~FastWriter() {}

Diff for: src/lib_json/json_valueiterator.inl

+8
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ UInt ValueIteratorBase::index() const {
9292
return Value::UInt(-1);
9393
}
9494

95+
std::string ValueIteratorBase::name() const {
96+
char const* key;
97+
char const* end;
98+
key = memberName(&end);
99+
if (!key) return std::string();
100+
return std::string(key, end);
101+
}
102+
95103
char const* ValueIteratorBase::memberName() const {
96104
const char* name = (*current_).first.data();
97105
return name ? name : "";

Diff for: src/test_lib_json/main.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -2282,6 +2282,42 @@ JSONTEST_FIXTURE(IteratorTest, distance) {
22822282
JSONTEST_ASSERT_STRING_EQUAL("b", str);
22832283
}
22842284

2285+
JSONTEST_FIXTURE(IteratorTest, names) {
2286+
Json::Value json;
2287+
json["k1"] = "a";
2288+
json["k2"] = "b";
2289+
Json::ValueIterator it = json.begin();
2290+
JSONTEST_ASSERT(it != json.end());
2291+
JSONTEST_ASSERT_EQUAL(Json::Value("k1"), it.key());
2292+
JSONTEST_ASSERT_STRING_EQUAL("k1", it.name());
2293+
JSONTEST_ASSERT_EQUAL(-1, it.index());
2294+
++it;
2295+
JSONTEST_ASSERT(it != json.end());
2296+
JSONTEST_ASSERT_EQUAL(Json::Value("k2"), it.key());
2297+
JSONTEST_ASSERT_STRING_EQUAL("k2", it.name());
2298+
JSONTEST_ASSERT_EQUAL(-1, it.index());
2299+
++it;
2300+
JSONTEST_ASSERT(it == json.end());
2301+
}
2302+
2303+
JSONTEST_FIXTURE(IteratorTest, indexes) {
2304+
Json::Value json;
2305+
json[0] = "a";
2306+
json[1] = "b";
2307+
Json::ValueIterator it = json.begin();
2308+
JSONTEST_ASSERT(it != json.end());
2309+
JSONTEST_ASSERT_EQUAL(Json::Value(Json::ArrayIndex(0)), it.key());
2310+
JSONTEST_ASSERT_STRING_EQUAL("", it.name());
2311+
JSONTEST_ASSERT_EQUAL(0, it.index());
2312+
++it;
2313+
JSONTEST_ASSERT(it != json.end());
2314+
JSONTEST_ASSERT_EQUAL(Json::Value(Json::ArrayIndex(1)), it.key());
2315+
JSONTEST_ASSERT_STRING_EQUAL("", it.name());
2316+
JSONTEST_ASSERT_EQUAL(1, it.index());
2317+
++it;
2318+
JSONTEST_ASSERT(it == json.end());
2319+
}
2320+
22852321
int main(int argc, const char* argv[]) {
22862322
JsonTest::Runner runner;
22872323
JSONTEST_REGISTER_FIXTURE(runner, ValueTest, checkNormalizeFloatingPointStr);
@@ -2346,6 +2382,8 @@ int main(int argc, const char* argv[]) {
23462382
JSONTEST_REGISTER_FIXTURE(runner, BuilderTest, settings);
23472383

23482384
JSONTEST_REGISTER_FIXTURE(runner, IteratorTest, distance);
2385+
JSONTEST_REGISTER_FIXTURE(runner, IteratorTest, names);
2386+
JSONTEST_REGISTER_FIXTURE(runner, IteratorTest, indexes);
23492387

23502388
return runner.runCommandLine(argc, argv);
23512389
}

0 commit comments

Comments
 (0)