public override void Write(Utf8JsonWriter writer, FlagOffset value, JsonSerializerOptions options) { writer.WriteStartObject(); writer.WritePropertyName(nameof(FlagOffset.Offsets)); writer.WriteStringValue(BasicOffsetConverter.ToString(value.Offsets)); writer.WritePropertyName(nameof(FlagOffset.On)); writer.WriteStringValue(ToString(value.On)); writer.WritePropertyName(nameof(FlagOffset.Off)); writer.WriteStringValue(ToString(value.Off)); writer.WriteEndObject(); }
public override FlagOffset Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { ulong[] offsets = null; byte[] on = null; byte[] off = null; while (reader.Read()) { if (reader.TokenType == JsonTokenType.EndObject) { if (offsets == null) { throw new JsonException("Flag offsets must have 'Offsets' value"); } return(new FlagOffset(offsets, on, off)); } if (reader.TokenType != JsonTokenType.PropertyName) { throw new JsonException(); } string propertyName = reader.GetString(); if (propertyName == nameof(FlagOffset.On)) { reader.Read(); on = ToBytes(reader.GetString()); } else if (propertyName == nameof(FlagOffset.Off)) { reader.Read(); off = ToBytes(reader.GetString()); } else if (propertyName == nameof(FlagOffset.Offsets)) { reader.Read(); offsets = BasicOffsetConverter.ToOffsets(reader.GetString()); } } throw new JsonException(); }