Skip to content

Commit

Permalink
Merge pull request #814 from Orckestra/json-serialization-fixes
Browse files Browse the repository at this point in the history
Json serialization fixes
  • Loading branch information
napernik authored Sep 27, 2022
2 parents 07dae80 + a68c838 commit af856ab
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
9 changes: 6 additions & 3 deletions Composite/Core/Serialization/CompositeJsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ public static T Deserialize<T>(string str)
var obj = JsonConvert.DeserializeObject<T>(str, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto,
Binder = CompositeSerializationBinder.Instance
Binder = CompositeSerializationBinder.Instance,
MaxDepth = 128
});

return obj;
Expand Down Expand Up @@ -168,7 +169,8 @@ public static T Deserialize<T>(params string[] strs)
var obj = JsonConvert.DeserializeObject<T>(combinedObj.ToString(), new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto,
Binder = CompositeSerializationBinder.Instance
Binder = CompositeSerializationBinder.Instance,
MaxDepth = 128
});

return obj;
Expand All @@ -184,7 +186,8 @@ public static object Deserialize(string str)
var obj = JsonConvert.DeserializeObject(str, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Objects,
Binder = CompositeSerializationBinder.Instance
Binder = CompositeSerializationBinder.Instance,
MaxDepth = 128
});

return obj;
Expand Down
25 changes: 18 additions & 7 deletions Composite/Core/Serialization/CompositeSerializationBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,30 @@ public override Type BindToType(string assemblyName, string typeName)

var type = base.BindToType(assemblyName, typeName);

if (!TypeIsSupported(assemblyName, typeName, type))
{
throw new NotSupportedException($"Not supported object type '{typeName}'");
}
VerityTypeIsSupported(new AssemblyName(assemblyName), typeName, type);

return type;
}

private bool TypeIsSupported(string assemblyName, string typeName, Type type)
private void VerityTypeIsSupported(AssemblyName assemblyName, string typeFullName, Type type)
{
assemblyName = new AssemblyName(assemblyName).Name;
if (!TypeIsSupported(assemblyName, typeFullName, type))
{
throw new NotSupportedException($"Not supported object type '{typeFullName}'");
}

if (assemblyName == typeof(object).Assembly.GetName().Name /* "mscorlib" */)
if (type.IsGenericType)
{
foreach (var typeArgument in type.GetGenericArguments())
{
VerityTypeIsSupported(typeArgument.Assembly.GetName(), typeArgument.FullName, typeArgument);
}
}
}

private bool TypeIsSupported(AssemblyName assemblyName, string typeName, Type type)
{
if (assemblyName.Name == typeof(object).Assembly.GetName().Name /* "mscorlib" */)
{
var dotOffset = typeName.LastIndexOf(".", StringComparison.Ordinal);
if (dotOffset > 0)
Expand Down

0 comments on commit af856ab

Please sign in to comment.