diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2019-08-09 01:18:05 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2019-09-14 19:08:06 -0400 |
commit | fd77ae93d59222d70c86aebfc044f3af11063372 (patch) | |
tree | 76fac6ffc0cac70fb44f8b9ffc72471a2796a02f /src/SMAPI/Framework/Serialization/PointConverter.cs | |
parent | 3f6865e8301535c8fbe83bc0f931a116adac0636 (diff) | |
download | SMAPI-fd77ae93d59222d70c86aebfc044f3af11063372.tar.gz SMAPI-fd77ae93d59222d70c86aebfc044f3af11063372.tar.bz2 SMAPI-fd77ae93d59222d70c86aebfc044f3af11063372.zip |
fix typos and inconsistent spelling
Diffstat (limited to 'src/SMAPI/Framework/Serialization/PointConverter.cs')
-rw-r--r-- | src/SMAPI/Framework/Serialization/PointConverter.cs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/SMAPI/Framework/Serialization/PointConverter.cs b/src/SMAPI/Framework/Serialization/PointConverter.cs new file mode 100644 index 00000000..8c2f3396 --- /dev/null +++ b/src/SMAPI/Framework/Serialization/PointConverter.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="PointConverter"/> for crossplatform compatibility.</summary> + /// <remarks> + /// - Linux/Mac format: { "X": 1, "Y": 2 } + /// - Windows format: "1, 2" + /// </remarks> + internal class PointConverter : SimpleReadOnlyConverter<Point> + { + /********* + ** 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 Point ReadObject(JObject obj, string path) + { + int x = obj.ValueIgnoreCase<int>(nameof(Point.X)); + int y = obj.ValueIgnoreCase<int>(nameof(Point.Y)); + return new Point(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 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); + } + } +} |