Hi Telerik Team,
Telerik is the best! Keep growing!
I'm using Telerik Blazor Grid with server-side grouping, and I’m manually group returned JSON structure with AggregateFunctionsGroup. I'm deserializing the result using a helper like this:
And just for some context:
My main concern:
group.Key always arrives from the backend as a JsonElement, so we have to inspect the ValueKind manually to convert it (e.g. to string).
This feels fragile — what if the key is a bool, int, DateTime, or some complex object, is it converted to string no matter what - for grouping???
Without additional metadata, it's hard to confidently deserialize Key to its intended type.
My question:
Is this the expected approach for deserializing grouped data with AggregateFunctionsGroup?
Would you consider adding a KeyType or some metadata in the grouped result to help with type-safe deserialization?
Is there a recommended way to deserialize group.Key if the grouping field can vary in type (e.g., string, int, bool)?
Thanks in advance — just want to ensure I’m handling this correctly and not missing a simpler or more robust pattern.
Best regards,
Bohdan
Telerik is the best! Keep growing!
I'm using Telerik Blazor Grid with server-side grouping, and I’m manually group returned JSON structure with AggregateFunctionsGroup. I'm deserializing the result using a helper like this:
public static List<AggregateFunctionsGroup>? DeserializeGroups<TGroupItem>(this List<AggregateFunctionsGroup> groups)
{
if (groups is null) return null;
for (int i = 0; i < groups.Count; i++)
{
var group = groups[i];
// Workaround for JsonElement -> string
if (group.Key is JsonElement elem && elem.ValueKind == JsonValueKind.String)
{
group.Key = elem.GetString();
}
if (group.HasSubgroups)
{
var subGroups = group.Items
.Cast<JsonElement>()
.Select(x => x.Deserialize<AggregateFunctionsGroup>())
.ToList();
group.Items = DeserializeGroups<TGroupItem>(subGroups);
}
else
{
var items = group.Items
.Cast<JsonElement>()
.Select(x => x.Deserialize<TGroupItem>())
.ToList();
group.Items = items;
}
}
return groups;
}
public async Task ReadItems(GridReadEventArgs args)
{
if (!ModelIsReady) return;
if (_readItemsPending) return;
if (string.IsNullOrWhiteSpace(PreparedQuery.Id)) return;
_readItemsPending = true;
try
{
var result = await ApiService.QueryDataSourceAsync<UniversalDSModel>(PreparedQuery.QueryId, args.Request, CancellationToken.None);
if (result is null) return;
if (args.Request.Groups.Count > 0)
{
args.Data = result?.GroupedData.DeserializeGroups<UniversalDSModel>();
}
else
{
args.Data = result?.CurrentPageData.Cast<object>().ToList();
}
if (result?.AggregateResults != null)
{
args.AggregateResults = result.AggregateResults;
}
if (result != null) { args.Total = result.TotalItemCount; }
}
catch (Exception ex)
{
Console.WriteLine($"Error loading data: {ex.Message}");
ApiRequestFailed = true;
}
finally
{
_readItemsPending = false;
LoaderContainerVisible = false;
}
}
My main concern:
group.Key always arrives from the backend as a JsonElement, so we have to inspect the ValueKind manually to convert it (e.g. to string).
This feels fragile — what if the key is a bool, int, DateTime, or some complex object, is it converted to string no matter what - for grouping???
Without additional metadata, it's hard to confidently deserialize Key to its intended type.
My question:
Is this the expected approach for deserializing grouped data with AggregateFunctionsGroup?
Would you consider adding a KeyType or some metadata in the grouped result to help with type-safe deserialization?
Is there a recommended way to deserialize group.Key if the grouping field can vary in type (e.g., string, int, bool)?
Thanks in advance — just want to ensure I’m handling this correctly and not missing a simpler or more robust pattern.
Best regards,
Bohdan