runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Support "dynamic" and writable DOM

Open justinush opened this issue 7 years ago • 60 comments

Does JsonSerializer.Parse(String, Type, JsonSerializerOptions) support for dynamic ExpandoObject return type?

Something like this: dynamic p = JsonSerializer.Parse(json, typeof(ExpandoObject));

justinush avatar May 29 '19 03:05 justinush

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 avatar May 30 '19 04:05 ahsonkhan

@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

deinok avatar May 30 '19 14:05 deinok

@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.

tiksn avatar Aug 13 '19 13:08 tiksn

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.

SidShaytay avatar Aug 16 '19 17:08 SidShaytay

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?

MikeReznikov avatar Sep 18 '19 04:09 MikeReznikov

@MickeyReznikov : See https://stackoverflow.com/questions/15455304/deserialize-a-property-as-an-expandoobject-using-json-net or google “json.net expandoobject”

SidShaytay avatar Sep 18 '19 06:09 SidShaytay

@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

ahsonkhan avatar Sep 25 '19 20:09 ahsonkhan

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.

estiller avatar Sep 27 '19 14:09 estiller

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

ahsonkhan avatar Oct 03 '19 02:10 ahsonkhan

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

sheryever avatar Oct 08 '19 06:10 sheryever

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.

daviddesmet avatar Oct 12 '19 17:10 daviddesmet

Is it possible to have a milestone closer to now than Future? 🤔

ExpandoObject's been in the BCL for a looooong time

lfr avatar Oct 23 '19 17:10 lfr

Is there any alternative for ExpandoObject?

fatihyildizhan avatar Oct 25 '19 07:10 fatihyildizhan

@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

sheryever avatar Oct 26 '19 14:10 sheryever

I was surprised this isn't supported yet.

corbinmunce avatar Oct 28 '19 21:10 corbinmunce

Moving this to 5.0.

ahsonkhan avatar Nov 12 '19 02:11 ahsonkhan

Moving to 5.0? That means we have to wait for a year at least? Back to JSON.Net it is.

RobbyDeLaet avatar Nov 19 '19 13:11 RobbyDeLaet

5.0? wow, that definitely sucks.

Laiteux avatar Dec 10 '19 14:12 Laiteux

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

AlexeiScherbakov avatar Dec 15 '19 20:12 AlexeiScherbakov

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

Arash-Sabet avatar Jan 07 '20 18:01 Arash-Sabet