skip to Main Content

I have an application that serializes an object and sends a JSON string value as below.

public class Rootobject
{
    public int MessageState { get; set; }
    public object Message { get; set; }
    public object HeaderId { get; set; } 
}

public class Message
{
    public string FName { get; set; }
    public string LName { get; set; }
    public string Address { get; set; }       
}

Rootobject ro = new Rootobject();
ro.HeaderId = "HID";
ro.MessageState = 0;
ro.Message = new Message() { FName = "john", LName = "Doe", Address = "123456" };

string x = JsonConvert.SerializeObject(ro);

This x value is sent to another application and it is deserialized as below.

public class Rootobject1
{
    public int MessageState { get; set; }
    public object Message { get; set; }
    public object HeaderId { get; set; }
}

public class Message1
{
    public string FName { get; set; }
    public string LName { get; set; }
    public string Address { get; set; }
}

var y = JsonConvert.DeserializeObject<Rootobject1>(receivedMsg);

Here the nested object is not getting deserialized for me and is just a JObject.

deserializedobj

How can I deserialize the nested object? Kindly help.

Thanks.

2

Answers


  1. I don’t see the reason that defines the Message property in Rootobject1 as an object type as you know the data structure.

    The quick way to resolve this is to define Message with Message1 type.

    public class Rootobject1
    {
        public int MessageState { get; set; }
        public Message1 Message { get; set; }
        public object HeaderId { get; set; }
    }
    

    Alternatively, you may look to convert the JObject to the Message1 type.

    var message = ((JObject)y.Message).ToObject<Message1>();
    
    Login or Signup to reply.
  2. When the library tries to deserialize the message it does not know what type it should be. The Json will just be a tree of properties, there is nothing to indicate that Message should be deserialized to a Message1. Json will inspect the Rootobject1 to figure out the various types it should use, but if you only have object-property there is no type information to go on.

    There are a few solutions

    Use the actual type

    public Message1 Message { get; set; }
    

    This is by far the simplest and best way to handle the problem if your object model allows it.

    Specify type name handling

    string jsonTypeNameAll = JsonConvert.SerializeObject(stockholder, Formatting.Indented, new JsonSerializerSettings
    {
        TypeNameHandling = TypeNameHandling.All
    })
    

    This makes the serializer emit a $type="<typename>" property that it can use to reconstruct the correct type. But I would warn against this, by default it includes the full namespace, to it makes renaming and moving types difficult, and it can result in security problems. System.Text.Json uses attributes to specify the type name, and that is an approach I much prefer.

    Parse the json by hand

    You can use JsonTextReader to parse the json tree and do whatever you want with it. This allow for a high degree of flexibility, but is the most cumbersome to use.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search