Skip to content

Optimized FieldConverter #7736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ namespace Elastic.Clients.Elasticsearch;

internal sealed class FieldConverter : JsonConverter<Field>
{
private static readonly JsonEncodedText FieldProperty = JsonEncodedText.Encode("field");
private static readonly JsonEncodedText FormatProperty = JsonEncodedText.Encode("format");

private IElasticsearchClientSettings _settings;

public override void WriteAsPropertyName(Utf8JsonWriter writer, Field value, JsonSerializerOptions options)
Expand Down Expand Up @@ -48,19 +51,19 @@ private static Field ReadObjectField(ref Utf8JsonReader reader)
{
if (reader.TokenType == JsonTokenType.PropertyName)
{
var propertyName = reader.GetString();
reader.Read();

switch (propertyName)
if (reader.ValueTextEquals(FieldProperty.EncodedUtf8Bytes))
{
reader.Read();
field = reader.GetString();
}
else if (reader.ValueTextEquals(FormatProperty.EncodedUtf8Bytes))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For your info, I proposed an ability to pass an encoded text directly to the reader, but it was declined. See dotnet/runtime#86591.

{
reader.Read();
format = reader.GetString();
}
else
{
case "field":
field = reader.GetString();
break;
case "format":
format = reader.GetString();
break;
default:
throw new JsonException("Unexpected property while reading `Field`.");
throw new JsonException($"Unexpected property while reading `{nameof(Field)}`.");
}
}
}
Expand All @@ -70,19 +73,13 @@ private static Field ReadObjectField(ref Utf8JsonReader reader)
return new Field(field, format);
}

throw new JsonException("Unable to read `Field` from JSON.");
throw new JsonException($"Unable to read `{nameof(Field)}` from JSON.");
}

public override void Write(Utf8JsonWriter writer, Field value, JsonSerializerOptions options)
{
InitializeSettings(options);

if (value is null)
{
writer.WriteNullValue();
return;
}

var fieldName = _settings.Inferrer.Field(value);

if (string.IsNullOrEmpty(value.Format))
Expand All @@ -92,10 +89,8 @@ public override void Write(Utf8JsonWriter writer, Field value, JsonSerializerOpt
else
{
writer.WriteStartObject();
writer.WritePropertyName("field");
writer.WriteStringValue(fieldName);
writer.WritePropertyName("format");
writer.WriteStringValue(value.Format);
writer.WriteString(FieldProperty, fieldName);
writer.WriteString(FormatProperty, value.Format);
writer.WriteEndObject();
}
}
Expand Down