summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/Serialisation/PointConverter.cs
blob: fbc857d24711ed1c01e071ca7c6200e21635c2a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using System;
using Microsoft.Xna.Framework;
using Newtonsoft.Json.Linq;
using StardewModdingAPI.Toolkit.Serialisation;
using StardewModdingAPI.Toolkit.Serialisation.Converters;

namespace StardewModdingAPI.Framework.Serialisation
{
    /// <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.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);
        }
    }
}