开发者

C# custom json serialization

开发者 https://www.devze.com 2023-04-10 05:53 出处:网络
This class class X{ public string a{get;set} public string b{get;set} } is serialized like {..., inst:{a:\"value\", b:\"value\"}, ...}

This class

class X{
    public string a{get;set}
    public string b{get;set}
}

is serialized like

{..., inst:{a:"value", b:"value"}, ...}

I need object of my class to be serialized like this:

{..., inst: x, ...}

Where x is a+b How can 开发者_运维百科I customize JSON serialization process from my class?


Checkout Newton-softs JSON serializer. Its pretty sweet.

Have you tried making a and b private, and then have something like

public string x { get { return a + b; } }


Try it like this

class X{
    public string a{private get;set;}
    public string b{private get;set;}
    public string x {get{ return a + b; }}
}

Having a private get removes it from serialization but still allows it to be set. The other option is to do it like this.

class X{
    public string a{set;}
    public string b{set;}
    public string x {get{ return a + b; }}
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号