-
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 #1 from devencire/feat/support-2d-int-types
- Loading branch information
Showing
10 changed files
with
475 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
Chickensoft.Serialization.Godot.Tests/test/src/Transform2DConverterTest.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,58 @@ | ||
namespace Chickensoft.Serialization.Godot.Tests; | ||
|
||
using Chickensoft.GoDotTest; | ||
using System.Text.Json; | ||
using global::Godot; | ||
using Shouldly; | ||
|
||
public class Transform2DConverterTest : TestClass { | ||
public Transform2DConverterTest(Node testScene) : base(testScene) { } | ||
|
||
[Test] | ||
public void CanConvert() { | ||
var converter = new Transform2DConverter(); | ||
converter.CanConvert(typeof(Transform2D)).ShouldBeTrue(); | ||
} | ||
|
||
[Test] | ||
public void Converts() { | ||
GodotSerialization.Setup(); | ||
|
||
var options = new JsonSerializerOptions() { | ||
WriteIndented = true, | ||
TypeInfoResolver = new SerializableTypeResolver(), | ||
}; | ||
|
||
var obj = new Transform2D( | ||
new Vector2(1, 2), | ||
new Vector2(3, 4), | ||
new Vector2(5, 6) | ||
); | ||
|
||
var json = JsonSerializer.Serialize(obj, options); | ||
|
||
json.ShouldBe( | ||
/*lang=json*/ | ||
""" | ||
{ | ||
"x": { | ||
"x": 1, | ||
"y": 2 | ||
}, | ||
"y": { | ||
"x": 3, | ||
"y": 4 | ||
}, | ||
"origin": { | ||
"x": 5, | ||
"y": 6 | ||
} | ||
} | ||
""" | ||
); | ||
|
||
var deserialized = JsonSerializer.Deserialize<Transform2D>(json, options); | ||
|
||
deserialized.ShouldBe(obj); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
Chickensoft.Serialization.Godot.Tests/test/src/Vector2ConverterTest.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,43 @@ | ||
namespace Chickensoft.Serialization.Godot.Tests; | ||
|
||
using Chickensoft.GoDotTest; | ||
using System.Text.Json; | ||
using global::Godot; | ||
using Shouldly; | ||
|
||
public class Vector2ConverterTest : TestClass { | ||
public Vector2ConverterTest(Node testScene) : base(testScene) { } | ||
|
||
[Test] | ||
public void CanConvert() { | ||
var converter = new Vector2Converter(); | ||
converter.CanConvert(typeof(Vector2)).ShouldBeTrue(); | ||
} | ||
|
||
[Test] | ||
public void Converts() { | ||
GodotSerialization.Setup(); | ||
|
||
var options = new JsonSerializerOptions() { | ||
WriteIndented = true, | ||
TypeInfoResolver = new SerializableTypeResolver(), | ||
}; | ||
|
||
var obj = new Vector2(1, 2); | ||
var json = JsonSerializer.Serialize(obj, options); | ||
|
||
json.ShouldBe( | ||
/*lang=json*/ | ||
""" | ||
{ | ||
"x": 1, | ||
"y": 2 | ||
} | ||
""" | ||
); | ||
|
||
var deserialized = JsonSerializer.Deserialize<Vector2>(json, options); | ||
|
||
deserialized.ShouldBe(obj); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
Chickensoft.Serialization.Godot.Tests/test/src/Vector2IConverterTest.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,55 @@ | ||
namespace Chickensoft.Serialization.Godot.Tests; | ||
|
||
using System; | ||
using Chickensoft.GoDotTest; | ||
using System.Text.Json; | ||
using global::Godot; | ||
using Shouldly; | ||
|
||
public class Vector2IConverterTest : TestClass { | ||
public Vector2IConverterTest(Node testScene) : base(testScene) { } | ||
|
||
[Test] | ||
public void CanConvert() { | ||
var converter = new Vector2IConverter(); | ||
converter.CanConvert(typeof(Vector2I)).ShouldBeTrue(); | ||
} | ||
|
||
[Test] | ||
public void Converts() { | ||
GodotSerialization.Setup(); | ||
|
||
var options = new JsonSerializerOptions() { | ||
WriteIndented = true, | ||
TypeInfoResolver = new SerializableTypeResolver(), | ||
}; | ||
|
||
var obj = new Vector2I(1, 2); | ||
var json = JsonSerializer.Serialize(obj, options); | ||
|
||
json.ShouldBe( | ||
/*lang=json*/ | ||
""" | ||
{ | ||
"x": 1, | ||
"y": 2 | ||
} | ||
""" | ||
); | ||
|
||
var deserialized = JsonSerializer.Deserialize<Vector2I>(json, options); | ||
|
||
deserialized.ShouldBe(obj); | ||
} | ||
|
||
[Test] | ||
public void ThrowsOnDecimals() { | ||
GodotSerialization.Setup(); | ||
|
||
const string json = """{"x": 1.2, "y": 2.3}"""; | ||
var options = new JsonSerializerOptions() { | ||
TypeInfoResolver = new SerializableTypeResolver(), | ||
}; | ||
Should.Throw<JsonException>(() => JsonSerializer.Deserialize<Vector2I>(json, options)); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
Chickensoft.Serialization.Godot.Tests/test/src/Vector3IConverterTest.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,55 @@ | ||
namespace Chickensoft.Serialization.Godot.Tests; | ||
|
||
using Chickensoft.GoDotTest; | ||
using System.Text.Json; | ||
using global::Godot; | ||
using Shouldly; | ||
|
||
public class Vector3IConverterTest : TestClass { | ||
public Vector3IConverterTest(Node testScene) : base(testScene) { } | ||
|
||
[Test] | ||
public void CanConvert() { | ||
var converter = new Vector3IConverter(); | ||
converter.CanConvert(typeof(Vector3I)).ShouldBeTrue(); | ||
} | ||
|
||
[Test] | ||
public void Converts() { | ||
GodotSerialization.Setup(); | ||
|
||
var options = new JsonSerializerOptions() { | ||
WriteIndented = true, | ||
TypeInfoResolver = new SerializableTypeResolver(), | ||
}; | ||
|
||
var obj = new Vector3I(1, 2, 3); | ||
var json = JsonSerializer.Serialize(obj, options); | ||
|
||
json.ShouldBe( | ||
/*lang=json*/ | ||
""" | ||
{ | ||
"x": 1, | ||
"y": 2, | ||
"z": 3 | ||
} | ||
""" | ||
); | ||
|
||
var deserialized = JsonSerializer.Deserialize<Vector3I>(json, options); | ||
|
||
deserialized.ShouldBe(obj); | ||
} | ||
|
||
[Test] | ||
public void ThrowsOnDecimals() { | ||
GodotSerialization.Setup(); | ||
|
||
const string json = """{"x": 1, "y": 2.3, "z": 3}"""; | ||
var options = new JsonSerializerOptions() { | ||
TypeInfoResolver = new SerializableTypeResolver(), | ||
}; | ||
Should.Throw<JsonException>(() => JsonSerializer.Deserialize<Vector3I>(json, options)); | ||
} | ||
} |
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
68 changes: 68 additions & 0 deletions
68
Chickensoft.Serialization.Godot/src/Transform2DConverter.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,68 @@ | ||
namespace Chickensoft.Serialization.Godot; | ||
|
||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using global::Godot; | ||
|
||
/// <summary>Transform2D JSON converter.</summary> | ||
public class Transform2DConverter : JsonConverter<Transform2D> { | ||
/// <inheritdoc /> | ||
public override bool CanConvert(Type typeToConvert) => | ||
typeToConvert == typeof(Transform2D); | ||
|
||
/// <inheritdoc /> | ||
public override Transform2D Read( | ||
ref Utf8JsonReader reader, | ||
Type typeToConvert, | ||
JsonSerializerOptions options | ||
) { | ||
var x = new Vector2(); | ||
var y = new Vector2(); | ||
var origin = new Vector2(); | ||
|
||
while (reader.Read()) { | ||
if (reader.TokenType == JsonTokenType.EndObject) { | ||
return new Transform2D(x, y, origin); | ||
} | ||
|
||
if (reader.TokenType != JsonTokenType.PropertyName) { | ||
continue; | ||
} | ||
|
||
var propertyName = reader.GetString(); | ||
reader.Read(); | ||
|
||
switch (propertyName) { | ||
case "x": | ||
x = JsonSerializer.Deserialize<Vector2>(ref reader, options); | ||
break; | ||
case "y": | ||
y = JsonSerializer.Deserialize<Vector2>(ref reader, options); | ||
break; | ||
case "origin": | ||
origin = JsonSerializer.Deserialize<Vector2>(ref reader, options); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
throw new JsonException("Unexpected end when reading Transform2D."); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override void Write(Utf8JsonWriter writer, Transform2D value, JsonSerializerOptions options) { | ||
var resolver = options.TypeInfoResolver; | ||
var vectorTypeInfo = resolver!.GetTypeInfo(typeof(Vector2), options)!; | ||
|
||
writer.WriteStartObject(); | ||
writer.WritePropertyName("x"); | ||
JsonSerializer.Serialize(writer, value.X, vectorTypeInfo); | ||
writer.WritePropertyName("y"); | ||
JsonSerializer.Serialize(writer, value.Y, vectorTypeInfo); | ||
writer.WritePropertyName("origin"); | ||
JsonSerializer.Serialize(writer, value.Origin, vectorTypeInfo); | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
namespace Chickensoft.Serialization.Godot; | ||
|
||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using global::Godot; | ||
|
||
/// <summary>Vector2 JSON converter.</summary> | ||
public class Vector2Converter : JsonConverter<Vector2> { | ||
/// <inheritdoc /> | ||
public override bool CanConvert(Type typeToConvert) => | ||
typeToConvert == typeof(Vector2); | ||
|
||
/// <inheritdoc /> | ||
public override Vector2 Read( | ||
ref Utf8JsonReader reader, | ||
Type typeToConvert, | ||
JsonSerializerOptions options | ||
) { | ||
var x = 0f; | ||
var y = 0f; | ||
|
||
while (reader.Read()) { | ||
if (reader.TokenType == JsonTokenType.EndObject) { | ||
return new Vector2(x, y); | ||
} | ||
|
||
if (reader.TokenType != JsonTokenType.PropertyName) { | ||
continue; | ||
} | ||
|
||
var propertyName = reader.GetString(); | ||
reader.Read(); | ||
|
||
switch (propertyName) { | ||
case "x": | ||
x = reader.GetSingle(); | ||
break; | ||
case "y": | ||
y = reader.GetSingle(); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
throw new JsonException("Unexpected end when reading Vector2."); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override void Write( | ||
Utf8JsonWriter writer, | ||
Vector2 value, | ||
JsonSerializerOptions options | ||
) { | ||
writer.WriteStartObject(); | ||
writer.WriteNumber("x", value.X); | ||
writer.WriteNumber("y", value.Y); | ||
writer.WriteEndObject(); | ||
} | ||
} |
Oops, something went wrong.