Skip to content
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
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 the boolean `false` for `Color.Empty`.
- @System.Globalization.CultureInfo
- @System.Text.Encoding
- @System.EventArgs
Expand Down
32 changes: 27 additions & 5 deletions src/Nerdbank.MessagePack/Converters/BclConverters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,39 @@ namespace Nerdbank.MessagePack.Converters;
internal class SystemDrawingColorConverter : MessagePackConverter<Color>
{
public override Color Read(ref MessagePackReader reader, SerializationContext context)
=> Color.FromArgb(reader.ReadInt32());
{
if (reader.NextMessagePackType == MessagePackType.Boolean)
{
if (!reader.ReadBoolean())
{
return Color.Empty;
}

throw new MessagePackSerializationException("Unexpected true value while deserializing System.Drawing.Color.");
}

return Color.FromArgb(reader.ReadInt32());
}

public override void Write(ref MessagePackWriter writer, in Color value, SerializationContext context)
=> writer.Write(value.ToArgb());
{
if (value.IsEmpty)
{
writer.Write(false);
}
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"] = "boolean", ["enum"] = new JsonArray(false) },
new JsonObject { ["type"] = "integer", ["format"] = "int32" }),
Comment thread
AArnott marked this conversation as resolved.
["description"] = "An ARGB color value, or false for Color.Empty.",
};
}

Expand Down
5 changes: 3 additions & 2 deletions src/Nerdbank.MessagePack/MessagePackConverter`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,11 @@ protected internal static JsonObject ApplyJsonSchemaNullability(JsonObject schem
}
else
{
// This is probably a schema reference.
// This is probably a schema reference or a composition that may already allow null.
string compositionKeyword = schema.ContainsKey("oneOf") || schema.ContainsKey("anyOf") || schema.ContainsKey("allOf") ? "anyOf" : "oneOf";
schema = new()
{
["oneOf"] = new JsonArray(schema, new JsonObject { ["type"] = "null" }),
[compositionKeyword] = new JsonArray(schema, new JsonObject { ["type"] = "null" }),
};
}

Expand Down
35 changes: 35 additions & 0 deletions test/Nerdbank.MessagePack.TUnit/BuiltInConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,40 @@ 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.Boolean, 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 NullableSystemDrawingColor_Null()
{
ReadOnlySequence<byte> msgpack = this.AssertRoundtrip<Color?, Witness>(null);
Assert.Equal(MessagePackType.Nil, new MessagePackReader(msgpack).NextMessagePackType);
Assert.Null(this.Serializer.Deserialize<Color?, Witness>(msgpack, this.TimeoutToken));
}

[Test]
public void NullableSystemDrawingColor_Empty()
{
ReadOnlySequence<byte> msgpack = this.AssertRoundtrip<Color?, Witness>(Color.Empty);
Color? result = this.Serializer.Deserialize<Color?, Witness>(msgpack, this.TimeoutToken);
Assert.True(result.HasValue);
Assert.True(result.Value.IsEmpty);
}

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

Expand Down Expand Up @@ -596,6 +630,7 @@ public partial record HasDateTimeOffset(DateTimeOffset Value);
[GenerateShapeFor<Guid>]
[GenerateShapeFor<Point>]
[GenerateShapeFor<Color>]
[GenerateShapeFor<Color?>]
[GenerateShapeFor<byte[]>]
[GenerateShapeFor<CultureInfo>]
[GenerateShapeFor<EventArgs>]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"oneOf": [
{
"$ref": "#/$defs/SchemaTests\u002BHasNullableColors"
},
{
"type": "null"
}
],
"$defs": {
"System.Drawing.Color": {
"anyOf": [
{
"type": "boolean",
"enum": [
false
]
},
{
"type": "integer",
"format": "int32"
}
],
"description": "An ARGB color value, or false for Color.Empty."
},
"System.Nullable\u00601[System.Drawing.Color][]": {
"type": "array",
"items": {
"oneOf": [
{
"$ref": "#/$defs/System.Drawing.Color"
},
{
"type": "null"
}
]
}
},
"SchemaTests\u002BHasNullableColors": {
"type": "object",
"properties": {
"Color": {
"anyOf": [
{
"oneOf": [
{
"$ref": "#/$defs/System.Drawing.Color"
},
{
"type": "null"
}
]
},
{
"type": "null"
}
]
},
"Colors": {
"$ref": "#/$defs/System.Nullable%601%5BSystem.Drawing.Color%5D%5B%5D"
}
},
"required": [
"Color",
"Colors"
]
}
},
"$schema": "https://json-schema.org/draft/2020-12/schema"
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"oneOf": [
"anyOf": [
{
"oneOf": [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"oneOf": [
"anyOf": [
{
"oneOf": [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"oneOf": [
{
"$ref": "#/definitions/SchemaTests\u002BHasNullableColors"
},
{
"type": "null"
}
],
"definitions": {
"System.Drawing.Color": {
"anyOf": [
{
"type": "boolean",
"enum": [
false
]
},
{
"type": "integer",
"format": "int32"
}
],
"description": "An ARGB color value, or false for Color.Empty."
},
"System.Nullable\u00601[System.Drawing.Color][]": {
"type": "array",
"items": {
"oneOf": [
{
"$ref": "#/definitions/System.Drawing.Color"
},
{
"type": "null"
}
]
}
},
"SchemaTests\u002BHasNullableColors": {
"type": "object",
"properties": {
"Color": {
"anyOf": [
{
"oneOf": [
{
"$ref": "#/definitions/System.Drawing.Color"
},
{
"type": "null"
}
]
},
{
"type": "null"
}
]
},
"Colors": {
"$ref": "#/definitions/System.Nullable%601%5BSystem.Drawing.Color%5D%5B%5D"
}
},
"required": [
"Color",
"Colors"
]
}
},
"$schema": "http://json-schema.org/draft-04/schema#"
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"oneOf": [
"anyOf": [
{
"oneOf": [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"oneOf": [
"anyOf": [
{
"oneOf": [
{
Expand Down
11 changes: 11 additions & 0 deletions test/Nerdbank.MessagePack.TUnit/SchemaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#endif

using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Reflection;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -42,6 +43,13 @@ internal enum Sex
Female,
}

[Test, MatrixDataSource]
public void NullableColorSchema(JsonSchemaDialect dialect)
{
JSchema schema = this.AssertSchema(dialect, [new HasNullableColors(null, [null, Color.Empty, Color.FromArgb(0)])]);
Assert.False(JToken.Parse("""{"Color":true,"Colors":[]}""").IsValid(schema));
}

[Test, MatrixDataSource]
public void BasicObject_Map(JsonSchemaDialect dialect) => this.AssertSchema(dialect, [new BasicObject { IntProperty = 3, StringProperty = "hi" }]);

Expand Down Expand Up @@ -514,4 +522,7 @@ public override void Write(ref MessagePackWriter writer, in CustomType? value, S
writer.WriteMapHeader(0);
}
}

[GenerateShape]
public partial record HasNullableColors(Color? Color, Color?[] Colors);
}
Loading