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

.NET 5.0 System.Text.Json Serialize/Deserialize throw exception #45221

Closed
jacksonwj opened this issue Nov 25, 2020 · 4 comments
Closed

.NET 5.0 System.Text.Json Serialize/Deserialize throw exception #45221

jacksonwj opened this issue Nov 25, 2020 · 4 comments

Comments

@jacksonwj
Copy link

Here is class and extend function

// class
public class Res
{
    public int Code { get; set; }
    public string Msg { get; set; }

    public Res(int code = 200, string msg = "Success")
    {
        Code = code;
        Msg = msg;
    }
}

public class Res<T> : Res
{
    public T Body { get; set; }

    public Res(T body = default, int code = 200, string msg = "Success")
        : base(code, msg)
    {
        Body = body;
    }
}
// Extend function ToJson()
public static class Ext
{
    private static readonly JsonSerializerOptions _opts;

    static Ext()
    {
        _opts = new JsonSerializerOptions
        {
            // ... default configuration
        };
    }

    // Serialize to JSON string from entity
    public static string ToJson(this object data, JsonSerializerOptions options = null)
    {
        if (data == null)
        {
            return string.Empty;
        }

        return JsonSerializer.Serialize(data, options ?? _opts);   // throw exception when data has Value Type body
    }

    // Deserialize to entity from JSON string
    public Static T ToJson<T>(this string json, JsonSerializerOptions options = null)
    {
        if (string.IsNullOrWhiteSpace(json))
        {
            return default;
        }

        return JsonSerializer.Deserialize<T>(json, options ?? _opts);   // throw exception when data has Value Type body
    }
}

When I serialize an entity to JSON string by System.Text.Json.JsonSerializer (.NET 5.0), it throw an exception.
Deserialize also has the same exception.

var res0 = new Res();
var res0Json = res0.ToJson();   // Success (Correct JSON string)

var res1 = new Res<string>("a");   // body is Reference Type
var res1Json = res1.ToJson();   // Success (Correct JSON string)

var res2 = new Res<int>(1);   // body is Value Type
var res2Json = res2.ToJson();   // Fail (Throw an exception: Object reference not set to an instance of an object.)

It seems that: Value Type CANNOT Serialize/Deserialize, but Reference Type CAN

How do I resolve the exception?

Thanks

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-System.Text.Json untriaged New issue has not been triaged by the area owner labels Nov 25, 2020
@huoyaoyuan
Copy link
Member

It should be an internal bug. NullReferenceException should never be thrown in library code. Arguments verification failure should throw other exceptions.

@jacksonwj
Copy link
Author

It should be an internal bug. NullReferenceException should never be thrown in library code. Arguments verification failure should throw other exceptions.

The code CAN RUN without exception in .NET Core 3.1 or before.

@benaadams
Copy link
Member

Duplicate of #43757

Workarounds #43757 (comment)

Fix #43831

@layomia
Copy link
Contributor

layomia commented Dec 1, 2020

Closing as dup of #43757. I'll propose porting the fix (#43831) to a servicing release for .NET 5.0.

@layomia layomia closed this as completed Dec 1, 2020
@layomia layomia removed the untriaged New issue has not been triaged by the area owner label Dec 1, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 31, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants