|
4 | 4 | // Created : 02-22-2020
|
5 | 5 | //
|
6 | 6 | // Last Modified By : Mario
|
7 |
| -// Last Modified On : 02-06-2025 |
| 7 | +// Last Modified On : 02-19-2025 |
8 | 8 | // ***********************************************************************
|
9 | 9 | // <copyright file="ModParser.cs" company="Mario">
|
10 | 10 | // Mario
|
@@ -88,7 +88,10 @@ public IModObject Parse(IEnumerable<string> lines, DescriptorModType descriptorM
|
88 | 88 | /// <returns>JsonSerializerSettings.</returns>
|
89 | 89 | private JsonSerializerSettings GetJsonSerializerSettings()
|
90 | 90 | {
|
91 |
| - jsonSerializerSettings ??= new JsonSerializerSettings { Error = (_, error) => error.ErrorContext.Handled = true, NullValueHandling = NullValueHandling.Ignore }; |
| 91 | + jsonSerializerSettings ??= new JsonSerializerSettings |
| 92 | + { |
| 93 | + Error = (_, error) => error.ErrorContext.Handled = true, NullValueHandling = NullValueHandling.Ignore, Converters = new List<JsonConverter> { new JsonMetaDataConverter() } |
| 94 | + }; |
92 | 95 | return jsonSerializerSettings;
|
93 | 96 | }
|
94 | 97 |
|
@@ -129,33 +132,7 @@ private IModObject ParseJsonMetadata(IEnumerable<string> lines)
|
129 | 132 | try
|
130 | 133 | {
|
131 | 134 | var json = string.Join(Environment.NewLine, lines);
|
132 |
| - JsonMetadataBase result = null; |
133 |
| - var ex = new List<Exception>(); |
134 |
| - try |
135 |
| - { |
136 |
| - result = JsonConvert.DeserializeObject<JsonMetadata>(json, GetJsonSerializerSettings()); |
137 |
| - } |
138 |
| - catch (Exception e) |
139 |
| - { |
140 |
| - ex.Add(e); |
141 |
| - } |
142 |
| - |
143 |
| - if (ex.Count > 0) |
144 |
| - { |
145 |
| - try |
146 |
| - { |
147 |
| - result = JsonConvert.DeserializeObject<JsonMetadataV2>(json, GetJsonSerializerSettings()); |
148 |
| - } |
149 |
| - catch (Exception e) |
150 |
| - { |
151 |
| - ex.Add(e); |
152 |
| - } |
153 |
| - } |
154 |
| - |
155 |
| - if (ex.Count >= 2) |
156 |
| - { |
157 |
| - throw new AggregateException(ex); |
158 |
| - } |
| 135 | + var result = JsonConvert.DeserializeObject<JsonMetadataBase>(json, GetJsonSerializerSettings()); |
159 | 136 |
|
160 | 137 | if (result!.GameCustomData != null)
|
161 | 138 | {
|
@@ -327,6 +304,73 @@ private abstract class JsonMetadataBase
|
327 | 304 | #endregion Properties
|
328 | 305 | }
|
329 | 306 |
|
| 307 | + /// <summary> |
| 308 | + /// Class JsonMetaDataConverter. |
| 309 | + /// Implements the <see cref="Newtonsoft.Json.JsonConverter{IronyModManager.Parser.Mod.ModParser.JsonMetadataBase}" /> |
| 310 | + /// </summary> |
| 311 | + /// <seealso cref="Newtonsoft.Json.JsonConverter{IronyModManager.Parser.Mod.ModParser.JsonMetadataBase}" /> |
| 312 | + private class JsonMetaDataConverter : JsonConverter<JsonMetadataBase> |
| 313 | + { |
| 314 | + #region Methods |
| 315 | + |
| 316 | + /// <summary> |
| 317 | + /// Reads the JSON representation of the object. |
| 318 | + /// </summary> |
| 319 | + /// <param name="reader">The <see cref="T:Newtonsoft.Json.JsonReader" /> to read from.</param> |
| 320 | + /// <param name="objectType">Type of the object.</param> |
| 321 | + /// <param name="existingValue">The existing value of object being read. If there is no existing value then <c>null</c> will be used.</param> |
| 322 | + /// <param name="hasExistingValue">The existing value has a value.</param> |
| 323 | + /// <param name="serializer">The calling serializer.</param> |
| 324 | + /// <returns>The object value.</returns> |
| 325 | + public override JsonMetadataBase ReadJson(JsonReader reader, Type objectType, JsonMetadataBase existingValue, bool hasExistingValue, JsonSerializer serializer) |
| 326 | + { |
| 327 | + var obj = JObject.Load(reader); |
| 328 | + var relationshipToken = obj["relationships"]; |
| 329 | + JsonMetadataBase result = null; |
| 330 | + if (relationshipToken != null) |
| 331 | + { |
| 332 | + result = relationshipToken.Type switch |
| 333 | + { |
| 334 | + JTokenType.Array => CheckArrayType(relationshipToken as JArray), |
| 335 | + _ => new JsonMetadataV2() |
| 336 | + }; |
| 337 | + } |
| 338 | + |
| 339 | + result ??= new JsonMetadataV2(); |
| 340 | + serializer.Populate(obj.CreateReader(), result); |
| 341 | + return result; |
| 342 | + } |
| 343 | + |
| 344 | + /// <summary> |
| 345 | + /// Writes the JSON representation of the object. |
| 346 | + /// </summary> |
| 347 | + /// <param name="writer">The <see cref="T:Newtonsoft.Json.JsonWriter" /> to write to.</param> |
| 348 | + /// <param name="value">The value.</param> |
| 349 | + /// <param name="serializer">The calling serializer.</param> |
| 350 | + /// <exception cref="System.NotSupportedException"></exception> |
| 351 | + public override void WriteJson(JsonWriter writer, JsonMetadataBase value, JsonSerializer serializer) |
| 352 | + { |
| 353 | + throw new NotSupportedException(); |
| 354 | + } |
| 355 | + |
| 356 | + /// <summary> |
| 357 | + /// Checks the type of the array. |
| 358 | + /// </summary> |
| 359 | + /// <param name="token">The token.</param> |
| 360 | + /// <returns>JsonMetadataBase.</returns> |
| 361 | + private JsonMetadataBase CheckArrayType(JToken token) |
| 362 | + { |
| 363 | + if (token.All(p => p.Type == JTokenType.String)) |
| 364 | + { |
| 365 | + return new JsonMetadata(); |
| 366 | + } |
| 367 | + |
| 368 | + return new JsonMetadataV2(); |
| 369 | + } |
| 370 | + |
| 371 | + #endregion Methods |
| 372 | + } |
| 373 | + |
330 | 374 | /// <summary>
|
331 | 375 | /// Class JsonMetadataV2.
|
332 | 376 | /// </summary>
|
|
0 commit comments