diff options
| author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2018-01-24 11:44:28 -0500 |
|---|---|---|
| committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2018-01-24 11:44:28 -0500 |
| commit | 0da5dab8932c53ba39f2290268294e9a72b9c5bb (patch) | |
| tree | 9302a2a53a68202ac97e86662394a509df499541 /src/SMAPI/Framework/Serialisation/CrossplatformConverters/PointConverter.cs | |
| parent | 15d4b6310e3dd15c62f3faedbf1290b2db26fb59 (diff) | |
| parent | 5c96a10da5801049ee17ffa185dbf19e6d8a2306 (diff) | |
| download | SMAPI-0da5dab8932c53ba39f2290268294e9a72b9c5bb.tar.gz SMAPI-0da5dab8932c53ba39f2290268294e9a72b9c5bb.tar.bz2 SMAPI-0da5dab8932c53ba39f2290268294e9a72b9c5bb.zip | |
Merge branch 'develop' into stable
Diffstat (limited to 'src/SMAPI/Framework/Serialisation/CrossplatformConverters/PointConverter.cs')
| -rw-r--r-- | src/SMAPI/Framework/Serialisation/CrossplatformConverters/PointConverter.cs | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/SMAPI/Framework/Serialisation/CrossplatformConverters/PointConverter.cs b/src/SMAPI/Framework/Serialisation/CrossplatformConverters/PointConverter.cs new file mode 100644 index 00000000..84c70989 --- /dev/null +++ b/src/SMAPI/Framework/Serialisation/CrossplatformConverters/PointConverter.cs @@ -0,0 +1,42 @@ +using System; +using Microsoft.Xna.Framework; +using Newtonsoft.Json.Linq; +using StardewModdingAPI.Framework.Exceptions; + +namespace StardewModdingAPI.Framework.Serialisation.CrossplatformConverters +{ + /// <summary>Handles deserialisation 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.Value<int>(nameof(Point.X)); + int y = obj.Value<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); + } + } +} |
