C# Don';t反序列化属性(将值设置为原始序列化数据)
当反序列化对象时,是否有一种简单的方法告诉反序列化程序“不要序列化此属性的内容,无论序列化的数据看起来如何,将其转储到此属性中” (序列化时显然相反) 因此,我有一个从JSON反序列化的类(当调用使用此类型的web方法时,.NET自动启动反序列化)。 传入JSON数据包中的大部分数据由服务器端代码使用,但这一属性仅由客户端使用 服务器端代码不需要知道该属性的内容,甚至不需要知道该属性的格式——它应该被视为一个黑匣子,只需按原样复制,并在适当的时间返回给客户端 如果我将属性的类型设置为字符串,反序列化将失败,因为它尝试在字符串对象上设置属性。 如果将属性的类型设置为object,则它包含一个部分反序列化的dictionary对象 如果整个对象只存在于内存中,那么dictionary对象就可以了,但事实上,整个对象将被重新序列化为xml[不用问,可以说是XSLT] 我可以将属性序列化为xml(或者直接返回JSON并将其存储在xml中),但这似乎是白费力气 一些代码块- 班级:C# Don';t反序列化属性(将值设置为原始序列化数据),c#,.net,json,jsonserializer,C#,.net,Json,Jsonserializer,当反序列化对象时,是否有一种简单的方法告诉反序列化程序“不要序列化此属性的内容,无论序列化的数据看起来如何,将其转储到此属性中” (序列化时显然相反) 因此,我有一个从JSON反序列化的类(当调用使用此类型的web方法时,.NET自动启动反序列化)。 传入JSON数据包中的大部分数据由服务器端代码使用,但这一属性仅由客户端使用 服务器端代码不需要知道该属性的内容,甚至不需要知道该属性的格式——它应该被视为一个黑匣子,只需按原样复制,并在适当的时间返回给客户端 如果我将属性的类型设置为字符串,反序
public class MyClass : IMyserializationInterface
{
public string PropertyThatShouldBeDeserializedAsNormal {get;set;}
[SomeAttr] public object PropertyThatShouldNotBeDeserialized {get;set;}
public void Deserialize(IDictionary<string, object> dictionary, System.Web.Script.Serialization.JavaScriptSerializer serializer, MySerializer customSerializer)
{
customSerializer.DefaultDeSerialize<MyClass>(this, dictionary, serializer);
customSerializer.PropagateParent(this);
}
web.config:
<configuration>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization>
<converters>
<add name="IMyserializationInterface" type="MySerializer"/>
</converters>
</jsonSerialization>
</webServices>
</scripting>
</system.web.extensions>
</configuration>
我正在寻找一种低开销的方法,可以简单地跳过特定属性的反序列化过程,也就是说,
不应序列化的属性将包含字符串{“An”:“Object”}
,很抱歉问了这么长的问题!不是复制品。这个问题询问如何从序列化中完全删除属性,这将使属性值保留为NULL—这不是我想要做的。这将需要专门的序列化。您必须自己编写序列化代码。
public class MySerializer: System.Web.Script.Serialization.JavaScriptConverter
{
//class processes some attributes to decide what to de/serialize
//methods in the class are supplied with the already part-deserialized dictionary object
//what we really need is the raw JSON
}
<configuration>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization>
<converters>
<add name="IMyserializationInterface" type="MySerializer"/>
</converters>
</jsonSerialization>
</webServices>
</scripting>
</system.web.extensions>
</configuration>
{
"PropertyThatShouldBeDeserializedAsNormal":"SomeString",
"PropertyThatShouldNotBeDeserialized": {"An":"Object"}
}