-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from dxman/add-color-converter
feat: add Color converter
- Loading branch information
Showing
5 changed files
with
103 additions
and
2 deletions.
There are no files selected for viewing
4 changes: 2 additions & 2 deletions
4
Chickensoft.Serialization.Godot.Tests/badges/line_coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions
42
Chickensoft.Serialization.Godot.Tests/test/src/ColorConverterTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters