Skip to content

Commit cd11212

Browse files
authored
Merge pull request open-source-parsers#901 from res2k/demand
Implement Value::demand()
2 parents 3e2f8d3 + 69402d1 commit cd11212

File tree

5 files changed

+32
-6
lines changed

5 files changed

+32
-6
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ if(NOT DEFINED CMAKE_BUILD_TYPE)
6363
endif()
6464

6565
project(JSONCPP
66-
VERSION 1.8.4 # <major>[.<minor>[.<patch>[.<tweak>]]]
66+
VERSION 1.9.0 # <major>[.<minor>[.<patch>[.<tweak>]]]
6767
LANGUAGES CXX)
6868

6969
set( JSONCPP_VERSION ${JSONCPP_VERSION_MAJOR}.${JSONCPP_VERSION_MINOR}.${JSONCPP_VERSION_PATCH} )
7070
message(STATUS "JsonCpp Version: ${JSONCPP_VERSION_MAJOR}.${JSONCPP_VERSION_MINOR}.${JSONCPP_VERSION_PATCH}")
71-
set( JSONCPP_SOVERSION 19 )
71+
set( JSONCPP_SOVERSION 21 )
7272

7373
option(JSONCPP_WITH_TESTS "Compile and (for jsoncpp_check) run JsonCpp test executables" ON)
7474
option(JSONCPP_WITH_POST_BUILD_UNITTEST "Automatically run unit-tests as a post build step" ON)

include/json/value.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ Json::Value obj_value(Json::objectValue); // {}
524524
/// Most general and efficient version of object-mutators.
525525
/// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
526526
/// \return non-zero, but JSON_ASSERT if this is neither object nor nullValue.
527-
Value const* demand(char const* begin, char const* end);
527+
Value* demand(char const* begin, char const* end);
528528
/// \brief Remove and return the named member.
529529
///
530530
/// Do nothing if it did not exist.

meson.build

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
project(
22
'jsoncpp',
33
'cpp',
4-
version : '1.8.4',
4+
version : '1.9.0',
55
default_options : [
66
'buildtype=release',
77
'cpp_std=c++11',
@@ -62,7 +62,7 @@ jsoncpp_lib = library(
6262
'src/lib_json/json_reader.cpp',
6363
'src/lib_json/json_value.cpp',
6464
'src/lib_json/json_writer.cpp'],
65-
soversion : 20,
65+
soversion : 21,
6666
install : true,
6767
include_directories : jsoncpp_include_directories,
6868
cpp_args: dll_export_flag)

src/lib_json/json_value.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1115,7 +1115,7 @@ bool Value::isValidIndex(ArrayIndex index) const { return index < size(); }
11151115

11161116
Value const* Value::find(char const* begin, char const* end) const {
11171117
JSON_ASSERT_MESSAGE(type() == nullValue || type() == objectValue,
1118-
"in Json::Value::find(key, end, found): requires "
1118+
"in Json::Value::find(begin, end): requires "
11191119
"objectValue or nullValue");
11201120
if (type() == nullValue)
11211121
return nullptr;
@@ -1126,6 +1126,12 @@ Value const* Value::find(char const* begin, char const* end) const {
11261126
return nullptr;
11271127
return &(*it).second;
11281128
}
1129+
Value* Value::demand(char const* begin, char const* end) {
1130+
JSON_ASSERT_MESSAGE(type() == nullValue || type() == objectValue,
1131+
"in Json::Value::demand(begin, end): requires "
1132+
"objectValue or nullValue");
1133+
return &resolveReference(begin, end);
1134+
}
11291135
const Value& Value::operator[](const char* key) const {
11301136
Value const* found = find(key, key + strlen(key));
11311137
if (!found)

src/test_lib_json/main.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,26 @@ JSONTEST_FIXTURE(ValueTest, objects) {
204204
JSONTEST_ASSERT_EQUAL(Json::Value(1234), constObject["id"]);
205205
JSONTEST_ASSERT_EQUAL(Json::Value(), constObject["unknown id"]);
206206

207+
// Access through find()
208+
const char idKey[] = "id";
209+
const Json::Value* foundId = object1_.find(idKey, idKey + strlen(idKey));
210+
JSONTEST_ASSERT(foundId != nullptr);
211+
JSONTEST_ASSERT_EQUAL(Json::Value(1234), *foundId);
212+
213+
const char unknownIdKey[] = "unknown id";
214+
const Json::Value* foundUnknownId = object1_.find(unknownIdKey, unknownIdKey + strlen(unknownIdKey));
215+
JSONTEST_ASSERT_EQUAL(nullptr, foundUnknownId);
216+
217+
// Access through demand()
218+
const char yetAnotherIdKey[] = "yet another id";
219+
const Json::Value* foundYetAnotherId = object1_.find(yetAnotherIdKey, yetAnotherIdKey + strlen(yetAnotherIdKey));
220+
JSONTEST_ASSERT_EQUAL(nullptr, foundYetAnotherId);
221+
Json::Value* demandedYetAnotherId = object1_.demand(yetAnotherIdKey, yetAnotherIdKey + strlen(yetAnotherIdKey));
222+
JSONTEST_ASSERT(demandedYetAnotherId != nullptr);
223+
*demandedYetAnotherId = "baz";
224+
225+
JSONTEST_ASSERT_EQUAL(Json::Value("baz"), object1_["yet another id"]);
226+
207227
// Access through non-const reference
208228
JSONTEST_ASSERT_EQUAL(Json::Value(1234), object1_["id"]);
209229
JSONTEST_ASSERT_EQUAL(Json::Value(), object1_["unknown id"]);

0 commit comments

Comments
 (0)