Runtime: JsonSerializer does not serialize structs

Created on 4 May 2019  路  15Comments  路  Source: dotnet/runtime

When I try to serialize struct to JSON I get an error.

Struct:
```c#
public struct MyStruct
{
public MyStruct(string name)
{
Name = name;
}

public string Name { get; }

}

Execution:
```c#
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)
area-System.Text.Json enhancement

Most helpful comment

Does it mean that my PR will be merged only after 3.0 release?

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)!

All 15 comments

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

@ahsonkhan There is dotnet/runtime#29045 to track this.

Serializing structs is not supported atm and is out-of-scope for 3.0.

Does it mean that my PR will be merged only after 3.0 release?

/cc @karelz

Does it mean that my PR will be merged only after 3.0 release?

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.

This one is done, but there is non-parameterless constructor support yet.

@ahsonkhan @karelz Change the milestone please to 3.0.

We should change the milestone only after the PR gets merged.

dotnet/corefx#36506 was merged a day ago.

Why wasn't this issue closed as fixed then? Oversight?

I haven't updated description of the PR to automatically close this issue, and it was filed later than I proposed the fix.

I let @ahsonkhan to verify and close the right issues, etc.

I verified that struct support (including structs in deep object graphs) works as expected.

Closing as fixed. Thanks @YohDeadfall

```C#
[Fact]
public static void SerializeStructsWorks()
{
var myFoo = new Foo
{
NestedClass = new NestedFoo
{
SomeInt = 5,
AnotherStruct = new MyStruct
{
Name = "NestedTest"
}
},
Temp = "bar",
MyStruct = new MyStruct
{
Name = "Test"
}
};

var json = JsonSerializer.ToString(myFoo);
Assert.Equal("{\"Temp\":\"bar\",\"MyStruct\":{\"Name\":\"Test\"},\"NestedClass\":{\"SomeInt\":5,\"AnotherStruct\":{\"Name\":\"NestedTest\"}}}", json);

}

public class Foo
{
public string Temp { get; set; }
public MyStruct MyStruct { get; set; }
public NestedFoo NestedClass { get; set; }
}

public class NestedFoo
{
public int SomeInt { get; set; }
public MyStruct AnotherStruct { get; set; }
}

public struct MyStruct
{
public string Name { get; set; }
}
```

@ahsonkhan It still has Future as milestone.

@YohDeadfall we change milestones of closed issues after the fact ... I do it once per week/month as time permits.

Got it, thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

omajid picture omajid  路  3Comments

EgorBo picture EgorBo  路  3Comments

jamesqo picture jamesqo  路  3Comments

bencz picture bencz  路  3Comments

v0l picture v0l  路  3Comments