@@ -267,6 +267,21 @@ JSONTEST_FIXTURE_LOCAL(ValueTest, objects) {
267267 did = object1_.removeMember (" some other id" , gotPtr);
268268 JSONTEST_ASSERT_EQUAL (nullptr , gotPtr);
269269 JSONTEST_ASSERT_EQUAL (true , did);
270+
271+ // Using other removeMember interfaces, the test idea is the same as above.
272+ object1_[" some other id" ] = " foo" ;
273+ const Json::String key (" some other id" );
274+ did = object1_.removeMember (key, &got);
275+ JSONTEST_ASSERT_EQUAL (Json::Value (" foo" ), got);
276+ JSONTEST_ASSERT_EQUAL (true , did);
277+ got = Json::Value (" bar" );
278+ did = object1_.removeMember (key, &got);
279+ JSONTEST_ASSERT_EQUAL (Json::Value (" bar" ), got);
280+ JSONTEST_ASSERT_EQUAL (false , did);
281+
282+ object1_[" some other id" ] = " foo" ;
283+ object1_.removeMember (key);
284+ JSONTEST_ASSERT_EQUAL (Json::nullValue, object1_[key]);
270285}
271286
272287JSONTEST_FIXTURE_LOCAL (ValueTest, arrays) {
@@ -314,6 +329,50 @@ JSONTEST_FIXTURE_LOCAL(ValueTest, arrays) {
314329 JSONTEST_ASSERT_EQUAL (Json::Value (17 ), got);
315330 JSONTEST_ASSERT_EQUAL (false , array1_.removeIndex (2 , &got)); // gone now
316331}
332+ JSONTEST_FIXTURE_LOCAL (ValueTest, resizeArray) {
333+ Json::Value array;
334+ {
335+ for (int i = 0 ; i < 10 ; i++)
336+ array[i] = i;
337+ JSONTEST_ASSERT_EQUAL (array.size (), 10 );
338+ // The length set is greater than the length of the array.
339+ array.resize (15 );
340+ JSONTEST_ASSERT_EQUAL (array.size (), 15 );
341+
342+ // The length set is less than the length of the array.
343+ array.resize (5 );
344+ JSONTEST_ASSERT_EQUAL (array.size (), 5 );
345+
346+ // The length of the array is set to 0.
347+ array.resize (0 );
348+ JSONTEST_ASSERT_EQUAL (array.size (), 0 );
349+ }
350+ {
351+ for (int i = 0 ; i < 10 ; i++)
352+ array[i] = i;
353+ JSONTEST_ASSERT_EQUAL (array.size (), 10 );
354+ array.clear ();
355+ JSONTEST_ASSERT_EQUAL (array.size (), 0 );
356+ }
357+ }
358+ JSONTEST_FIXTURE_LOCAL (ValueTest, getArrayValue) {
359+ Json::Value array;
360+ for (int i = 0 ; i < 5 ; i++)
361+ array[i] = i;
362+
363+ JSONTEST_ASSERT_EQUAL (array.size (), 5 );
364+ const Json::Value defaultValue (10 );
365+ Json::ArrayIndex index = 0 ;
366+ for (; index <= 4 ; index++)
367+ JSONTEST_ASSERT_EQUAL (index, array.get (index, defaultValue).asInt ());
368+
369+ index = 4 ;
370+ JSONTEST_ASSERT_EQUAL (array.isValidIndex (index), true );
371+ index = 5 ;
372+ JSONTEST_ASSERT_EQUAL (array.isValidIndex (index), false );
373+ JSONTEST_ASSERT_EQUAL (defaultValue, array.get (index, defaultValue));
374+ JSONTEST_ASSERT_EQUAL (array.isValidIndex (index), false );
375+ }
317376JSONTEST_FIXTURE_LOCAL (ValueTest, arrayIssue252) {
318377 int count = 5 ;
319378 Json::Value root;
@@ -1964,7 +2023,89 @@ JSONTEST_FIXTURE_LOCAL(ValueTest, precision) {
19642023 result = Json::writeString (b, v);
19652024 JSONTEST_ASSERT_STRING_EQUAL (expected, result);
19662025}
2026+ JSONTEST_FIXTURE_LOCAL (ValueTest, searchValueByPath) {
2027+ Json::Value root, subroot;
2028+ root[" property1" ][0 ] = 0 ;
2029+ root[" property1" ][1 ] = 1 ;
2030+ subroot[" object" ] = " object" ;
2031+ root[" property2" ] = subroot;
2032+
2033+ const Json::Value defaultValue (" error" );
2034+ Json::FastWriter writer;
19672035
2036+ {
2037+ const Json::String expected (" {"
2038+ " \" property1\" :[0,1],"
2039+ " \" property2\" :{\" object\" :\" object\" }"
2040+ " }\n " );
2041+ Json::String outcome = writer.write (root);
2042+ JSONTEST_ASSERT_STRING_EQUAL (expected, outcome);
2043+
2044+ // Array member exists.
2045+ const Json::Path path1 (" .property1.[%]" , 1 );
2046+ Json::Value result = path1.resolve (root);
2047+ JSONTEST_ASSERT_EQUAL (Json::Value (1 ), result);
2048+ result = path1.resolve (root, defaultValue);
2049+ JSONTEST_ASSERT_EQUAL (Json::Value (1 ), result);
2050+
2051+ // Array member does not exist.
2052+ const Json::Path path2 (" .property1.[2]" );
2053+ result = path2.resolve (root);
2054+ JSONTEST_ASSERT_EQUAL (Json::nullValue, result);
2055+ result = path2.resolve (root, defaultValue);
2056+ JSONTEST_ASSERT_EQUAL (defaultValue, result);
2057+
2058+ // Access array path form error
2059+ const Json::Path path3 (" .property1.0" );
2060+ result = path3.resolve (root);
2061+ JSONTEST_ASSERT_EQUAL (Json::nullValue, result);
2062+ result = path3.resolve (root, defaultValue);
2063+ JSONTEST_ASSERT_EQUAL (defaultValue, result);
2064+
2065+ // Object member exists.
2066+ const Json::Path path4 (" .property2.%" , " object" );
2067+ result = path4.resolve (root);
2068+ JSONTEST_ASSERT_EQUAL (Json::Value (" object" ), result);
2069+ result = path4.resolve (root, defaultValue);
2070+ JSONTEST_ASSERT_EQUAL (Json::Value (" object" ), result);
2071+
2072+ // Object member does not exist.
2073+ const Json::Path path5 (" .property2.hello" );
2074+ result = path5.resolve (root);
2075+ JSONTEST_ASSERT_EQUAL (Json::nullValue, result);
2076+ result = path5.resolve (root, defaultValue);
2077+ JSONTEST_ASSERT_EQUAL (defaultValue, result);
2078+
2079+ // Access object path form error
2080+ const Json::Path path6 (" .property2.[0]" );
2081+ result = path5.resolve (root);
2082+ JSONTEST_ASSERT_EQUAL (Json::nullValue, result);
2083+ result = path6.resolve (root, defaultValue);
2084+ JSONTEST_ASSERT_EQUAL (defaultValue, result);
2085+
2086+ // resolve will not change the value
2087+ outcome = writer.write (root);
2088+ JSONTEST_ASSERT_STRING_EQUAL (expected, outcome);
2089+ }
2090+ {
2091+ const Json::String expected (" {"
2092+ " \" property1\" :[0,1,null],"
2093+ " \" property2\" :{"
2094+ " \" hello\" :null,"
2095+ " \" object\" :\" object\" }}\n " );
2096+ Json::Path path1 (" .property1.[%]" , 2 );
2097+ Json::Value& value1 = path1.make (root);
2098+ JSONTEST_ASSERT_EQUAL (Json::nullValue, value1);
2099+
2100+ Json::Path path2 (" .property2.%" , " hello" );
2101+ Json::Value& value2 = path2.make (root);
2102+ JSONTEST_ASSERT_EQUAL (Json::nullValue, value2);
2103+
2104+ // make will change the value
2105+ const Json::String outcome = writer.write (root);
2106+ JSONTEST_ASSERT_STRING_EQUAL (expected, outcome);
2107+ }
2108+ }
19682109struct FastWriterTest : JsonTest::TestCase {};
19692110
19702111JSONTEST_FIXTURE_LOCAL (FastWriterTest, dropNullPlaceholders) {
0 commit comments