diff options
| author | Jacob Palecki <[email protected]> | 2020-09-27 21:13:03 -0700 |
|---|---|---|
| committer | Jacob Palecki <[email protected]> | 2020-09-27 21:13:03 -0700 |
| commit | 5d8700a462b4798c02f4a73bc66d2a69a4920ae1 (patch) | |
| tree | 3a2b71991a6efce65be9af0c303ec2b59bdebff4 /grapher/Models/Serialized/RawAccelSettings.cs | |
| parent | Set tab order (diff) | |
| parent | Merge pull request #26 from a1xd/argcheck (diff) | |
| download | rawaccel-5d8700a462b4798c02f4a73bc66d2a69a4920ae1.tar.xz rawaccel-5d8700a462b4798c02f4a73bc66d2a69a4920ae1.zip | |
Merge and fix write button
Diffstat (limited to 'grapher/Models/Serialized/RawAccelSettings.cs')
| -rw-r--r-- | grapher/Models/Serialized/RawAccelSettings.cs | 27 |
1 files changed, 20 insertions, 7 deletions
diff --git a/grapher/Models/Serialized/RawAccelSettings.cs b/grapher/Models/Serialized/RawAccelSettings.cs index 3f5aebc..570a6c8 100644 --- a/grapher/Models/Serialized/RawAccelSettings.cs +++ b/grapher/Models/Serialized/RawAccelSettings.cs @@ -1,6 +1,8 @@ using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using System; using System.IO; +using System.Linq; namespace grapher.Models.Serialized { @@ -13,9 +15,8 @@ namespace grapher.Models.Serialized public static readonly string DefaultSettingsFile = Path.Combine(ExecutingDirectory, Constants.DefaultSettingsFileName); public static readonly JsonSerializerSettings SerializerSettings = new JsonSerializerSettings { - MissingMemberHandling = MissingMemberHandling.Error, + MissingMemberHandling = MissingMemberHandling.Ignore, }; - #endregion Fields #region Constructors @@ -33,9 +34,10 @@ namespace grapher.Models.Serialized #endregion Constructors #region Properties - + [JsonProperty(Required = Required.Always)] public GUISettings GUISettings { get; set; } + [JsonProperty(DriverSettings.Key, Required = Required.Always)] public DriverSettings AccelerationSettings { get; set; } #endregion Properties @@ -51,15 +53,17 @@ namespace grapher.Models.Serialized { try { - return JsonConvert.DeserializeObject<RawAccelSettings>(File.ReadAllText(file), SerializerSettings); + var settings = JsonConvert.DeserializeObject<RawAccelSettings>(File.ReadAllText(file), SerializerSettings); + if (settings is null) throw new JsonException($"{file} contains invalid JSON"); + return settings; } catch (FileNotFoundException e) { throw new FileNotFoundException($"Settings file does not exist at {file}", e); } - catch (JsonSerializationException e) + catch (JsonException e) { - throw new JsonSerializationException($"Settings file at {file} does not contain valid Raw Accel Settings.", e); + throw new JsonException($"Settings file at {file} does not contain valid Raw Accel Settings.", e); } } @@ -80,7 +84,16 @@ namespace grapher.Models.Serialized public void Save(string file) { - File.WriteAllText(file, JsonConvert.SerializeObject(this, Formatting.Indented)); + JObject thisJO = JObject.FromObject(this); + AddComments(thisJO); + File.WriteAllText(file, thisJO.ToString(Formatting.Indented)); + } + + private void AddComments(JObject thisJO) + { + string modes = string.Join(" | ", Enum.GetNames(typeof(AccelMode))); + ((JObject)thisJO[DriverSettings.Key]) + .AddFirst(new JProperty("### Mode Types ###", modes)); } #endregion Methods |