using System;
using Microsoft.Xna.Framework;
using Newtonsoft.Json.Linq;
using StardewModdingAPI.Toolkit.Serialization;
using StardewModdingAPI.Toolkit.Serialization.Converters;
namespace StardewModdingAPI.Framework.Serialization
{
/// Handles deserialization 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.ValueIgnoreCase(nameof(Point.X));
int y = obj.ValueIgnoreCase(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);
}
}
}