I have this class which I am trying to serialize\deserialize:
public class MyClass
{
private string value;
public string Value
{
get
{
return this.value;
}
set
{
if (value.Length != 5)
{
throw new ArgumentOutOfRangeException("value", value.Length, "Expecting 6 length.");
}
this.value = value;
}
}
public int intItem { get; set; }
public SubClass SubClass { get; set; }
}
public class SubClass
{
public static SubClass GetInstance()
{
return new SubClass();
}
private SubClass()
{ }
public int item { get; set; }
public string stinngitem { get; set; }
}
//Initialization for testing purpose:
JsonSerializer.SetDefaultResolver(Utf8Json.Resolvers.StandardResolver.AllowPrivateExcludeNull);
MyClass myclass = new MyClass();
myclass.SubClass = SubClass.GetInstance();
myclass.SubClass.item = 1;
//Serialization
Type type = myclass.GetType();
byte[] serialized = JsonSerializer.Serialize(myclass); //works fine
//deserialization
object instance = JsonSerializer.Deserialize<object>(serialized); //No error. This is what I would need to use
MyClass newMyclass = instance as MyClass; //returns null.
//Tried these too
MyClass newinstance = JsonSerializer.Deserialize<MyClass>(serialized); //error
MyClass anotherinstance = JsonSerializer.NonGeneric.Deserialize(type, serialized); //error
For all the deserialization operations the error I get is
System.NullReferenceException: 'Object reference not set to an instance of an object.
The reason is the Value property is null and it has a setter logic.
set
{
if (value.Length != 5)
{
throw new ArgumentOutOfRangeException("value", value.Length, "Expecting 6 length.");
}
this.value = value;
}
But I have set the AllowPrivateExcludeNull option.
When I try to deserialize with object as the type then it converts it all to a nested dictionary but lose all type information. So when cast to MyClass I get only a null.
I am able to do this just fine with JSON.NET. Is this a bug?
I have this class which I am trying to serialize\deserialize:
For all the deserialization operations the error I get is
System.NullReferenceException: 'Object reference not set to an instance of an object.
The reason is the Value property is null and it has a setter logic.
But I have set the AllowPrivateExcludeNull option.
When I try to deserialize with object as the type then it converts it all to a nested dictionary but lose all type information. So when cast to MyClass I get only a null.
I am able to do this just fine with JSON.NET. Is this a bug?