简体   繁体   English

反序列化包含原始类型和一个类类型的object []

[英]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数组时,我得到一个匿名类型,它包含AddressName而不是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. 然后在序列化和反序列化过程中使用AllTypeNameHandling选项。

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.

相关问题 对象类原语类型堆栈和堆 - Object Class Primitive Types Stack and Heap 将对象类型拆箱为基本类型和其他类型(模拟后期绑定) - Unboxing an Object type to a primitive and other types (simulating late-binding) 将字符串反序列化为包含 object 的字典 - Deserializing a string into a dictionary that contains an object 如何对3种对象类型中的任何一种进行序列化/反序列化,其中一种包含抽象类? - How to serialize-deserialize any of 3 object types, one of which contains an abstract class? 模板 class 用于原始类型的可空值 - template class for nullables on primitive types 使用不同的对象类型反序列化JSON - Deserializing JSON with different object types 反序列化不同对象类型的数组 - Deserializing an array of different object types 在反序列化 C# 之前,是否有办法检查 object 的 class 类型? - Is there anyway to check the class type of an object BEFORE deserializing in C#? 错误:无法创建类型为“ System.Object”的常量值。 在此上下文中仅支持原始类型或枚举类型 - Error: Unable to create a constant value of type 'System.Object'. Only primitive types or enumeration types are supported in this context 参数对象[type]的类型不是基本类型 - The type of the argument object [type] is not primitive
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM