@@ -58,33 +58,35 @@ public virtual object Deserialize(string json, Type type) {
58
58
}
59
59
60
60
private bool ShouldSerialize ( JsonTextWriterWithDepth jw , JsonProperty property , object obj , int maxDepth , IEnumerable < string > excludedPropertyNames ) {
61
- if ( excludedPropertyNames != null && property . PropertyName . AnyWildcardMatches ( excludedPropertyNames , true ) )
62
- return false ;
63
-
64
- bool isPrimitiveType = DefaultContractResolver . IsJsonPrimitiveType ( property . PropertyType ) ;
65
- bool isPastMaxDepth = ! ( isPrimitiveType ? jw . CurrentDepth <= maxDepth : jw . CurrentDepth < maxDepth ) ;
66
- if ( isPastMaxDepth )
67
- return false ;
61
+ try {
62
+ if ( excludedPropertyNames != null && property . PropertyName . AnyWildcardMatches ( excludedPropertyNames , true ) )
63
+ return false ;
68
64
69
- if ( isPrimitiveType )
70
- return true ;
65
+ bool isPrimitiveType = DefaultContractResolver . IsJsonPrimitiveType ( property . PropertyType ) ;
66
+ bool isPastMaxDepth = ! ( isPrimitiveType ? jw . CurrentDepth <= maxDepth : jw . CurrentDepth < maxDepth ) ;
67
+ if ( isPastMaxDepth )
68
+ return false ;
71
69
72
- object value = property . ValueProvider . GetValue ( obj ) ;
73
- if ( value == null )
74
- return false ;
70
+ if ( isPrimitiveType )
71
+ return true ;
75
72
76
- if ( typeof ( ICollection ) . IsAssignableFrom ( property . PropertyType ) ) {
77
- var collection = value as ICollection ;
78
- if ( collection != null && collection . Count == 0 )
73
+ object value = property . ValueProvider . GetValue ( obj ) ;
74
+ if ( value == null )
79
75
return false ;
80
- }
81
76
82
- var collectionType = value . GetType ( ) . GetInterfaces ( ) . FirstOrDefault ( i => i . IsGenericType && i . GetGenericTypeDefinition ( ) == typeof ( ICollection < > ) ) ;
83
- if ( collectionType != null ) {
84
- int count = ( int ) collectionType . GetProperty ( "Count" ) . GetValue ( value , null ) ;
85
- if ( count == 0 )
86
- return false ;
87
- }
77
+ if ( typeof ( ICollection ) . IsAssignableFrom ( property . PropertyType ) ) {
78
+ var collection = value as ICollection ;
79
+ if ( collection != null )
80
+ return collection . Count > 0 ;
81
+ }
82
+
83
+ var collectionType = value . GetType ( ) . GetInterfaces ( ) . FirstOrDefault ( i => i . IsGenericType && i . GetGenericTypeDefinition ( ) == typeof ( ICollection < > ) ) ;
84
+ if ( collectionType != null ) {
85
+ var countProperty = collectionType . GetProperty ( "Count" ) ;
86
+ if ( countProperty != null )
87
+ return ( int ) countProperty . GetValue ( value , null ) > 0 ;
88
+ }
89
+ } catch ( Exception ) { }
88
90
89
91
return true ;
90
92
}
0 commit comments