[英]Deserializing object[] that contains primitive types and one class type
I have an object array that contains strings and longs and this class: 我有一个包含字符串和long以及此类的对象数组:
public class SimpleMailAddress
{
public string Address { get; set; }
public string Name { get; set; }
public static implicit operator MailAddress(SimpleMailAddress m)
{
return new MailAddress(m.Address, m.Name);
}
}
However, when deserializing the JSON array with Json.Net, I get an anonymous type that contains Address
and Name
instead of a SimpleMailAddress
object. 但是,当使用Json.Net反序列化JSON数组时,我得到一个匿名类型,它包含
Address
和Name
而不是SimpleMailAddress
对象。
I don't want to create a strongly-typed object to deserialize into because it will not be reused and I'd have to create a lot of objects for it. 我不想创建要反序列化为的强类型对象,因为它不会被重用,因此我必须为其创建很多对象。 Is there a way to do this with Json.Net or any other library?
有没有办法用Json.Net或任何其他库来做到这一点?
This is how I'm serializing/deserializing: 这就是我要序列化/反序列化的方式:
var json = JsonConvert.SerializeObject(myObject);
var myObject = JsonConvert.DeserializeObject<MailMessageRequest>(json);
And MailMessageRequest
: 和
MailMessageRequest
:
public class MailMessageRequest
{
public string Mailer { get; set; }
public string Method { get; set; }
public object[] Args { get; set; }
}
Json does not contain any inherit knowledge about your SimpleMailAddress
class. Json不包含有关您的
SimpleMailAddress
类的任何继承知识。 So when you are telling it to deserialize, the fact that your Args property is of type Object
, the deserializer is doing the best it can (by creating an anonymous type). 因此,当您告诉它反序列化时,您的Args属性是
Object
类型的事实,反序列化器正在尽其所能(通过创建匿名类型)。 It just sees data, it has no knowledge that you want a SimpleMailAddress
object. 它只是看到数据,不知道您是否需要
SimpleMailAddress
对象。
Json.net has a JObject
class. Json.net有一个
JObject
类。 Try using that instead of Object for your Args parameter if the actual contents of Args may change type. 如果Args的实际内容可能会更改类型,请尝试使用该参数代替Object作为Args参数。
Then, as needed, you can read the data from the JObject
object. 然后,根据需要,您可以从
JObject
对象读取数据。
If you don't care about the actual contents of Args, then leave it as Object
and ignore it. 如果您不关心Args的实际内容,则将其保留为
Object
并忽略它。
Edit: JSon.Net can embed type information during serialization that can be used during deserialization. 编辑: JSon.Net可以在序列化过程中嵌入类型信息,这些信息可以在反序列化过程中使用。
Leave your Args
parameter as an Object
. 将
Args
参数保留为Object
。 Then use the TypeNameHandling
option of All
during both serialization and deserialization. 然后在序列化和反序列化过程中使用
All
的TypeNameHandling
选项。
var json = JsonConvert.SerializeObject(myObject, Formatting.None,
new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All });
var myObject = JsonConvert.DeserializeObject<MailMessageRequest>(json,
new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All });
You should end up with your Args
object as your desired SimpleMailAddress
object. 您应该最终将
Args
对象作为所需的SimpleMailAddress
对象。
The data contact serializers built into the.net framework have the concept of known types where you tell them what types to expect and it uses those during deserialization. .net框架中内置的数据联系序列化程序具有已知类型的概念,您可以在其中告知期望的类型,并在反序列化期间使用这些类型。
There is a built in Json data contract serializer but I'm not sure it will be compatible with your Json data, it may need to be serialized and deserialized via a datacontract serializer to work using this method. 有一个内置的Json数据协定序列化器,但是我不确定它是否与您的Json数据兼容,它可能需要通过datacontract序列化器进行序列化和反序列化才能使用此方法。
Got it. 得到它了。 I have to use these settings:
我必须使用以下设置:
var settings = new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All,
TypeNameAssemblyFormat = FormatterAssemblyStyle.Full
};
var json = JsonConvert.SerializeObject(object, Formatting.None, settings);
var object = JsonConvert.DeserializeObject<MailMessageRequest>(message.Body, settings);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.