summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/Serialization
diff options
context:
space:
mode:
Diffstat (limited to 'src/SMAPI/Framework/Serialization')
-rw-r--r--src/SMAPI/Framework/Serialization/PointConverter.cs2
-rw-r--r--src/SMAPI/Framework/Serialization/Vector2Converter.cs43
2 files changed, 44 insertions, 1 deletions
diff --git a/src/SMAPI/Framework/Serialization/PointConverter.cs b/src/SMAPI/Framework/Serialization/PointConverter.cs
index 8c2f3396..3481c9b2 100644
--- a/src/SMAPI/Framework/Serialization/PointConverter.cs
+++ b/src/SMAPI/Framework/Serialization/PointConverter.cs
@@ -6,7 +6,7 @@ using StardewModdingAPI.Toolkit.Serialization.Converters;
namespace StardewModdingAPI.Framework.Serialization
{
- /// <summary>Handles deserialization of <see cref="PointConverter"/> for crossplatform compatibility.</summary>
+ /// <summary>Handles deserialization of <see cref="Point"/> for crossplatform compatibility.</summary>
/// <remarks>
/// - Linux/Mac format: { "X": 1, "Y": 2 }
/// - Windows format: "1, 2"
diff --git a/src/SMAPI/Framework/Serialization/Vector2Converter.cs b/src/SMAPI/Framework/Serialization/Vector2Converter.cs
new file mode 100644
index 00000000..1d9b08e0
--- /dev/null
+++ b/src/SMAPI/Framework/Serialization/Vector2Converter.cs
@@ -0,0 +1,43 @@
+using System;
+using Microsoft.Xna.Framework;
+using Newtonsoft.Json.Linq;
+using StardewModdingAPI.Toolkit.Serialization;
+using StardewModdingAPI.Toolkit.Serialization.Converters;
+
+namespace StardewModdingAPI.Framework.Serialization
+{
+ /// <summary>Handles deserialization of <see cref="Vector2"/> for crossplatform compatibility.</summary>
+ /// <remarks>
+ /// - Linux/Mac format: { "X": 1, "Y": 2 }
+ /// - Windows format: "1, 2"
+ /// </remarks>
+ internal class Vector2Converter : SimpleReadOnlyConverter<Vector2>
+ {
+ /*********
+ ** Protected methods
+ *********/
+ /// <summary>Read a JSON object.</summary>
+ /// <param name="obj">The JSON object to read.</param>
+ /// <param name="path">The path to the current JSON node.</param>
+ protected override Vector2 ReadObject(JObject obj, string path)
+ {
+ float x = obj.ValueIgnoreCase<float>(nameof(Vector2.X));
+ float y = obj.ValueIgnoreCase<float>(nameof(Vector2.Y));
+ return new Vector2(x, y);
+ }
+
+ /// <summary>Read a JSON string.</summary>
+ /// <param name="str">The JSON string value.</param>
+ /// <param name="path">The path to the current JSON node.</param>
+ protected override Vector2 ReadString(string str, string path)
+ {
+ string[] parts = str.Split(',');
+ if (parts.Length != 2)
+ throw new SParseException($"Can't parse {typeof(Vector2).Name} from invalid value '{str}' (path: {path}).");
+
+ float x = Convert.ToSingle(parts[0]);
+ float y = Convert.ToSingle(parts[1]);
+ return new Vector2(x, y);
+ }
+ }
+}