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

Trying to Use GridDataModel.Deserialize(json) #42

Open
jasonhilldm opened this issue Jun 2, 2023 · 7 comments
Open

Trying to Use GridDataModel.Deserialize(json) #42

jasonhilldm opened this issue Jun 2, 2023 · 7 comments

Comments

@jasonhilldm
Copy link

I have installed the nuget package but the GridDataModel doesn't seem to have a static Deserialize method. How do I get access to that?

@abjerner
Copy link
Member

abjerner commented Jun 2, 2023

@jasonhilldm please remember to provide more than the bare minimum of information. Like what version of Umbraco and the package you're using. And what you're trying to do.

@jasonhilldm
Copy link
Author

Sorry…it’s the latest version of the package 5.0.2 and using Umbraco Heartcore with the Umbraco.Heartcore.Client.Net library for retrieving data from the API.

I am retrieving content from Umbraco which has a grid property with the grid json.

the documentation mentions being able to call GridDataModel.Deserialize(json) but this method is not found on the GridDataModel.

@abjerner
Copy link
Member

abjerner commented Jun 2, 2023

I'd imagine there are certain things that won't work when using the package outside of Umbraco. Eg. when working with a .NET frontend in a headless setup.

To answer your question, the package now has an IGridFactory that can be used to parse the grid model from a JObject value. This replaces GridDataModel.Deserialize (I can see that the README is a bit outdated). But the package's default implementation of the factory has a DI dependency for an Umbraco service like IGridConfig. Unless you're doing some extra to set up this as well, your .NET client won't have this service.

The package isn't really designed to be used outside of Umbraco, but I suppose you could write your own implementation for IGridFactory, but it's probably not a straight forward task.

@jasonhilldm
Copy link
Author

Hmmm...sounds like it's going to be tricky to get this working with Heartcore. Are you aware of any other packages that provide strongly typed grid support for Heartcore?

@abjerner
Copy link
Member

abjerner commented Jun 3, 2023

I haven't used Heartcore, but I'm fairly certain there isn't a package that provides strongly typed models outside of Umbraco. But you should be able to copy Umbraco's build in models from here:

https://github.com/umbraco/Umbraco-CMS/blob/release-11.4.0/src/Umbraco.Infrastructure/Models/GridValue.cs

The overall model for the grid is the GridValue class, and if you're also using JSON.net, you should be able to call JsonConvert.DeserializeObject<GridValue>(yourJsonString) to parse a JSON string into a GridValue instance.

It's however worth mentioning that Umbraco's models only cover the "outer" grid structure, and only exposes the value of each grid control as a JToken value.

My package provides strongly typed models for the control values as well - at least for the build in grid editors. But then also a system for adding your own models for custom grid editors. This is why it - among other things - needs the IGridConfig service from Umbraco.

@jasonhilldm
Copy link
Author

Thanks for your help.

I was trying to use a custom JsonConverter to deserialize the values from individual grid controls into the correct control types but I couldn't get it to be called when using the .Net client library methods to fetch data from Heartcore, e.g. ContentDeliveryService.Content.GetByUrl().

I added a JsonConverter attribute to my GridControl class but this was not being called during deserialization. Do you know if it's possible to use custom JsonConverters in this way?

@abjerner
Copy link
Member

abjerner commented Jun 4, 2023

This depends a lot of the rest of your code around the grid. I think it should be possible to handle via a custom JSON converter for the grid control. But it might be hard getting to work with a JSON converter on the models for various grid control values.

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

No branches or pull requests

2 participants