Skip to content
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

Fix serialisation of empty and single item Fields instances #8018

Merged
merged 1 commit into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
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 @@ -27,7 +27,7 @@ public sealed class Fields : IUrlParameter, IEnumerable<Field>, IEquatable<Field

internal Fields() => ListOfFields = new List<Field>();

internal Fields(IEnumerable<Field> fieldNames) => ListOfFields = fieldNames.ToList();
internal Fields(IEnumerable<Field> fieldNames) => ListOfFields = new List<Field>(fieldNames);

private string DebugDisplay =>
$"Count: {ListOfFields.Count} [" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ internal sealed class FieldsConverter : JsonConverter<Fields>
Fields fields = reader.GetString();
return fields;
}
else if (reader.TokenType == JsonTokenType.StartArray)

if (reader.TokenType == JsonTokenType.StartArray)
{
var fields = new List<Field>();
while (reader.Read() && reader.TokenType != JsonTokenType.EndArray)
Expand All @@ -45,6 +46,19 @@ public override void Write(Utf8JsonWriter writer, Fields value, JsonSerializerOp
return;
}

if (value.ListOfFields.Count == 0)
{
writer.WriteStartObject();
writer.WriteEndObject();
return;
}

if (value.ListOfFields.Count == 1)
{
JsonSerializer.Serialize(writer, value.ListOfFields[0], options);
return;
}

writer.WriteStartArray();
foreach (var field in value.ListOfFields)
{
Expand Down
Loading