24
24
#nullable restore
25
25
namespace Elastic . Clients . Elasticsearch . Aggregations
26
26
{
27
- public sealed partial class ChildrenAggregate : Aggregate
27
+ [ JsonConverter ( typeof ( ChildrenAggregateConverter ) ) ]
28
+ public sealed partial class ChildrenAggregate : AggregateDictionary , IAggregate
28
29
{
29
- [ JsonInclude ]
30
- [ JsonPropertyName ( "aggregations" ) ]
31
- public Elastic . Clients . Elasticsearch . Aggregations . AggregateDictionary Aggregations { get ; init ; }
30
+ public ChildrenAggregate ( IReadOnlyDictionary < string , IAggregate > backingDictionary ) : base ( backingDictionary )
31
+ {
32
+ }
32
33
33
34
[ JsonInclude ]
34
35
[ JsonPropertyName ( "doc_count" ) ]
@@ -38,4 +39,49 @@ public sealed partial class ChildrenAggregate : Aggregate
38
39
[ JsonPropertyName ( "meta" ) ]
39
40
public Dictionary < string , object > ? Meta { get ; init ; }
40
41
}
42
+
43
+ internal sealed class ChildrenAggregateConverter : JsonConverter < ChildrenAggregate >
44
+ {
45
+ public override ChildrenAggregate ? Read ( ref Utf8JsonReader reader , Type typeToConvert , JsonSerializerOptions options )
46
+ {
47
+ if ( reader . TokenType != JsonTokenType . StartObject )
48
+ throw new JsonException ( $ "Expected { JsonTokenType . StartObject } but read { reader . TokenType } .") ;
49
+ var subAggs = new Dictionary < string , IAggregate > ( ) ; // TODO - Optimise this and only create if we need it.
50
+ long docCount = default ;
51
+ Dictionary < string , object > ? meta = default ;
52
+ while ( reader . Read ( ) )
53
+ {
54
+ if ( reader . TokenType == JsonTokenType . EndObject )
55
+ break ;
56
+ if ( reader . TokenType != JsonTokenType . PropertyName )
57
+ throw new JsonException ( $ "Expected { JsonTokenType . PropertyName } but read { reader . TokenType } .") ;
58
+ var name = reader . GetString ( ) ; // TODO: Future optimisation, get raw bytes span and parse based on those
59
+ reader . Read ( ) ;
60
+ if ( name . Equals ( "doc_count" , StringComparison . Ordinal ) )
61
+ {
62
+ docCount = JsonSerializer . Deserialize < long > ( ref reader , options ) ;
63
+ continue ;
64
+ }
65
+
66
+ if ( name . Equals ( "meta" , StringComparison . Ordinal ) )
67
+ {
68
+ meta = JsonSerializer . Deserialize < Dictionary < string , object > ? > ( ref reader , options ) ;
69
+ continue ;
70
+ }
71
+
72
+ if ( name . Contains ( "#" ) )
73
+ {
74
+ AggregateDictionaryConverter . ReadAggregate ( ref reader , options , subAggs , name ) ;
75
+ continue ;
76
+ }
77
+
78
+ throw new JsonException ( "Unknown property read from JSON." ) ;
79
+ }
80
+
81
+ return new ChildrenAggregate ( subAggs )
82
+ { DocCount = docCount , Meta = meta } ;
83
+ }
84
+
85
+ public override void Write ( Utf8JsonWriter writer , ChildrenAggregate value , JsonSerializerOptions options ) => throw new NotImplementedException ( ) ;
86
+ }
41
87
}
0 commit comments