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.net can't deserialize simple struct, throws System.NullReferenceException #67781

Closed
sake402 opened this issue Apr 8, 2022 · 7 comments
Closed

Comments

@sake402
Copy link

sake402 commented Apr 8, 2022

Description

System.Text.Json is unable to deserialize a very basic struct

Reproduction Steps

using System;
					
public class Program
{
	
    public enum LengthUnitTypes
    {
        None,
        m,
        cm,
        mm,
        um,
        @in,
        ft,
        km,
        mi
    }
	public struct Length{
            public Length()
            {
            }

            public Length(double value, LengthUnitTypes unit)
            {
                Value = value;
                Unit = unit;
            }

            public double Value { get; set; } = 0;
            public LengthUnitTypes Unit { get; set; } = LengthUnitTypes.None;
	}
	public static void Main()
	{
		var l = new Length();
		var json = System.Text.Json.JsonSerializer.Serialize(l);
		Console.WriteLine(json);
		l = System.Text.Json.JsonSerializer.Deserialize<Length>(json);
		Console.WriteLine("Hello World");
	}
}

Result

{"Value":0,"Unit":0}
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at ValueSetter(Object , Double )
   at System.Text.Json.Serialization.Metadata.JsonPropertyInfo`1.ReadJsonAndSetMember(Object obj, ReadStack& state, Utf8JsonReader& reader)
   at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 utf8Json, JsonTypeInfo jsonTypeInfo, Nullable`1 actualByteCount)
   at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 json, JsonTypeInfo jsonTypeInfo)
   at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options)
   at Program.Main()
Command terminated by signal 6

See fiddle here https://dotnetfiddle.net/n21G3a

Expected behavior

No exception. And if something is wrong with class/struct declaration, throw a more descriptive exception.

Actual behavior

Throws null reference

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

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

ghost commented Apr 8, 2022

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

System.Text.Json is unable to deserialize a very basic struct

Reproduction Steps

using System;
					
public class Program
{
	
    public enum LengthUnitTypes
    {
        None,
        m,
        cm,
        mm,
        um,
        @in,
        ft,
        km,
        mi
    }
	public struct Length{
		public Length()
        {
        }

        public Length(double value, LengthUnitTypes unit)
        {
            Value = value;
            Unit = unit;
        }

        public double Value { get; set; } = 0;
        public LengthUnitTypes Unit { get; set; } = LengthUnitTypes.None;
	}
	public static void Main()
	{
		var l = new Length();
		var json = System.Text.Json.JsonSerializer.Serialize(l);
		Console.WriteLine(json);
		l = System.Text.Json.JsonSerializer.Deserialize<Length>(json);
		Console.WriteLine("Hello World");
	}
}

Result

{"Value":0,"Unit":0}
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at ValueSetter(Object , Double )
   at System.Text.Json.Serialization.Metadata.JsonPropertyInfo`1.ReadJsonAndSetMember(Object obj, ReadStack& state, Utf8JsonReader& reader)
   at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 utf8Json, JsonTypeInfo jsonTypeInfo, Nullable`1 actualByteCount)
   at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 json, JsonTypeInfo jsonTypeInfo)
   at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options)
   at Program.Main()
Command terminated by signal 6

See fiddle here https://dotnetfiddle.net/n21G3a

Expected behavior

No exception. And if something is wrong with class/struct declaration, throw a more descriptive exception.

Actual behavior

Throws null reference

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Author: sake402
Assignees: -
Labels:

area-System.Text.Json, untriaged

Milestone: -

@EgorBo
Copy link
Member

EgorBo commented Apr 8, 2022

Likely dupe of #62983 (fixed in 7.0)

@eiriktsarpalis
Copy link
Member

Can confirm that the issue has been resolved with the latest builds in .NET 7

@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Apr 9, 2022
@danmoseley
Copy link
Member

Should we consider for backport?

@EgorBo
Copy link
Member

EgorBo commented Apr 11, 2022

Should we consider for backport?

I asked that in #62989 but since nobody responded I didn't file one

@eiriktsarpalis
Copy link
Member

I'm guessing it got dropped because it happened over the holidays. I think we should backport it.

@EgorBo
Copy link
Member

EgorBo commented Apr 11, 2022

Ok, I'll file a backport

@ghost ghost locked as resolved and limited conversation to collaborators May 11, 2022
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

4 participants