Using Json.net - partial custom serialization of a c# object

前端 未结 3 428
萌比男神i
萌比男神i 2020-12-11 02:44

I ma using Newtonsofts\' Json.Net to serialize some and array of objects to json. The objects have a common set of properties but also have Meta property which is a diction

3条回答
  •  佛祖请我去吃肉
    2020-12-11 03:15

    You could use the .Net DataContractJsonSerializer.

    For custom serialization, see:

    http://msdn.microsoft.com/en-us/library/system.runtime.serialization.idatacontractsurrogate.aspx

    One advantage of using IDataContractSurrogate (compared to simply adding properties to your class for serialization) is that you don't have to mix actual properties and serialization properties together in the same class.

    Another advantage (compared to having to do custom serialization against a property bag, ala KeyValuePairConverter) is that you only have to add attributes to properties on your classes (the actual type and the surrogate type) and you can write all your conversion/custom serialization code directly against those types. This keeps your code higher level, and lets the framework deal with the exact transport mechanism.

提交回复
热议问题