Skip to content

Support System.Text.Json #96

@niemyjski

Description

@niemyjski

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:

  1. 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);
  1. 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.
  2. 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.
  3. 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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions