1
0

Fallback-Wert für JsonDeserialize, InvariantCulture

This commit is contained in:
2021-11-12 23:59:45 +01:00
parent 6c4eb521a7
commit afda0b5471

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Globalization;
using System.IO; using System.IO;
using System.Text; using System.Text;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
@@ -19,7 +20,8 @@ namespace Newtonsoft.Json
/// </summary> /// </summary>
private static readonly JsonSerializerSettings jsonSerializerSettings = new() private static readonly JsonSerializerSettings jsonSerializerSettings = new()
{ {
ContractResolver = new CamelCasePropertyNamesContractResolver() ContractResolver = new CamelCasePropertyNamesContractResolver(),
Culture = CultureInfo.InvariantCulture
}; };
/// <summary> /// <summary>
@@ -74,13 +76,14 @@ namespace Newtonsoft.Json
/// </summary> /// </summary>
/// <typeparam name="T">The type of the instance to deserialize.</typeparam> /// <typeparam name="T">The type of the instance to deserialize.</typeparam>
/// <param name="json">The JSON string to read the values from.</param> /// <param name="json">The JSON string to read the values from.</param>
/// <param name="fallback">A fallback value.</param>
/// <returns>A new instance of <typeparamref name="T"/> with the deserialized values.</returns> /// <returns>A new instance of <typeparamref name="T"/> with the deserialized values.</returns>
public static T DeserializeJson<T>(this string json) public static T DeserializeJson<T>(this string json, T fallback = default)
{ {
if (!string.IsNullOrWhiteSpace(json)) if (!string.IsNullOrWhiteSpace(json))
return JsonConvert.DeserializeObject<T>(json, jsonSerializerSettings); return JsonConvert.DeserializeObject<T>(json, jsonSerializerSettings);
return default; return fallback;
} }
/// <summary> /// <summary>