File tree Expand file tree Collapse file tree 3 files changed +48
-0
lines changed Expand file tree Collapse file tree 3 files changed +48
-0
lines changed Original file line number Diff line number Diff line change @@ -395,6 +395,14 @@ Json::Value obj_value(Json::objectValue); // {}
395
395
Value removeMember (const char * key);
396
396
// / Same as removeMember(const char*)
397
397
Value removeMember (const std::string& key);
398
+ /* * \brief Remove the indexed array element.
399
+
400
+ O(n) expensive operations.
401
+ Update 'removed' iff removed.
402
+ (This is a better pattern than removeMember().)
403
+ \return true iff removed (no exceptions)
404
+ */
405
+ bool removeIndex (ArrayIndex i, Value* removed);
398
406
399
407
// / Return true if the object has a member named key.
400
408
bool isMember (const char * key) const ;
Original file line number Diff line number Diff line change @@ -1018,6 +1018,34 @@ Value Value::removeMember(const std::string& key) {
1018
1018
return removeMember (key.c_str ());
1019
1019
}
1020
1020
1021
+ bool Value::removeIndex (ArrayIndex index, Value* removed) {
1022
+ if (type_ != arrayValue) {
1023
+ return false ;
1024
+ }
1025
+ #ifdef JSON_VALUE_USE_INTERNAL_MAP
1026
+ JSON_FAIL_MESSAGE (" removeIndex is not implemented for ValueInternalArray." );
1027
+ return false ;
1028
+ #else
1029
+ CZString key (index);
1030
+ ObjectValues::iterator it = value_.map_ ->find (key);
1031
+ if (it == value_.map_ ->end ()) {
1032
+ return false ;
1033
+ }
1034
+ *removed = it->second ;
1035
+ ArrayIndex oldSize = size ();
1036
+ // shift left all items left, into the place of the "removed"
1037
+ for (ArrayIndex i = index; i < (oldSize - 1 ); ++i){
1038
+ CZString key (i);
1039
+ (*value_.map_ )[key] = (*this )[i + 1 ];
1040
+ }
1041
+ // erase the last one ("leftover")
1042
+ CZString keyLast (oldSize - 1 );
1043
+ ObjectValues::iterator itLast = value_.map_ ->find (keyLast);
1044
+ value_.map_ ->erase (itLast);
1045
+ return true ;
1046
+ #endif
1047
+ }
1048
+
1021
1049
#ifdef JSON_USE_CPPTL
1022
1050
Value Value::get (const CppTL::ConstString& key,
1023
1051
const Value& defaultValue) const {
Original file line number Diff line number Diff line change @@ -198,6 +198,14 @@ JSONTEST_FIXTURE(ValueTest, objects) {
198
198
199
199
object1_[" some other id" ] = " foo" ;
200
200
JSONTEST_ASSERT_EQUAL (Json::Value (" foo" ), object1_[" some other id" ]);
201
+ JSONTEST_ASSERT_EQUAL (Json::Value (" foo" ), object1_[" some other id" ]);
202
+
203
+ // Remove.
204
+ Json::Value got;
205
+ got = object1_.removeMember (" some other id" );
206
+ JSONTEST_ASSERT_EQUAL (Json::Value (" foo" ), got);
207
+ got = object1_.removeMember (" some other id" );
208
+ JSONTEST_ASSERT_EQUAL (Json::nullValue, got);
201
209
}
202
210
203
211
JSONTEST_FIXTURE (ValueTest, arrays) {
@@ -240,6 +248,10 @@ JSONTEST_FIXTURE(ValueTest, arrays) {
240
248
array1_[2 ] = Json::Value (17 );
241
249
JSONTEST_ASSERT_EQUAL (Json::Value (), array1_[1 ]);
242
250
JSONTEST_ASSERT_EQUAL (Json::Value (17 ), array1_[2 ]);
251
+ Json::Value got;
252
+ JSONTEST_ASSERT_EQUAL (true , array1_.removeIndex (2 , &got));
253
+ JSONTEST_ASSERT_EQUAL (Json::Value (17 ), got);
254
+ JSONTEST_ASSERT_EQUAL (false , array1_.removeIndex (2 , &got)); // gone now
243
255
}
244
256
245
257
JSONTEST_FIXTURE (ValueTest, null) {
You can’t perform that action at this time.
0 commit comments