Skip to content

mysterious deserialization result for auto property initializer #229

@cubed-it

Description

@cubed-it

The following linqpad example...

<Query Kind="Program">
  <NuGetReference>NetJSON</NuGetReference>
  <Namespace>NetJSON</Namespace>
</Query>

void Main()
{
	var myObject1 = new SimpleObject1() { ID = 0, Name = "Test", Value = "Value" };
	var json1 = NetJSON.NetJSON.Serialize(myObject1, new NetJSONSettings() {SkipDefaultValue = false});
	json1.Dump();
	var recreatedObject1 = NetJSON.NetJSON.Deserialize<SimpleObject1>(json1);
	recreatedObject1.Dump();

	var myObject2 = new SimpleObject2() { ID = 0, Name = "Test", Value = "Value" };
	var json2 = NetJSON.NetJSON.Serialize(myObject2, new NetJSONSettings() {SkipDefaultValue = false});
	json2.Dump();
	var recreatedObject2 = NetJSON.NetJSON.Deserialize<SimpleObject2>(json2, new NetJSONSettings() {SkipDefaultValue = false});
	recreatedObject2.Dump();

	var myObject3 = new SimpleObject3() { ID = 0, Name = "Test", Value = "Value" };
	var json3 = NetJSON.NetJSON.Serialize(myObject3);
	json3.Dump();
	var recreatedObject3 = NetJSON.NetJSON.Deserialize<SimpleObject3>(json3);
	recreatedObject3.Dump();
}

public class SimpleObject1
{
	public int ID { get; set; }
	public string Name { get; set; }
	public string Value { get; set; }
}

public class SimpleObject2
{
	public int ID { get; set; } = -5;
	public string Name { get; set; }
	public string Value { get; set; }
}

public class SimpleObject3
{
	public SimpleObject3()
	{
		ID = 5;
	}
	
	public int ID { get; set; }
	public string Name { get; set; }
	public string Value { get; set; }
}

...produces the following result. SimpleObject2.Id should actually be 0, but the result is -5?

image

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions