41

I am using System.Text.Json package to use the serialization and deserialization.

I can deserialize a json string into an object when the type is explicitly specified like below.

var data = JsonSerializer.Deserialize<PersonType>(jsonString);

But the dynamic type does not work. Is it possible to deserialize without having to specify the type? Thank you!

var data = JsonSerializer.Deserialize<dynamic>(jsonString);
10
  • Does this answer your question? Deserialize JSON into C# dynamic object? Commented Mar 31, 2022 at 15:32
  • 5
    @Vulpex OP is specifically asking about System.Text.Json. Although using another deserialiser would work, I don't think it's relevant to this question. Commented Mar 31, 2022 at 15:35
  • 3
    github.com/dotnet/runtime/issues/31175 Commented Mar 31, 2022 at 15:37
  • 1
    @RahulSharma your answer in that question refers to Newtonsoft.JSON, doesn’t it? The issue I linked to above says it is not - and not planned to be - possible to deserialise to dynamic with System.Text.Json because it would mean adding a dependency - see github.com/dotnet/runtime/issues/31175#issuecomment-937646022 Commented Mar 31, 2022 at 17:48
  • 1
    I did read the answer - it uses JsonDocument.Parse which is a good way of doing it IMO - but I’m not seeing an actual dynamic object? Commented Mar 31, 2022 at 17:52

3 Answers 3

53

tl:dr JsonNode is the recommended way but dynamic typing with deserializing to ExpandoObject works and I am not sure why.

It is not possible to deserialize to dynamic in the way you want to. JsonSerializer.Deserialize<T>() casts the result of parsing to T. Casting something to dynamic is similar to casting to object

Type dynamic behaves like type object in most circumstances. In particular, any non-null expression can be converted to the dynamic type. The dynamic type differs from object in that operations that contain expressions of type dynamic are not resolved or type checked by the compiler. The compiler packages together information about the operation, and that information is later used to evaluate the operation at run time

docs.

The following code snippet shows this happening with your example.

var jsonString = "{\"foo\": \"bar\"}";
dynamic data = JsonSerializer.Deserialize<dynamic>(jsonString);
Console.WriteLine(data.GetType());

Outputs: System.Text.Json.JsonElement

The recommended approach is to use the new JsonNode which has easy methods for getting values. It works like this:

JsonNode data2 = JsonSerializer.Deserialize<JsonNode>(jsonString);
Console.WriteLine(data2["foo"].GetValue<string>());

And finally trying out this worked for me and gives you want you want but I am struggling to find documentation on why it works because according to this issue it should not be supported but this works for me. My System.Text.Json package is version 4.7.2

dynamic data = JsonSerializer.Deserialize<ExpandoObject>(jsonString);
Console.WriteLine(data.GetType());
Console.WriteLine(data.foo);
Sign up to request clarification or add additional context in comments.

5 Comments

Regarding your observation that JsonSerializer.Deserialize<ExpandoObject>() mysteriously works, my guess is that System.Text.Json is able to deserialize to ExpandoObject because it has a parameterless constructor and implements IDictionary<string, object>. However, the values are getting deserialized as JsonElement rather than as .NET primitives or nested expandos, which you can see if you do Console.WriteLine($"data.foo={data.foo}, data.foo.GetType()={data.foo.GetType()}");. Demo here: dotnetfiddle.net/nxKBB2. So there's no deserialization to a dynamic hierarchy built in.
@dbc do you know what is the solution to convert it to primitive types recursively ?
@SrivathsaHarishVenkataramana - It's not built in to System.Text.Json, but you could use a custom converter. ObjectAsPrimitiveConverter from this answer to C# - Deserializing nested json to nested Dictionary<string, object> has an option to recursively deserialize JSON objects to .NET ExpandoObjects and primitives. Demo here: dotnetfiddle.net/1Y6hI6
It's good that this solution exists but I'm pretty surprised it's not built into System.Text.Json - I can't imagine why anyone would want a dynamic object with properties that are raw strings and numbers are actually of type JsonElement.
The ExpandoObject only works because Console.WriteLine() implicitily calls .ToString(). It won't work if you're using a non-string value as there will be no auto-conversion. If you try to assign the value to a string variable it also won't work unless you explicitly call .ToString() or use .GetValue<T>() to cast to the appropriate type. It's good that we can do this but disappointing they didn't provide a better dynamic interface to walk these values in the same way that NewtonSoft's JsonValue/JsonObject does.
3

I have tried using System.Text.Json in a dynamic way and it just does not work in an easy and meaningful way it seems. So while not a direct answer to your question, but I was "forced" to use the good old Newtonsoft.Json that just works:

dynamic result = JObject.Parse(message);

Comments

-3

Yes, you can do the above JGilmore's answer and get a JsonNode or... JsonObject. But, specifically, you can also get a dynamic if you so desire by specifying object as well:

var jsonString = "{\"foo\": \"bar\"}";
dynamic dynam1 = JsonSerializer.Deserialize<object>(jsonString);
var jsonString2 = Convert.ToString(dynam1);  //Or ToString or Serialize...

To work with a JsonObject instead of a JsonNode, you should get System.Text.Json.Nodes, then:

JsonNode data2 = JsonSerializer.Deserialize<JsonNode>(jsonString);
var jsonObject = data2.AsObject();
var subNode = data2["foo"]  //yay, you got the value as a node... woot...

var objVal = subNode.GetValue<object>(); //wonky hope if you want int, it isn't string.
string objStr = objVal?.ToString();

1 Comment

I see a lot of downvotes, but I'm right. Love to see ya prove me wrong. StackOverflow is a place to provide alternatives and teach. If you can't say why you downvote, don't bother to do so.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.