Using JSON.net, how do I prevent serializing properties of a derived class, when used in a base class context?

后端 未结 6 1593
花落未央
花落未央 2020-12-01 00:20

Given a data model:

[DataContract]
public class Parent
{
    [DataMember]
    public IEnumerable Children { get; set; }
}

[DataContract]
publ         


        
6条回答
  •  情歌与酒
    2020-12-01 01:02

    Haven't compared performance implications, but this is a working solution as well, and works with nested/referenced objects as well.

    Derived d = new Derived();           
    string jsonStringD = JsonConvert.SerializeObject(d);
    Base b = new Base();
    JsonConvert.PopulateObject(jsonStringD, b);
    string jsonStringB = JsonConvert.SerializeObject(b);
    

提交回复
热议问题