简体   繁体   中英

Deserializing object[] that contains primitive types and one class type

I have an object array that contains strings and longs and this class:

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.

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?

This is how I'm serializing/deserializing:

var json = JsonConvert.SerializeObject(myObject);
var myObject = JsonConvert.DeserializeObject<MailMessageRequest>(json);

And 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. 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). It just sees data, it has no knowledge that you want a SimpleMailAddress object.

Json.net has a JObject class. Try using that instead of Object for your Args parameter if the actual contents of Args may change type.

Then, as needed, you can read the data from the JObject object.

If you don't care about the actual contents of Args, then leave it as Object and ignore it.

Edit: JSon.Net can embed type information during serialization that can be used during deserialization.

Leave your Args parameter as an Object . Then use the TypeNameHandling option of All during both serialization and deserialization.

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.

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.

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM