runtime: JsonSerializer does not serialize structs
When I try to serialize struct to JSON I get an error.
Struct:
public struct MyStruct
{
public MyStruct(string name)
{
Name = name;
}
public string Name { get; }
}
Execution:
var myStruct = new MyStruct("Test")
var json = JsonSerializer.ToString(myStruct);
Exception: Exception has been thrown by the target of an invocation.
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture)
at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture)
at System.Text.Json.Serialization.JsonClassInfo.CreateProperty(Type declaredPropertyType, Type runtimePropertyType, PropertyInfo propertyInfo, Type parentClassType, JsonSerializerOptions options)
at System.Text.Json.Serialization.JsonClassInfo.AddProperty(Type propertyType, PropertyInfo propertyInfo, Type classType, JsonSerializerOptions options)
at System.Text.Json.Serialization.JsonClassInfo..ctor(Type type, JsonSerializerOptions options)
at System.Text.Json.Serialization.JsonSerializerOptions.GetOrAddClass(Type classType)
at System.Text.Json.Serialization.WriteStackFrame.Initialize(Type type, JsonSerializerOptions options)
at System.Text.Json.Serialization.JsonSerializer.WriteCore(ArrayBufferWriter`1 output, Object value, Type type, JsonSerializerOptions options)
at System.Text.Json.Serialization.JsonSerializer.WriteCoreString(Object value, Type type, JsonSerializerOptions options)
at System.Text.Json.Serialization.JsonSerializer.ToString[TValue](TValue value, JsonSerializerOptions options)
Inner exception: Cannot bind to the target method because its signature is not compatible with that of the delegate type.
at System.Delegate.CreateDelegate(Type type, MethodInfo method, Boolean throwOnBindFailure)
at System.Text.Json.Serialization.JsonPropertyInfoCommon`3..ctor(Type parentClassType, Type declaredPropertyType, Type runtimePropertyType, PropertyInfo propertyInfo, Type elementType, JsonSerializerOptions options)
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 15 (15 by maintainers)
Nope. I think we can review and merge your PR. If we can pull the whole feature in for 3.0 (including fields and nested structs), that would be great (especially if you are up for it)!
Sure, I will start as soon as the current PR gets merged. The next issue depends on it.
@YohDeadfall we change milestones of closed issues after the fact … I do it once per week/month as time permits.
I haven’t updated description of the PR to automatically close this issue, and it was filed later than I proposed the fix.
Serializing structs is not supported atm and is out-of-scope for 3.0. This is a known issue.
There’s a PR out for top-level struct support atm: https://github.com/dotnet/corefx/pull/36506
cc @steveharter