@@ -12,6 +12,9 @@ namespace Elastic.Clients.Elasticsearch;
1212
1313internal sealed class FieldConverter : JsonConverter < Field >
1414{
15+ private static readonly JsonEncodedText FieldProperty = JsonEncodedText . Encode ( "field" ) ;
16+ private static readonly JsonEncodedText FormatProperty = JsonEncodedText . Encode ( "format" ) ;
17+
1518 private IElasticsearchClientSettings _settings ;
1619
1720 public override void WriteAsPropertyName ( Utf8JsonWriter writer , Field value , JsonSerializerOptions options )
@@ -48,19 +51,19 @@ private static Field ReadObjectField(ref Utf8JsonReader reader)
4851 {
4952 if ( reader . TokenType == JsonTokenType . PropertyName )
5053 {
51- var propertyName = reader . GetString ( ) ;
52- reader . Read ( ) ;
53-
54- switch ( propertyName )
54+ if ( reader . ValueTextEquals ( FieldProperty . EncodedUtf8Bytes ) )
55+ {
56+ reader . Read ( ) ;
57+ field = reader . GetString ( ) ;
58+ }
59+ else if ( reader . ValueTextEquals ( FormatProperty . EncodedUtf8Bytes ) )
60+ {
61+ reader . Read ( ) ;
62+ format = reader . GetString ( ) ;
63+ }
64+ else
5565 {
56- case "field" :
57- field = reader . GetString ( ) ;
58- break ;
59- case "format" :
60- format = reader . GetString ( ) ;
61- break ;
62- default :
63- throw new JsonException ( "Unexpected property while reading `Field`." ) ;
66+ throw new JsonException ( $ "Unexpected property while reading `{ nameof ( Field ) } `.") ;
6467 }
6568 }
6669 }
@@ -70,19 +73,13 @@ private static Field ReadObjectField(ref Utf8JsonReader reader)
7073 return new Field ( field , format ) ;
7174 }
7275
73- throw new JsonException ( "Unable to read `Field` from JSON." ) ;
76+ throw new JsonException ( $ "Unable to read `{ nameof ( Field ) } ` from JSON.") ;
7477 }
7578
7679 public override void Write ( Utf8JsonWriter writer , Field value , JsonSerializerOptions options )
7780 {
7881 InitializeSettings ( options ) ;
7982
80- if ( value is null )
81- {
82- writer . WriteNullValue ( ) ;
83- return ;
84- }
85-
8683 var fieldName = _settings . Inferrer . Field ( value ) ;
8784
8885 if ( string . IsNullOrEmpty ( value . Format ) )
@@ -92,10 +89,8 @@ public override void Write(Utf8JsonWriter writer, Field value, JsonSerializerOpt
9289 else
9390 {
9491 writer . WriteStartObject ( ) ;
95- writer . WritePropertyName ( "field" ) ;
96- writer . WriteStringValue ( fieldName ) ;
97- writer . WritePropertyName ( "format" ) ;
98- writer . WriteStringValue ( value . Format ) ;
92+ writer . WriteString ( FieldProperty , fieldName ) ;
93+ writer . WriteString ( FormatProperty , value . Format ) ;
9994 writer . WriteEndObject ( ) ;
10095 }
10196 }
0 commit comments