Skip to content

Commit

Permalink
Merge pull request #31 from dxman/add-color-converter
Browse files Browse the repository at this point in the history
feat: add Color converter
  • Loading branch information
jolexxa authored Nov 30, 2024
2 parents ee6a297 + a62532f commit 619853f
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 2 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
namespace Chickensoft.Serialization.Godot.Tests;

using Chickensoft.GoDotTest;
using System.Text.Json;
using global::Godot;
using Shouldly;

public class ColorConverterTest : TestClass {
public ColorConverterTest(Node testScene) : base(testScene) { }

[Test]
public void CanConvert() {
var converter = new ColorConverter();
converter.CanConvert(typeof(Color)).ShouldBeTrue();
}

[Test]
public void Converts() {
GodotSerialization.Setup();

var options = new JsonSerializerOptions() {
WriteIndented = true,
TypeInfoResolver = new SerializableTypeResolver(),
};

var obj = new Color(0xff0000ff);
var json = JsonSerializer.Serialize(obj, options);

json.ShouldBe(
/*lang=json*/
"""
{
"rgba": "FF0000FF"
}
"""
);

var deserialized = JsonSerializer.Deserialize<Color>(json, options);

deserialized.ShouldBe(obj);
}
}
57 changes: 57 additions & 0 deletions Chickensoft.Serialization.Godot/src/ColorConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
namespace Chickensoft.Serialization.Godot;

using System;
using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;
using global::Godot;

/// <summary>Color JSON converter.</summary>
public class ColorConverter : JsonConverter<Color> {
/// <inheritdoc />
public override bool CanConvert(Type typeToConvert) =>
typeToConvert == typeof(Color);

/// <inheritdoc />
public override Color Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options
) {
var rgba = (uint)0x000000ff;

while (reader.Read()) {
if (reader.TokenType == JsonTokenType.EndObject) {
return new Color(rgba);
}

if (reader.TokenType != JsonTokenType.PropertyName) {
continue;
}

var propertyName = reader.GetString();
reader.Read();

switch (propertyName) {
case "rgba":
rgba = uint.Parse(reader.GetString() ?? string.Empty, NumberStyles.HexNumber);
break;
default:
break;
}
}

throw new JsonException("Unexpected end when reading Color.");
}

/// <inheritdoc />
public override void Write(
Utf8JsonWriter writer,
Color value,
JsonSerializerOptions options
) {
writer.WriteStartObject();
writer.WriteString("rgba", value.ToRgba32().ToString("X"));
writer.WriteEndObject();
}
}
1 change: 1 addition & 0 deletions Chickensoft.Serialization.Godot/src/GodotSerialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ public static void Setup() {
Serializer.AddConverter(new Vector3IConverter());
Serializer.AddConverter(new BasisConverter());
Serializer.AddConverter(new Transform3DConverter());
Serializer.AddConverter(new ColorConverter());
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Please contribute! This only supports the following types:
-`Vector3i`
-`Basis`
-`Transform3D`
-`Color`

Be sure to place the following line somewhere before you start serializing/deserializing:

Expand Down

0 comments on commit 619853f

Please sign in to comment.