Why does Json.NET not include $type for the root object when TypeNameHandling is Auto?

后端 未结 3 1052
谎友^
谎友^ 2021-01-18 06:41

When I set Json.NET to serialize with TypeNameHandling set to TypeNameHandling.Auto, it correctly sets $type for child properties of an object but does not do so for the roo

3条回答
  •  情书的邮戳
    2021-01-18 07:16

    Newer versions of Json.Net allow you to pass the expected type to the serialize method

     ser.Serialize(stream, rootObject, typeof(BaseClass));
    

    You can pass the base class to the serialize method and TypeNameHandling.Auto will write the $type if the object and expected type do not match.

提交回复
热议问题