@@ -308,7 +308,7 @@ private static MethodInfo GetConvOp (Type t1, Type t2)
308
308
return op ;
309
309
}
310
310
311
- private static object ReadValue ( Type inst_type , JsonReader reader )
311
+ private static object ReadValue ( Type inst_type , JsonReader reader , bool skipUnknownProperties = false )
312
312
{
313
313
reader . Read ( ) ;
314
314
@@ -398,7 +398,7 @@ private static object ReadValue (Type inst_type, JsonReader reader)
398
398
}
399
399
400
400
while ( true ) {
401
- object item = ReadValue ( elem_type , reader ) ;
401
+ object item = ReadValue ( elem_type , reader , skipUnknownProperties ) ;
402
402
if ( reader . Token == JsonToken . ArrayEnd )
403
403
break ;
404
404
@@ -435,29 +435,34 @@ private static object ReadValue (Type inst_type, JsonReader reader)
435
435
436
436
if ( prop_data . IsField ) {
437
437
( ( FieldInfo ) prop_data . Info ) . SetValue (
438
- instance , ReadValue ( prop_data . Type , reader ) ) ;
438
+ instance , ReadValue ( prop_data . Type , reader , skipUnknownProperties ) ) ;
439
439
} else {
440
440
PropertyInfo p_info =
441
441
( PropertyInfo ) prop_data . Info ;
442
442
443
443
if ( p_info . CanWrite )
444
444
p_info . SetValue (
445
445
instance ,
446
- ReadValue ( prop_data . Type , reader ) ,
446
+ ReadValue ( prop_data . Type , reader , skipUnknownProperties ) ,
447
447
null ) ;
448
448
else
449
- ReadValue ( prop_data . Type , reader ) ;
449
+ ReadValue ( prop_data . Type , reader , skipUnknownProperties ) ;
450
450
}
451
451
452
452
} else {
453
453
if ( ! t_data . IsDictionary )
454
+ {
455
+ if ( skipUnknownProperties )
456
+ continue ;
457
+
454
458
throw new JsonException ( String . Format (
455
459
"The type {0} doesn't have the " +
456
460
"property '{1}'" , inst_type , property ) ) ;
461
+ }
457
462
458
463
( ( IDictionary ) instance ) . Add (
459
464
property , ReadValue (
460
- t_data . ElementType , reader ) ) ;
465
+ t_data . ElementType , reader , skipUnknownProperties ) ) ;
461
466
}
462
467
463
468
}
@@ -862,11 +867,11 @@ public static T ToObject<T> (TextReader reader)
862
867
return ( T ) ReadValue ( typeof ( T ) , json_reader ) ;
863
868
}
864
869
865
- public static T ToObject < T > ( string json )
870
+ public static T ToObject < T > ( string json , bool skipUnknownProperties = false )
866
871
{
867
872
JsonReader reader = new JsonReader ( json ) ;
868
873
869
- return ( T ) ReadValue ( typeof ( T ) , reader ) ;
874
+ return ( T ) ReadValue ( typeof ( T ) , reader , skipUnknownProperties ) ;
870
875
}
871
876
872
877
public static IJsonWrapper ToWrapper ( WrapperFactory factory ,
0 commit comments