-
-
Notifications
You must be signed in to change notification settings - Fork 318
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 #833 from craftworkgames/rectanglef_serializer
Rectanglef serializer
- Loading branch information
Showing
3 changed files
with
59 additions
and
1 deletion.
There are no files selected for viewing
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
24 changes: 24 additions & 0 deletions
24
src/cs/MonoGame.Extended/Serialization/RectangleFJsonConverter.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,24 @@ | ||
using System; | ||
using Newtonsoft.Json; | ||
|
||
namespace MonoGame.Extended.Serialization; | ||
|
||
public class RectangleFJsonConverter: JsonConverter | ||
{ | ||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | ||
{ | ||
var rect = (RectangleF)value; | ||
writer.WriteValue($"{rect.Left} {rect.Top} {rect.Width} {rect.Height}"); | ||
} | ||
|
||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||
{ | ||
var values = reader.ReadAsMultiDimensional<float>(); | ||
return new RectangleF(values[0], values[1], values[2], values[3]); | ||
} | ||
|
||
public override bool CanConvert(Type objectType) | ||
{ | ||
return objectType == typeof(RectangleF); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/cs/Tests/MonoGame.Extended.Tests/Serialization/RectangleFJsonConverterTest.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,33 @@ | ||
using System.IO; | ||
using MonoGame.Extended.Serialization; | ||
using Newtonsoft.Json; | ||
using Xunit; | ||
|
||
namespace MonoGame.Extended.Tests.Serialization; | ||
|
||
public class RectangleFJsonConverterTest | ||
{ | ||
|
||
public class TestContent | ||
{ | ||
public RectangleF Box { get; set; } | ||
} | ||
|
||
[Fact] | ||
public void ConstructorTest() | ||
{ | ||
var jsonData = @" | ||
{ | ||
box: ""1 1 10 10"" | ||
} | ||
"; | ||
var serializer = new JsonSerializer(); | ||
serializer.Converters.Add(new RectangleFJsonConverter()); | ||
var content = serializer.Deserialize<TestContent>(new JsonTextReader(new StringReader(jsonData))); | ||
|
||
Assert.Equal(1, content.Box.Left); | ||
Assert.Equal(1, content.Box.Top); | ||
Assert.Equal(10, content.Box.Width); | ||
Assert.Equal(10, content.Box.Height); | ||
} | ||
} |