JSON的C#自动性能反序列化 [英] C# automatic property deserialization of JSON

查看:472
本文介绍了JSON的C#自动性能反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要反序列化重新psented在JSON到一个适当的C#类$ P $一些JavaScript对象。由于自动性能的不错的功能,我想preFER让他们在这些类,而不是仅仅有田。不幸的是,.NET序列化引擎(至少,在默认情况下),完全忽略了对反序列化自动属性和只关心支持字段,这显然不是present JavaScript的对象

由于有没有标准的方式来命名支持字段并说实话我不吨甚至要费心了,让我们创建一个看起来它有C#支持字段的JavaScript对象的做法,因为它听起来有点脏,我可以序列JavaScript的领域,以C#自动性能的唯一途径,如果我能迫使序列化引擎以某种方式忽略支持字段并直接使用属性。不幸的是,我无法弄清楚如何做到这一点,或者这可以在所有完成。任何想法将AP preciated。

修改:下面是一个例子:

JavaScript的:

 函数猫()
{
    this.Name =威威;
    this.Breed =虎斑;
}
VAR猫=新的猫();
 

这是再序列化到{名称:威威}

在C#类:

  [序列化()]
公共类猫
{
    公共字符串名称{;组; }
    公共字符串品种{获得;组; }
}
 

和反序列化code,失败:

 新DataContractJsonSerializer(typeof运算(猫))的readObject(InputStream的)。
 

和它失败,因为它正在寻找支持字段是从异常明显。

EDIT2 :这里的异常,有没有什么帮助(没有内部例外):

  

System.Runtime.Serialization.SerializationException

     

数据契约型Test.Cat   无法反序列化,因为   所需的数据成员   <名称> k__BackingField,<品种> k__BackingField '不   找到。

解决方案

这里发生了什么是解串器试图猜测你的支持字段的名称。 您可以通过添加显式映射(DataContract /的DataMember属性)这样的解决这个问题:

  [DataContract]
公共类猫
{
    [数据成员]
    公共字符串名称{;组; }

    [数据成员]
    公共字符串品种{获得;组; }
}
 

I need to deserialize some JavaScript object represented in JSON to an appropriate C# class. Given the nice features of automatic properties, I would prefer having them in these classes as opposed to just having fields. Unfortunately, the .NET serialization engine (at least, by default) totally ignores automatic properties on deserialization and only cares about the backing field, which is obviously not present in the JavaScript object.

Given that there's no standard way to name backing fields and to be honest I don't even want to bother with the "let's create a JavaScript object that looks like it had C# backing fields" approach as it sounds a bit dirty, the only way I could serialize JavaScript fields to C# auto-properties if I could force the serialization engine to somehow ignore the backing field and use the property directly. Unfortunately, I can't figure out how this is done or if this can be done at all. Any ideas would be appreciated.

EDIT: Here's an example:

Javascript:

function Cat()
{
    this.Name = "Whiskers";
    this.Breed = "Tabby";
}
var cat = new Cat();

This is then serialized to "{Name: 'Whiskers'}".

The C# class:

[Serializable()]
public class Cat
{
    public string Name { get; set; }
    public string Breed { get; set; }
}

And the deserialization code, that fails:

new DataContractJsonSerializer(typeof(Cat)).ReadObject(inputStream);

And it is apparent from the exception that it fails because it is looking for the backing field.

EDIT2: Here's the exception, if that helps (no inner exceptions):

System.Runtime.Serialization.SerializationException

"The data contract type 'Test.Cat' cannot be deserialized because the required data members '<Name>k__BackingField, <Breed>k__BackingField' were not found."

解决方案

What's happening here is the deserializer is trying to guess the name of your backing fields. You can solve this by adding explicit mappings (DataContract/DataMember attributes) like this:

[DataContract]
public class Cat
{
    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public string Breed { get; set; }
}

这篇关于JSON的C#自动性能反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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