From 1477c906ef79f66252d30661f52c59ee329efc57 Mon Sep 17 00:00:00 2001 From: Blake Niemyjski Date: Tue, 19 Apr 2016 14:40:09 -0500 Subject: [PATCH] Fixed an .NET Native issue that could cause serialization to fail. --- .../Serializer/DefaultJsonSerializer.cs | 46 ++++++++++--------- 1 file changed, 24 insertions(+), 22 deletions(-) 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; }