-
-
Notifications
You must be signed in to change notification settings - Fork 27
Closed
Labels
Description
Currently users of this library using System.Text.Json (STJ) cannot return search aggregation results (from redis cache, or from api endpoints) due to serialization dependencies on Newtonsoft.Json. Our goal is to update the Foundatio.Repositories to work with System.Text.Json. However, we don’t want to break other applications so we need to support Newtonsoft.Json by leaving the existing JsonConverters in place unless we can find a way to support both out of the box without the dependency or an easy way for it to be configured externally.
I see this happening in the following steps:
- Copy the following code snippet to end of
AggregationQueryTests.GetTermAggregationsWithTopHitsAsync()
.
string systemTextJson = System.Text.Json.JsonSerializer.Serialize(result);
Assert.Equal(json, systemTextJson);
roundTripped = System.Text.Json.JsonSerializer.Deserialize<CountResult>(systemTextJson);
Assert.Equal(10, roundTripped.Total);
Assert.Equal(1, roundTripped.Aggregations.Count);
Assert.Equal(10, roundTripped.Aggregations.Terms<int>("terms_age").Buckets.Count);
bucket = roundTripped.Aggregations.Terms<int>("terms_age").Buckets.First(f => f.Key == 19);
Assert.Equal(1, bucket.Total);
- Add custom converter to handle writing polymorphic json. The nest team is doing something like this: https://github.com/elastic/elasticsearch-net/blob/main/src/Elastic.Clients.Elasticsearch/_Generated/Types/Aggregations/TermsAggregation.g.cs#L27 for one of there aggretaions. I think you'd pass this converter in options of the first call to Serialize in the snippet above. This will solve the first assert.
- You'd need to add and convert the existing JsonConverters in the
Foundatio.Repositories.Utility
to System.Text.Json and then add the json converter attributes next to the existing converter attributes. This will fix the round trip part of reading the json text. - Request PR Review
Not sure if this link will help on the converters: https://bengribaudo.com/blog/2022/02/22/6569/recursive-polymorphic-deserialization-with-system-text-json