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

System.Text.Json Serialization issue when constructor has ReadOnlySpan<char> parameter #48827

Closed
kepar opened this issue Feb 26, 2021 · 2 comments
Labels
area-System.Text.Json untriaged New issue has not been triaged by the area owner

Comments

@kepar
Copy link

kepar commented Feb 26, 2021

When trying to serialize an object with a ReadOnlySpan parameter in the constructor, I get an Unhandled exception. of type System.TypeLoadException with the message: The generic type 'System.Text.Json.Serialization.Converters.SmallObjectWithParameterizedConstructorConverter`5' was used with an invalid instantiation in assembly 'System.Text.Json, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.

I'm using .Net 5 with "System.Text.Json" Version="5.0.1"

This problem can be reproduced with this code: https://dotnetfiddle.net/edSoUp
copied here for reference:

using System;

var typeWithString = new TypeWithString("foo");
var typewithSpan = new TypeWithSpan("bar");

var working =  System.Text.Json.JsonSerializer.Serialize(typeWithString);

Console.WriteLine(working);

var json = System.Text.Json.JsonSerializer.Serialize(typewithSpan);

Console.WriteLine(json);


class TypeWithSpan
{ 
    public TypeWithSpan(ReadOnlySpan<char> value)
    {
		FirstLetter = value.Slice(0,1).ToString();
    }
	
	public string FirstLetter {get; }
}

class TypeWithString
{
    public TypeWithString(string value)
    {
        FirstLetter = value.Substring(0, 1);
    }

    public string FirstLetter { get; }
}

The serialization of the type TypeWithString works as expected, the serialization of the type TypeWithSpan fails with :
Unhandled exception. System.TypeLoadException: The generic type 'System.Text.Json.Serialization.Converters.SmallObjectWithParameterizedConstructorConverter`5' was used with an invalid instantiation in assembly 'System.Text.Json, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
at System.RuntimeTypeHandle.Instantiate(Type[] inst)
at System.RuntimeType.MakeGenericType(Type[] instantiation)
at System.Text.Json.Serialization.Converters.ObjectConverterFactory.CreateConverter(Type typeToConvert, JsonSerializerOptions options)
at System.Text.Json.Serialization.JsonConverterFactory.GetConverterInternal(Type typeToConvert, JsonSerializerOptions options)
at System.Text.Json.JsonSerializerOptions.GetConverter(Type typeToConvert)
at System.Text.Json.JsonSerializerOptions.DetermineConverter(Type parentClassType, Type runtimePropertyType, MemberInfo memberInfo)
at System.Text.Json.JsonClassInfo.GetConverter(Type type, Type parentClassType, MemberInfo memberInfo, Type& runtimeType, JsonSerializerOptions options)
at System.Text.Json.JsonClassInfo..ctor(Type type, JsonSerializerOptions options)
at System.Text.Json.JsonSerializerOptions.GetOrAddClass(Type type)
at System.Text.Json.JsonSerializerOptions.GetOrAddClassForRootType(Type type)
at System.Text.Json.WriteStack.Initialize(Type type, JsonSerializerOptions options, Boolean supportContinuation)
at System.Text.Json.JsonSerializer.WriteCore[TValue](Utf8JsonWriter writer, TValue& value, Type inputType, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.Serialize[TValue](TValue& value, Type inputType, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.Serialize[TValue](TValue value, JsonSerializerOptions options)
at $.

$(String[] args)

@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Text.Json untriaged New issue has not been triaged by the area owner labels Feb 26, 2021
@ghost
Copy link

ghost commented Feb 26, 2021

Tagging subscribers to this area: @eiriktsarpalis, @layomia
See info in area-owners.md if you want to be subscribed.

Issue Details

When trying to serialize an object with a ReadOnlySpan parameter in the constructor, I get an Unhandled exception. of type System.TypeLoadException with the message: The generic type 'System.Text.Json.Serialization.Converters.SmallObjectWithParameterizedConstructorConverter`5' was used with an invalid instantiation in assembly 'System.Text.Json, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.

I'm using .Net 5 with "System.Text.Json" Version="5.0.1"

This problem can be reproduced with this code: https://dotnetfiddle.net/edSoUp
copied here for reference:

`using System;

var typeWithString = new TypeWithString("foo");
var typewithSpan = new TypeWithSpan("bar");

var working = System.Text.Json.JsonSerializer.Serialize(typeWithString);

Console.WriteLine(working);

var json = System.Text.Json.JsonSerializer.Serialize(typewithSpan);

Console.WriteLine(json);

class TypeWithSpan
{
public TypeWithSpan(ReadOnlySpan value)
{
FirstLetter = value.Slice(0,1).ToString();
}

public string FirstLetter {get; }

}

class TypeWithString
{
public TypeWithString(string value)
{
FirstLetter = value.Substring(0, 1);
}

public string FirstLetter { get; }

}`

The serialization of the type TypeWithString works as expected, the serialization of the type TypeWithSpan fails with :
Unhandled exception. System.TypeLoadException: The generic type 'System.Text.Json.Serialization.Converters.SmallObjectWithParameterizedConstructorConverter`5' was used with an invalid instantiation in assembly 'System.Text.Json, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
at System.RuntimeTypeHandle.Instantiate(Type[] inst)
at System.RuntimeType.MakeGenericType(Type[] instantiation)
at System.Text.Json.Serialization.Converters.ObjectConverterFactory.CreateConverter(Type typeToConvert, JsonSerializerOptions options)
at System.Text.Json.Serialization.JsonConverterFactory.GetConverterInternal(Type typeToConvert, JsonSerializerOptions options)
at System.Text.Json.JsonSerializerOptions.GetConverter(Type typeToConvert)
at System.Text.Json.JsonSerializerOptions.DetermineConverter(Type parentClassType, Type runtimePropertyType, MemberInfo memberInfo)
at System.Text.Json.JsonClassInfo.GetConverter(Type type, Type parentClassType, MemberInfo memberInfo, Type& runtimeType, JsonSerializerOptions options)
at System.Text.Json.JsonClassInfo..ctor(Type type, JsonSerializerOptions options)
at System.Text.Json.JsonSerializerOptions.GetOrAddClass(Type type)
at System.Text.Json.JsonSerializerOptions.GetOrAddClassForRootType(Type type)
at System.Text.Json.WriteStack.Initialize(Type type, JsonSerializerOptions options, Boolean supportContinuation)
at System.Text.Json.JsonSerializer.WriteCore[TValue](Utf8JsonWriter writer, TValue& value, Type inputType, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.Serialize[TValue](TValue& value, Type inputType, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.Serialize[TValue](TValue value, JsonSerializerOptions options)
at $.

$(String[] args)

Author: kepar
Assignees: -
Labels:

area-System.Text.Json, untriaged

Milestone: -

@eiriktsarpalis
Copy link
Member

Duplicate of #44327. Thanks for reporting the issue.

@ghost ghost locked as resolved and limited conversation to collaborators Apr 28, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Text.Json untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

2 participants