File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -819,6 +819,25 @@ public int Add (object value)
819819 return EnsureList ( ) . Add ( data ) ;
820820 }
821821
822+ public bool Remove ( object obj )
823+ {
824+ json = null ;
825+ if ( IsObject )
826+ {
827+ JsonData value = null ;
828+ if ( inst_object . TryGetValue ( ( string ) obj , out value ) )
829+ return inst_object . Remove ( ( string ) obj ) && object_list . Remove ( new KeyValuePair < string , JsonData > ( ( string ) obj , value ) ) ;
830+ else
831+ throw new KeyNotFoundException ( "The specified key was not found in the JsonData object." ) ;
832+ }
833+ if ( IsArray )
834+ {
835+ return inst_array . Remove ( ToJsonData ( obj ) ) ;
836+ }
837+ throw new InvalidOperationException (
838+ "Instance of JsonData is not an object or a list." ) ;
839+ }
840+
822841 public void Clear ( )
823842 {
824843 if ( IsObject ) {
Original file line number Diff line number Diff line change @@ -231,6 +231,49 @@ public void NullValue ()
231231 Assert . AreEqual ( json , data . ToJson ( ) ) ;
232232 }
233233
234+ [ Test ]
235+ public void RemoveValueFromObject ( )
236+ {
237+ string json = "{\" test1\" :1}" ;
238+
239+ JsonData data = new JsonData ( ) ;
240+ data [ "test1" ] = 1 ;
241+ data [ "test2" ] = 2 ;
242+ data . Remove ( "test2" ) ;
243+
244+ Assert . AreEqual ( json , data . ToJson ( ) ) ;
245+ }
246+
247+ [ Test ]
248+ public void RemoveValueFromNestedObject ( )
249+ {
250+ string json = "{\" test1\" :{\" test3\" :3}}" ;
251+
252+ JsonData data = new JsonData ( ) ;
253+ data [ "test1" ] = new JsonData ( ) ;
254+ data [ "test1" ] [ "test2" ] = 2 ;
255+ data [ "test1" ] [ "test3" ] = 3 ;
256+ data [ "test1" ] . Remove ( "test2" ) ;
257+
258+ Assert . AreEqual ( json , data . ToJson ( ) ) ;
259+ }
260+
261+ [ Test ]
262+ public void RemoveValueFromArray ( )
263+ {
264+ string json = "[\" test1\" ,2.0]" ;
265+
266+ JsonData data = new JsonData ( ) ;
267+ data . Add ( "test1" ) ;
268+ data . Add ( "test2" ) ;
269+ data . Remove ( "test2" ) ;
270+ data . Add ( 1 ) ;
271+ data . Add ( 2.0 ) ;
272+ data . Remove ( 1 ) ;
273+
274+ Assert . AreEqual ( json , data . ToJson ( ) ) ;
275+ }
276+
234277 [ Test ]
235278 public void PropertiesOrderTest ( )
236279 {
You can’t perform that action at this time.
0 commit comments