From 9636d5b3aac99459e5933bc4fa6ddb8ca84917af Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 20 Jan 2018 21:26:21 -0500 Subject: encapsulate common JSON converter code, improve parse errors (#423) --- .../Serialisation/SimpleReadOnlyConverter.cs | 77 ++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/SMAPI/Framework/Serialisation/SimpleReadOnlyConverter.cs (limited to 'src/SMAPI/Framework/Serialisation/SimpleReadOnlyConverter.cs') diff --git a/src/SMAPI/Framework/Serialisation/SimpleReadOnlyConverter.cs b/src/SMAPI/Framework/Serialisation/SimpleReadOnlyConverter.cs new file mode 100644 index 00000000..9308456b --- /dev/null +++ b/src/SMAPI/Framework/Serialisation/SimpleReadOnlyConverter.cs @@ -0,0 +1,77 @@ +using System; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using StardewModdingAPI.Framework.Exceptions; + +namespace StardewModdingAPI.Framework.Serialisation +{ + /// The base implementation for simplified converters which deserialise without overriding serialisation. + /// The type to deserialise. + internal abstract class SimpleReadOnlyConverter : JsonConverter + { + /********* + ** Accessors + *********/ + /// Whether this converter can write JSON. + public override bool CanWrite => false; + + + /********* + ** Public methods + *********/ + /// Get whether this instance can convert the specified object type. + /// The object type. + public override bool CanConvert(Type objectType) + { + return objectType == typeof(T); + } + + /// Writes the JSON representation of the object. + /// The JSON writer. + /// The value. + /// The calling serializer. + public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) + { + throw new InvalidOperationException("This converter does not write JSON."); + } + + /// Reads the JSON representation of the object. + /// The JSON reader. + /// The object type. + /// The object being read. + /// The calling serializer. + public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) + { + string path = reader.Path; + switch (reader.TokenType) + { + case JsonToken.StartObject: + return this.ReadObject(JObject.Load(reader), path); + case JsonToken.String: + return this.ReadString(JToken.Load(reader).Value(), path); + default: + throw new SParseException($"Can't parse {typeof(T).Name} from {reader.TokenType} (path: {reader.Path})."); + } + } + + + /********* + ** Protected methods + *********/ + /// Read a JSON object. + /// The JSON object to read. + /// The path to the current JSON node. + protected virtual T ReadObject(JObject obj, string path) + { + throw new SParseException($"Can't parse {typeof(T).Name} from object (path: {path})."); + } + + /// Read a JSON string. + /// The JSON string value. + /// The path to the current JSON node. + protected virtual T ReadString(string str, string path) + { + throw new SParseException($"Can't parse {typeof(T).Name} from string (path: {path})."); + } + } +} -- cgit From b10a4b410bb2d50245928bdb88a9efc05aadbe72 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 20 Jan 2018 22:18:11 -0500 Subject: tweak JSON error messages --- src/SMAPI/Framework/Serialisation/SimpleReadOnlyConverter.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/SMAPI/Framework/Serialisation/SimpleReadOnlyConverter.cs') diff --git a/src/SMAPI/Framework/Serialisation/SimpleReadOnlyConverter.cs b/src/SMAPI/Framework/Serialisation/SimpleReadOnlyConverter.cs index 9308456b..5765ad96 100644 --- a/src/SMAPI/Framework/Serialisation/SimpleReadOnlyConverter.cs +++ b/src/SMAPI/Framework/Serialisation/SimpleReadOnlyConverter.cs @@ -50,7 +50,7 @@ namespace StardewModdingAPI.Framework.Serialisation case JsonToken.String: return this.ReadString(JToken.Load(reader).Value(), path); default: - throw new SParseException($"Can't parse {typeof(T).Name} from {reader.TokenType} (path: {reader.Path})."); + throw new SParseException($"Can't parse {typeof(T).Name} from {reader.TokenType} node (path: {reader.Path})."); } } @@ -63,7 +63,7 @@ namespace StardewModdingAPI.Framework.Serialisation /// The path to the current JSON node. protected virtual T ReadObject(JObject obj, string path) { - throw new SParseException($"Can't parse {typeof(T).Name} from object (path: {path})."); + throw new SParseException($"Can't parse {typeof(T).Name} from object node (path: {path})."); } /// Read a JSON string. @@ -71,7 +71,7 @@ namespace StardewModdingAPI.Framework.Serialisation /// The path to the current JSON node. protected virtual T ReadString(string str, string path) { - throw new SParseException($"Can't parse {typeof(T).Name} from string (path: {path})."); + throw new SParseException($"Can't parse {typeof(T).Name} from string node (path: {path})."); } } } -- cgit