Json.NET-防止重新序列化已序列化的属性 [英] Json.NET - prevent re-serializing an already-serialized property

查看:128
本文介绍了Json.NET-防止重新序列化已序列化的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ASP.NET Web API应用程序中,我正在使用的某些模型包含大量临时JSON,这些JSON仅在客户端有用.在服务器上,它只是作为字符串进入和退出关系数据库.性能是关键,而处理JSON字符串服务器端似乎毫无意义.

In an ASP.NET Web API application, some of the models I'm working with contain a chunk of ad-hoc JSON that is useful only on the client side. On the server it simply goes in and out of a relational database as a string. Performance is key, and it seems pointless to process the JSON string server side at all.

因此在C#中,想象这样一个对象:

So in C#, imagine an object like this:

new Person
{
    FirstName = "John",
    LastName = "Smith",
    Json = "{ \"Age\": 30 }"
};

默认情况下,Json.NET将像这样序列化此对象:

By default, Json.NET will serialize this object like this:

{
    "FirstName": "John",
    "LastName": "Smith",
    "Json": "{ \"Age\": 30 }"
}

我希望能够指示Json.NET假定Json属性已经是序列化的表示形式,因此它不应重新序列化,并且生成的JSON应该如下所示:

I'd like to be able to instruct Json.NET to assume that the Json property is already a serialized representation, thus it shouldn't re-serialize, and the resulting JSON should look like this:

{
    "FirstName": "John",
    "LastName": "Smith",
    "Json": {
        "Age": 30
    }
}

理想情况下,这可以双向执行,即在发布JSON表示形式时,它会自动反序列化为上面的C#表示形式.

Ideally this works in both directions, i.e. when POSTing the JSON representation it will automatically deserialize to the C# representation above.

使用Json.NET实现此目的的最佳机制是什么?我需要自定义JsonConverter吗?有没有更简单的基于属性的机制?效率很重要;整个要点是跳过序列化开销,这可能有点微优化,但是为了论证,我们假设不是. (可能会有大的列表,并且返回笨重的Json属性.)

What is the best mechanism to achieve this with Json.NET? Do I need a custom JsonConverter? Is there a simpler attribute-based mechanism? Efficiency matters; the whole point is to skip the serialization overhead, which could be a bit of a micro-optimization, but for argument's sake let's assume it's not. (There will potentially be big lists with bulky Json properties being returned.)

推荐答案

如果可以将PersonJson属性的类型从string更改为JRaw,则将获得所需的结果

If you are able to change the type of the Json property on Person from string to JRaw then you will get the desired result.

public class Person
{
    public string FirstName { get; set;}
    public string LastName { get; set;}        
    public JRaw Json  { get; set;}
} 

或者,您可以保留string属性,并添加JRaw属性用作序列化的代理:

Alternatively, you could keep the string property, and add a JRaw property to be used as a proxy for serialization:

public class Person
{
    public string FirstName { get; set;}
    public string LastName { get; set;}
    [JsonIgnore]
    public string Json { get; set; }

    [JsonProperty("Json")]
    private JRaw MyJson
    {
        get { return new JRaw(this.Json); }
        set { this.Json = value.ToString(); }
    }        
}

无论哪种方式,序列化和反序列化都可以按照您的要求进行.

Either way, both serialization and deserialization will work as you have requested.

这篇关于Json.NET-防止重新序列化已序列化的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆