Skip to content

Commit

Permalink
Fix serialisation of empty and single item Fields instances (#8018)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevejgordon authored and github-actions[bot] committed Jan 30, 2024
1 parent 76e9ea4 commit a75a17f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
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

0 comments on commit a75a17f

Please sign in to comment.