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

后端 未结 3 1007
谎友^
谎友^ 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:22

    Brian is absolutely correct, Json.NET has no way of knowing the compile-time declared type of the object it's being passed as the value parameter is declared as an object. The easy fix for this was if Json.NET added generic serialize methods so that the compile-time declared type would automatically flow over to Json.NET but the library's author has decided against my proposal for this here.

    As an alternative, I've wrapped all my json (de)serialization needs in a JsonHelper class with generic serialize methods which use the typeof expression to automatically pass the compile-time declared type of the value to be serialized.

提交回复
热议问题