Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json serialization fixes #814

Merged
merged 2 commits into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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