Fallback-Wert für JsonDeserialize, InvariantCulture
This commit is contained in:
@@ -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>
|
||||||
|
|||||||
Reference in New Issue
Block a user