Skip to content

Commit

Permalink
Merge pull request #1 from devencire/feat/support-2d-int-types
Browse files Browse the repository at this point in the history
  • Loading branch information
jolexxa authored Jul 18, 2024
2 parents c2efc46 + 086155c commit 20e53bb
Show file tree
Hide file tree
Showing 10 changed files with 475 additions and 0 deletions.
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);
}
}
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);
}
}
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));
}
}
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));
}
}
4 changes: 4 additions & 0 deletions Chickensoft.Serialization.Godot/src/GodotSerialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ public static class GodotSerialization {
/// system.
/// </summary>
public static void Setup() {
Serializer.AddConverter(new Vector2Converter());
Serializer.AddConverter(new Vector2IConverter());
Serializer.AddConverter(new Transform2DConverter());
Serializer.AddConverter(new Vector3Converter());
Serializer.AddConverter(new Vector3IConverter());
Serializer.AddConverter(new BasisConverter());
Serializer.AddConverter(new Transform3DConverter());
}
Expand Down
68 changes: 68 additions & 0 deletions Chickensoft.Serialization.Godot/src/Transform2DConverter.cs
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();
}
}
61 changes: 61 additions & 0 deletions Chickensoft.Serialization.Godot/src/Vector2Converter.cs
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();
}
}
Loading

0 comments on commit 20e53bb

Please sign in to comment.