Support "dynamic" and writable DOM
Does JsonSerializer.Parse(String, Type, JsonSerializerOptions) support for dynamic ExpandoObject return type?
Something like this:
dynamic p = JsonSerializer.Parse(json, typeof(ExpandoObject));
No, this feature is not supported at this point but something we should consider for vNext. Do you have an example usage to motivate the feature request?
Marking as future.
System.NotSupportedException : The collection type 'System.Dynamic.ExpandoObject' is not supported.
@ahsonkhan GraphQL is a good example.
The spec recomends JSON but it is not tied to any specific serialization in the response. This implys that the "data" field of the response is of the kind: dynamic. As it can't be infered. Without ExpandoObject the deserialization makes dynamic a type of a JSON making. So accessing to that abstract dynamic "data" must be done knowing that in fact that dynamic is a JToken.
With ExpandoObject I think that we could enforce the access of the dynamic "data" like a common object
@ahsonkhan another example is Configuration Service in our project. It exposes collection like REST endpoints, which create collections in MongoDB (it is not just a dummy REST wrapper, and rest collections and mongo collection do not have exact 1-1 mapping, it also asserts certain rules).
So in our project we need dynamic/ExpandoObject support.
We use it in other microservices as well.
We also ran into this limitation. Our use case is gradually building a dynamic object before json serialization. Went back to the more mature Json.NET serializer.
Hey guys
whats the walk around for now? May i configure which one to use? @SidShetye you said something about went back to more mature one, may you explain please?
@MickeyReznikov : See https://stackoverflow.com/questions/15455304/deserialize-a-property-as-an-expandoobject-using-json-net or google “json.net expandoobject”
@MickeyReznikov, was your question answered? I believe @SidShetye meant going back to using Newtonsoft.Json for serializing dynamic objects since the in-box library (System.Text.Json) doesn't have support for those yet. In the case of asp.net apps, you can configure it to AddNewtonsoftJson back.
See https://docs.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.0&tabs=visual-studio#jsonnet-support
I also have a use-case for this - I just want to call a REST API with HttpClient and retrieve a single property from the response. I do not want to create a dedicated class just to parse the response... With JSON.NET I could just use "dynamic" and access the property of choice.
From https://github.com/dotnet/corefx/issues/41472 by @ghost1372:
hi I hope this is a good place to ask this question It seems we can't deserialize dynamic objects i used this code but not worked is there any way to do this?
var objList = System.Text.Json.JsonSerializer.Deserialize<List<dynamic>>(json);
In our many many applications we are controlling the fields of data from the stored procedures and dynamically render the List and Search List pages with jquery.jtable
Without JsonSerializer built-in support for ExpandoObject we cannot use dotnet core built-in Json Serialization
We do share the same use case as already mentioned by @estiller and @SidShetye We had to switch back to Json.NET in the meantime.
Is it possible to have a milestone closer to now than Future? 🤔
ExpandoObject's been in the BCL for a looooong time
Is there any alternative for ExpandoObject?
@fatihyildizhan No there is no alternate.
but we have written our own ExpandoObject converter, you can take the hint from this article ASP.NET Core 3.0: Custom JsonConverter for the new System.Text.Json
We need only the serialization, so we just create the serialization
I was surprised this isn't supported yet.
Moving this to 5.0.
Moving to 5.0? That means we have to wait for a year at least? Back to JSON.Net it is.
5.0? wow, that definitely sucks.
For temporal ugly workaround I can use JsonDocument with custom converter for it, but it is IDisposable.
public sealed class EventObject
{
[JsonPropertyName("id")]
public long Id
{
get; set;
}
[JsonPropertyName("eventData")]
[JsonConverter(typeof(JsonDocumentConverter))]
public System.Text.Json.JsonDocument EventData
{
get; set;
}
}
internal sealed class JsonDocumentConverter
: JsonConverter<System.Text.Json.JsonDocument>
{
public override JsonDocument Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return JsonDocument.ParseValue(ref reader);
}
public override void Write(Utf8JsonWriter writer, JsonDocument value, JsonSerializerOptions options)
{
value.WriteTo(writer);
}
}
We are no longer able to use the following POST action in the presence of System.Text.Json:
[HttpPost]
public async Task<IActionResult> SubmitAsync(dynamic model)
Instead, we had to use the following method but there was no straight forward to use 'model' in the downstream backend code:
[HttpPost]
public async Task<IActionResult> SubmitAsync(JsonElement model)
'model' is a complex object and it may contain a collection of objects and/or other nested complex objects. To be able to conclude a dynamic object out of JsonElement, we had to call model.GetRawText() and have Newtonsoft decode it into a dynamic object. This way is not the desired way because the main purpose of this exercise was to decommission Newtonsoft.json from the project.
Can I assume that addressing this ticket/issue implies a fix for our issue that we've been experiencing? It seems to be a bit urgent issue to address, so can it be addressed sooner than later?
/cc @ahsonkhan @terrajobst