1

Closed

Root type not handled correctly

description

The first test below should succeed, but it fails. It does succeed however with TypeNameHandling.Object and TypeNameHandling.All.
 
 
Basically, auto does not seem to handle (is not aware of) the root object inheritance, but it does handle all the properties correctly.
 
 
    private class A
    {
        public A PropertyOfA { get; set; }
    }
 
    private class B : A { }
 
    [TestMethod]
    public void AutoDeserializeObject_Success()
    {
        //THIS FAILS!!
        var b = new B();
        var bAsJson = JsonConvert.SerializeObject(b, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto });
        var aFromJson = JsonConvert.DeserializeObject<A>(bAsJson, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto });
        Assert.IsTrue(aFromJson is B);
    }
 
    [TestMethod]
    public void AutoDeserializeProperty_Success()
    {
        var a = new A { PropertyOfA = new B()};
        var aAsJson = JsonConvert.SerializeObject(a, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto });
        var aFromJson = JsonConvert.DeserializeObject<A>(aAsJson, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto });
        Assert.IsTrue(aFromJson is A);
        Assert.IsTrue(aFromJson.PropertyOfA is B);
    }
Closed Jun 9, 2012 at 12:57 AM by JamesNK
Not a bug. Use a standard class with the unknown object as a child of it.

comments