Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion docfx/docs/built-in-converters.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Enums, arrays and various dictionary types that utilize these types are implicit
## Other

- @System.Boolean
- @System.Drawing.Color
- <xref:System.Drawing.Color> - represented by an ARGB integer, or nil for `Color.Empty`.
- @System.Globalization.CultureInfo
- @System.Text.Encoding
- @System.EventArgs
Expand Down
20 changes: 15 additions & 5 deletions src/Nerdbank.MessagePack/Converters/BclConverters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,27 @@ namespace Nerdbank.MessagePack.Converters;
internal class SystemDrawingColorConverter : MessagePackConverter<Color>
{
public override Color Read(ref MessagePackReader reader, SerializationContext context)
=> Color.FromArgb(reader.ReadInt32());
=> reader.TryReadNil() ? Color.Empty : Color.FromArgb(reader.ReadInt32());

public override void Write(ref MessagePackWriter writer, in Color value, SerializationContext context)
=> writer.Write(value.ToArgb());
{
if (value.IsEmpty)
{
writer.WriteNil();
Comment thread
AArnott marked this conversation as resolved.
Resolved
}
else
{
writer.Write(value.ToArgb());
}
}

public override JsonObject? GetJsonSchema(JsonSchemaContext context, ITypeShape typeShape)
=> new()
{
["type"] = "integer",
["format"] = "int32",
["description"] = "An ARGB color value.",
["anyOf"] = new JsonArray(
new JsonObject { ["type"] = "null" },
new JsonObject { ["type"] = "integer", ["format"] = "int32" }),
Comment thread
AArnott marked this conversation as resolved.
Resolved
["description"] = "An ARGB color value, or nil for Color.Empty.",
};
}

Expand Down
17 changes: 17 additions & 0 deletions test/Nerdbank.MessagePack.TUnit/BuiltInConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,23 @@ public partial class BuiltInConverterTests : MessagePackSerializerTestBase
[Test]
public void SystemDrawingColor() => this.AssertRoundtrip<Color, Witness>(Color.FromArgb(1, 2, 3, 4));

[Test]
public void SystemDrawingColor_Empty()
{
ReadOnlySequence<byte> msgpack = this.AssertRoundtrip<Color, Witness>(Color.Empty);
Assert.Equal(MessagePackType.Nil, new MessagePackReader(msgpack).NextMessagePackType);
Assert.True(this.Serializer.Deserialize<Color, Witness>(msgpack, this.TimeoutToken).IsEmpty);
}

[Test]
public void SystemDrawingColor_TransparentBlack()
{
Color transparentBlack = Color.FromArgb(0);
ReadOnlySequence<byte> msgpack = this.AssertRoundtrip<Color, Witness>(transparentBlack);
Assert.Equal(MessagePackType.Integer, new MessagePackReader(msgpack).NextMessagePackType);
Assert.False(this.Serializer.Deserialize<Color, Witness>(msgpack, this.TimeoutToken).IsEmpty);
}

[Test]
public void SystemDrawingPoint() => this.AssertRoundtrip<Point, Witness>(new Point(1, 1));

Expand Down
Loading