Skip to content

Commit 1477c90

Browse files
committed
Fixed an .NET Native issue that could cause serialization to fail.
1 parent de38ab3 commit 1477c90

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

Source/Shared/Serializer/DefaultJsonSerializer.cs

+24-22
Original file line numberDiff line numberDiff line change
@@ -58,33 +58,35 @@ public virtual object Deserialize(string json, Type type) {
5858
}
5959

6060
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;
6864

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;
7169

72-
object value = property.ValueProvider.GetValue(obj);
73-
if (value == null)
74-
return false;
70+
if (isPrimitiveType)
71+
return true;
7572

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)
7975
return false;
80-
}
8176

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) {}
8890

8991
return true;
9092
}

0 commit comments

Comments
 (0)