Below is the relevant code I’ve written to return a ContentsResult using the Client Library. I would like to flatten DynamicContent, however I get the following error. What am I doing wrong? The schema for locations is simple and is similar to an address, but has a title field.
JsonSerializationException: Deserialized JSON type ‘Newtonsoft.Json.Linq.JValue’ is not compatible with expected type ‘Newtonsoft.Json.Linq.JObject’. Path ‘items[0].data.title’, line 1, position 221.
private readonly IContentsClient<DynamicContent, DynamicData> locationsClient;
public APIClient(IOptions<SquidexOptions> appOptions)
{
var options = appOptions.Value;
var clientManager = new SquidexClientManager(options);
locationsClient = clientManager.CreateDynamicContentsClient("locations");
}
var locationsResult = await _apiClient.GetLocationsAsync();
public async Task<ContentsResult<DynamicContent, DynamicData>> GetLocationsAsync()
{
var query = new ContentQuery {
Filter = $"data/slug/iv ne 'nowhere'" //trying to create an OData filter to select * so i can use the Flatten ctx below.
};
var ctx = QueryContext.Default.Flatten();
var locations = await locationsClient.GetAsync(query, ctx);
return locations;
}
I converted to a Content object and the code works. I guess I’m trying to figure out how to use DynamicData/DynamicContent so I don’t have to change the object model every time I update the Schema. Dynamics seem like a great way to do this, so I would love to find more documentation on using the Client Library with DynamicContent/DynamicData.
If you have a look to the content you will that DynamicData is a Dictionary of string to Object. You could say it is a bug or bad design because it would be better to use JToken here instead of a JObject.
@Sebastian - the screenshot is of the response I get from that GetLocationsAsync() call listed in the code above. When I attempt to flatten the data I get empty properties in the result. I created another method that uses strongly typed LocationData instead of DynamicData and get the field values just fine.
I would love to see an example of retrieving DynamicData using the client so I can use dynamic objects. I am proxying these responses to a reactjs front end, so there is little benefit for me to have strongly typed classes in my .net core project. I would like to query the content, return JSON, and consume it in my react site. I have business reasons for proxying the data through web API instead of calling Squidex directly from react.
Perhaps it is a problem with your serializer. ASP.NET Core uses System.Text.Json and the client library uses Newtonsoft.JSON. This could cause issues when you just copy the types over. Because STS does not understand JToken.