我可以指示Json.NET反序列化,但不能序列化,特定的属性? [英] Can I instruct Json.NET to deserialize, but not serialize, specific properties?

查看:211
本文介绍了我可以指示Json.NET反序列化,但不能序列化,特定的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个AngularJS + MVC 5 +网页API 2应用程序,允许用户管理在浏览器对象的集合并立即提交所有更改在单击保存按钮时。当进行更改时,一个或多个属性添加到JavaScript对象:IsAdded,IsUpdated,IsRemoved。这些属性检查服务器端,以确定持久化模型时该怎么做。

I have an AngularJS + MVC 5 + Web API 2 app that allows users to manage collections of objects in the browser and commit all changes at once when a Save button is clicked. As changes are made, one or more properties are added to the JavaScript objects: IsAdded, IsUpdated, IsRemoved. The properties are checked server-side to determine what to do when persisting the model.

该模型是担任了通过Web API使用Json.NET,基类是

The model is served up using Json.NET via Web API, and the base class is:

public class CollectionItemViewModel : ICollectionItem
{
    public bool IsAdded { get; set; }
    public bool IsUpdated { get; set; }
    public bool IsRemoved { get; set; }
}

这个伟大的工程,但增加了克鲁夫特我的序列化JSON。我可以选择不带序列的 ShouldSerialize ,但也阻止他们反序列化。

This works great, but adds cruft to my serialized JSON. I can choose to not serialize these three properties with ShouldSerialize, but that also prevents them from deserializing.

public bool ShouldSerializeIsAdded()
{
    return false;
}

public bool ShouldSerializeIsUpdated()
{
    return false;
}

public bool ShouldSerializeIsRemoved()
{
    return false;
}

是否有可能反序列化,而不是使用序列化,特定的属性Json.NET ?

Is it possible to deserialize, but not serialize, specific properties using Json.NET?

推荐答案

您应该能够只使用ShouldSerialize *方法如图所示的问题。这些只是影响系列化,不反序列化。

You should be able to just use the ShouldSerialize* methods as shown in the question. These only impact serialization, not deserialization.

这篇关于我可以指示Json.NET反序列化,但不能序列化,特定的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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