Skip to content

Commit

Permalink
Serialization between numbers and booleans.
Browse files Browse the repository at this point in the history
  • Loading branch information
oskar11120 committed Mar 23, 2024
1 parent 76f1c2b commit e5b6752
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions Apache.Druid.Querying/Json/DefaultSerializerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static class DefaultSerializerOptions
new PolymorphicSerializer<ILimitSpec.OrderBy>(),
UnixMilisecondsConverter.WithDateTimeOffset,
UnixMilisecondsConverter.WithDateTime,
AllowBoolFromStringConverter.Singleton
BoolStringNumberConverter.Singleton
}
};

Expand Down Expand Up @@ -98,11 +98,11 @@ public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions
}
}

public sealed class AllowBoolFromStringConverter : JsonConverter<bool>
public sealed class BoolStringNumberConverter : JsonConverter<bool>
{
public static readonly AllowBoolFromStringConverter Singleton = new();
public static readonly BoolStringNumberConverter Singleton = new();

private AllowBoolFromStringConverter()
private BoolStringNumberConverter()
{
}

Expand All @@ -113,11 +113,12 @@ public override bool Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSer
JsonTokenType.False => false,
JsonTokenType.String when reader.ValueTextEquals("true") || reader.ValueTextEquals("True") => true,
JsonTokenType.String when reader.ValueTextEquals("false") || reader.ValueTextEquals("False") => false,
JsonTokenType.Number => reader.GetInt32() is 1,
_ => throw new JsonException($"Could not deserialize {reader.GetString()} to {typeof(bool)}.")
};

public override void Write(Utf8JsonWriter writer, bool value, JsonSerializerOptions options)
=> writer.WriteBooleanValue(value);
=> writer.WriteNumberValue(value ? 1 : 0);
}
}
}

0 comments on commit e5b6752

Please sign in to comment.