File tree Expand file tree Collapse file tree 5 files changed +103
-2
lines changed
Chickensoft.Serialization.Godot.Tests
Chickensoft.Serialization.Godot/src Expand file tree Collapse file tree 5 files changed +103
-2
lines changed Original file line number Diff line number Diff line change 1+ namespace Chickensoft . Serialization . Godot . Tests ;
2+
3+ using Chickensoft . GoDotTest ;
4+ using System . Text . Json ;
5+ using global ::Godot ;
6+ using Shouldly ;
7+
8+ public class ColorConverterTest : TestClass {
9+ public ColorConverterTest ( Node testScene ) : base ( testScene ) { }
10+
11+ [ Test ]
12+ public void CanConvert ( ) {
13+ var converter = new ColorConverter ( ) ;
14+ converter . CanConvert ( typeof ( Color ) ) . ShouldBeTrue ( ) ;
15+ }
16+
17+ [ Test ]
18+ public void Converts ( ) {
19+ GodotSerialization . Setup ( ) ;
20+
21+ var options = new JsonSerializerOptions ( ) {
22+ WriteIndented = true ,
23+ TypeInfoResolver = new SerializableTypeResolver ( ) ,
24+ } ;
25+
26+ var obj = new Color ( 0xff0000ff ) ;
27+ var json = JsonSerializer . Serialize ( obj , options ) ;
28+
29+ json . ShouldBe (
30+ /*lang=json*/
31+ """
32+ {
33+ "rgba": "FF0000FF"
34+ }
35+ """
36+ ) ;
37+
38+ var deserialized = JsonSerializer . Deserialize < Color > ( json , options ) ;
39+
40+ deserialized . ShouldBe ( obj ) ;
41+ }
42+ }
Original file line number Diff line number Diff line change 1+ namespace Chickensoft . Serialization . Godot ;
2+
3+ using System ;
4+ using System . Globalization ;
5+ using System . Text . Json ;
6+ using System . Text . Json . Serialization ;
7+ using global ::Godot ;
8+
9+ /// <summary>Color JSON converter.</summary>
10+ public class ColorConverter : JsonConverter < Color > {
11+ /// <inheritdoc />
12+ public override bool CanConvert ( Type typeToConvert ) =>
13+ typeToConvert == typeof ( Color ) ;
14+
15+ /// <inheritdoc />
16+ public override Color Read (
17+ ref Utf8JsonReader reader ,
18+ Type typeToConvert ,
19+ JsonSerializerOptions options
20+ ) {
21+ var rgba = ( uint ) 0x000000ff ;
22+
23+ while ( reader . Read ( ) ) {
24+ if ( reader . TokenType == JsonTokenType . EndObject ) {
25+ return new Color ( rgba ) ;
26+ }
27+
28+ if ( reader . TokenType != JsonTokenType . PropertyName ) {
29+ continue ;
30+ }
31+
32+ var propertyName = reader . GetString ( ) ;
33+ reader . Read ( ) ;
34+
35+ switch ( propertyName ) {
36+ case "rgba" :
37+ rgba = uint . Parse ( reader . GetString ( ) ?? string . Empty , NumberStyles . HexNumber ) ;
38+ break ;
39+ default :
40+ break ;
41+ }
42+ }
43+
44+ throw new JsonException ( "Unexpected end when reading Color." ) ;
45+ }
46+
47+ /// <inheritdoc />
48+ public override void Write (
49+ Utf8JsonWriter writer ,
50+ Color value ,
51+ JsonSerializerOptions options
52+ ) {
53+ writer . WriteStartObject ( ) ;
54+ writer . WriteString ( "rgba" , value . ToRgba32 ( ) . ToString ( "X" ) ) ;
55+ writer . WriteEndObject ( ) ;
56+ }
57+ }
Original file line number Diff line number Diff line change @@ -18,5 +18,6 @@ public static void Setup() {
1818 Serializer . AddConverter ( new Vector3IConverter ( ) ) ;
1919 Serializer . AddConverter ( new BasisConverter ( ) ) ;
2020 Serializer . AddConverter ( new Transform3DConverter ( ) ) ;
21+ Serializer . AddConverter ( new ColorConverter ( ) ) ;
2122 }
2223}
Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ Please contribute! This only supports the following types:
1313- ✅ ` Vector3i `
1414- ✅ ` Basis `
1515- ✅ ` Transform3D `
16+ - ✅ ` Color `
1617
1718Be sure to place the following line somewhere before you start serializing/deserializing:
1819
You can’t perform that action at this time.
0 commit comments