15

For example, there's an object like the next one:

public class Container
{
   public object Data { get; set; }
}

And it's used this way:

Container container = new Container
{
    Data = new Dictionary<string, object> { { "Text", "Hello world" } }
};

If I deserialize a JSON string obtained from serializing the above instance, the Data property, even if I provide the ExpandoObjectConverter, it's not deserialized as an ExpandoObject:

Container container = JsonConvert.Deserialize<Container>(jsonText, new ExpandoObjectConverter());

How can I deserialize a class property assigned with an anonymous object, or at least, not concrete type as an ExpandoObject?

EDIT:

Someone answered that I could just use the dynamic object. This won't work for me. I know I could go this way, but this isn't the case because I need an ExpandoObject. Thanks.

EDIT 2:

Some other user answered I could deserialize a JSON string into an ExpandoObject. This isn't the goal of this question. I need to deserialize a concrete type having a dynamic property. In the JSON string this property could be an associative array.

Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206

1 Answers1

32

Try this:

Container container = new Container
{
    Data = new Dictionary<string, object> { { "Text", "Hello world" } }
};

string jsonText = JsonConvert.SerializeObject(container);

var obj = JsonConvert.DeserializeObject<ExpandoObject>(jsonText, new ExpandoObjectConverter());

I found that doing this got me an ExpandoObject from the call to DeserializeObject. I think the issue with the code you have provided is that while you are supplying an ExpandoObjectConverter, you are asking Json.Net to deserialize a Container, so I would imagine that the ExpandoObjectConverter is not being used.

Edit:

If I decorate the Data property with [JsonConverter(typeof(ExpandoObjectConverter))] and use the code:

var obj = JsonConvert.DeserializeObject<Container>(jsonText);

Then the Data property is deserialized to an ExpandoObject, while obj is a Container.

nick_w
  • 14,758
  • 3
  • 51
  • 71
  • 1
    Thanks for the effort. But as the other answerer, I believe you didn't read the question carefully. I know that I can fully deserialize a JSON string into an `ExpandoObject`. It's harder than that: I need to deserialize a **concrete type** which has a dynamic property. I want the whole property as an `ExpandoObject` when deserialized. – Matías Fidemraizer Mar 18 '13 at 09:11
  • Ah, so in your example the `Data` property would be deserialized as an `ExpandoObject`? – nick_w Mar 18 '13 at 09:13
  • 2
    Sadly, the `Data` property isn't deserialized as an `ExpandoObject`, but a `JContainer` (a JSON.NET specific type). I need the whole `Data` as an `ExpandoObject`. – Matías Fidemraizer Mar 18 '13 at 09:15
  • I don't know why I didn't receive any notification. I'm going to try this! – Matías Fidemraizer Mar 21 '13 at 10:53
  • Great, great! It works. Thanks, it's going to be a good achievement in my developments. – Matías Fidemraizer Mar 21 '13 at 10:56