diff --git a/Source/Shared/Serializer/DefaultJsonSerializer.cs b/Source/Shared/Serializer/DefaultJsonSerializer.cs index b2050c1a..d2ac7ae1 100644 --- a/Source/Shared/Serializer/DefaultJsonSerializer.cs +++ b/Source/Shared/Serializer/DefaultJsonSerializer.cs @@ -58,33 +58,35 @@ public virtual object Deserialize(string json, Type type) { } private bool ShouldSerialize(JsonTextWriterWithDepth jw, JsonProperty property, object obj, int maxDepth, IEnumerable excludedPropertyNames) { - if (excludedPropertyNames != null && property.PropertyName.AnyWildcardMatches(excludedPropertyNames, true)) - return false; - - bool isPrimitiveType = DefaultContractResolver.IsJsonPrimitiveType(property.PropertyType); - bool isPastMaxDepth = !(isPrimitiveType ? jw.CurrentDepth <= maxDepth : jw.CurrentDepth < maxDepth); - if (isPastMaxDepth) - return false; + try { + if (excludedPropertyNames != null && property.PropertyName.AnyWildcardMatches(excludedPropertyNames, true)) + return false; - if (isPrimitiveType) - return true; + bool isPrimitiveType = DefaultContractResolver.IsJsonPrimitiveType(property.PropertyType); + bool isPastMaxDepth = !(isPrimitiveType ? jw.CurrentDepth <= maxDepth : jw.CurrentDepth < maxDepth); + if (isPastMaxDepth) + return false; - object value = property.ValueProvider.GetValue(obj); - if (value == null) - return false; + if (isPrimitiveType) + return true; - if (typeof(ICollection).IsAssignableFrom(property.PropertyType)) { - var collection = value as ICollection; - if (collection != null && collection.Count == 0) + object value = property.ValueProvider.GetValue(obj); + if (value == null) return false; - } - var collectionType = value.GetType().GetInterfaces().FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICollection<>)); - if (collectionType != null) { - int count = (int)collectionType.GetProperty("Count").GetValue(value, null); - if (count == 0) - return false; - } + if (typeof(ICollection).IsAssignableFrom(property.PropertyType)) { + var collection = value as ICollection; + if (collection != null) + return collection.Count > 0; + } + + var collectionType = value.GetType().GetInterfaces().FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICollection<>)); + if (collectionType != null) { + var countProperty = collectionType.GetProperty("Count"); + if (countProperty != null) + return (int)countProperty.GetValue(value, null) > 0; + } + } catch (Exception) {} return true; }