Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert jsonDynamic to contentitem extension #16283

Closed
wants to merge 3 commits into from

Conversation

giannik
Copy link
Contributor

@giannik giannik commented Jun 10, 2024

resolves #16233

@giannik
Copy link
Contributor Author

giannik commented Jun 10, 2024

@gvkries please review.

@gvkries
Copy link
Contributor

gvkries commented Jun 10, 2024

I think it is beneficial to add support for a generic ToObject<T> functionality back to JsonDynamicObject and probably JsonDynamicValue as well. This was possible with Newtonsoft and would avoid having a specialized extension method just for ContentItem. It would also solve problems like #16221.

It would look like in this example:

@foreach (var foo in Model.ContentItem.Content.NamedBagPart.ContentItems)
{
    var contentItem = foo.ToObject<ContentItem>();
    ...
}

Note that this is actually possible right now, but only when using the JsonObject directly:

@foreach (var foo in Model.ContentItem.Content.NamedBagPart.ContentItems)
{
    var contentItem = ((JsonObject)foo).ToObject<ContentItem>();
    ...
}

We also have references to this style in the documentation, so we must fix it anyway IMHO.

var anchors = (Anchor[])Model.ContentItem.Content.Blog.Image.Anchors.ToObject<Anchor[]>();

It would just require something like public T? ToObject<T>() => _jsonObject.ToObject<T>(); in JsonDynamicObject and JsonDynamicValue, as there is an existing extension method for JsonNode.

@gvkries
Copy link
Contributor

gvkries commented Jun 10, 2024

I guess JsonDynamicArray must be considered as well.

@giannik
Copy link
Contributor Author

giannik commented Jun 10, 2024

@gvkries feel free to make any changes.

@hishamco hishamco changed the title convert jsonDynamic to contentitem extension Convert jsonDynamic to contentitem extension Jun 11, 2024
giannik and others added 2 commits June 11, 2024 13:47
…micObject.cs

Co-authored-by: Hisham Bin Ateya <hishamco_2007@yahoo.com>
…tentItemExtensions.cs

Co-authored-by: Hisham Bin Ateya <hishamco_2007@yahoo.com>
@gvkries
Copy link
Contributor

gvkries commented Jun 11, 2024

@gvkries feel free to make any changes.

I don't think I have access to your PR :) I will open another PR with a different approach.

@gvkries
Copy link
Contributor

gvkries commented Jun 11, 2024

@giannik Please have a look at #16292...

@giannik
Copy link
Contributor Author

giannik commented Jun 11, 2024

closing in favor of #16292

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

razor view does allow dynamic casting of property values using system.text.json
3 participants