How to make JSON.Net serializer to call ToString() when serializing a particular type?

前端 未结 4 823
温柔的废话
温柔的废话 2020-11-28 10:02

I am using Newtonsoft.Json serializer to convert C# classes to JSON. For some classes I don\'t need the serializer to an instance to individual properties, but instead just

4条回答
  •  抹茶落季
    2020-11-28 10:28

    You can simply try Newtonsoft's JSON builder library and Serilaize the object of type Person using such code:

    Dictionary collection = new Dictionary()
        {
          {"First", new Person()},
          {"Second", new Person()},
    
        };
    string json = JsonConvert.SerializeObject(collection, Formatting.Indented, new JsonSerializerSettings
      {
        TypeNameHandling = TypeNameHandling.All,
        TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple
      });
    

提交回复
热议问题