Fallback-Wert für JsonDeserialize, InvariantCulture
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json.Linq;
|
||||
@@ -19,7 +20,8 @@ namespace Newtonsoft.Json
|
||||
/// </summary>
|
||||
private static readonly JsonSerializerSettings jsonSerializerSettings = new()
|
||||
{
|
||||
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
||||
ContractResolver = new CamelCasePropertyNamesContractResolver(),
|
||||
Culture = CultureInfo.InvariantCulture
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
@@ -74,13 +76,14 @@ namespace Newtonsoft.Json
|
||||
/// </summary>
|
||||
/// <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="fallback">A fallback value.</param>
|
||||
/// <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))
|
||||
return JsonConvert.DeserializeObject<T>(json, jsonSerializerSettings);
|
||||
|
||||
return default;
|
||||
return fallback;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user