From 894fd25a18cd5ddf31a905860aca95c438894efd Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 20 Jan 2018 21:29:47 -0500 Subject: move converters into namespace --- .../Serialisation/Converters/PointConverter.cs | 42 ++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/SMAPI/Framework/Serialisation/Converters/PointConverter.cs (limited to 'src/SMAPI/Framework/Serialisation/Converters/PointConverter.cs') diff --git a/src/SMAPI/Framework/Serialisation/Converters/PointConverter.cs b/src/SMAPI/Framework/Serialisation/Converters/PointConverter.cs new file mode 100644 index 00000000..87d0ff34 --- /dev/null +++ b/src/SMAPI/Framework/Serialisation/Converters/PointConverter.cs @@ -0,0 +1,42 @@ +using System; +using Microsoft.Xna.Framework; +using Newtonsoft.Json.Linq; +using StardewModdingAPI.Framework.Exceptions; + +namespace StardewModdingAPI.Framework.Serialisation.Converters +{ + /// Handles deserialisation of for crossplatform compatibility. + /// + /// - Linux/Mac format: { "X": 1, "Y": 2 } + /// - Windows format: "1, 2" + /// + internal class PointConverter : SimpleReadOnlyConverter + { + /********* + ** Protected methods + *********/ + /// Read a JSON object. + /// The JSON object to read. + /// The path to the current JSON node. + protected override Point ReadObject(JObject obj, string path) + { + int x = obj.Value(nameof(Point.X)); + int y = obj.Value(nameof(Point.Y)); + return new Point(x, y); + } + + /// Read a JSON string. + /// The JSON string value. + /// The path to the current JSON node. + protected override Point ReadString(string str, string path) + { + string[] parts = str.Split(','); + if (parts.Length != 2) + throw new SParseException($"Can't parse {typeof(Point).Name} from invalid value '{str}' (path: {path})."); + + int x = Convert.ToInt32(parts[0]); + int y = Convert.ToInt32(parts[1]); + return new Point(x, y); + } + } +} -- cgit