Skip to content

Commit

Permalink
Fixed an .NET Native issue that could cause serialization to fail.
Browse files Browse the repository at this point in the history
  • Loading branch information
niemyjski committed Apr 19, 2016
1 parent de38ab3 commit 1477c90
Showing 1 changed file with 24 additions and 22 deletions.
46 changes: 24 additions & 22 deletions Source/Shared/Serializer/DefaultJsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,33 +58,35 @@ public virtual object Deserialize(string json, Type type) {
}

private bool ShouldSerialize(JsonTextWriterWithDepth jw, JsonProperty property, object obj, int maxDepth, IEnumerable<string> 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;
}
Expand Down

0 comments on commit 1477c90

Please sign in to comment.