-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
18283d5
commit 39443d6
Showing
5 changed files
with
106 additions
and
110 deletions.
There are no files selected for viewing
40 changes: 0 additions & 40 deletions
40
src/CommandQuery.SystemTextJson/Internal/DictionaryExtensions.cs
This file was deleted.
Oops, something went wrong.
37 changes: 35 additions & 2 deletions
37
src/CommandQuery.SystemTextJson/Internal/JsonExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,52 @@ | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace CommandQuery.SystemTextJson | ||
{ | ||
internal static class JsonExtensions | ||
{ | ||
internal static object? SafeDeserialize(this string json, Type type, JsonSerializerOptions? options) | ||
private static readonly JsonSerializerOptions _options = GetJsonSerializerOptions(); | ||
|
||
internal static object? SafeDeserialize(this string json, Type type, JsonSerializerOptions? options = null) | ||
{ | ||
try | ||
{ | ||
return JsonSerializer.Deserialize(json, type, options ?? _options); | ||
} | ||
catch | ||
{ | ||
return null; | ||
} | ||
} | ||
|
||
internal static object? SafeDeserialize(this IDictionary<string, object>? dictionary, Type type) | ||
{ | ||
if (dictionary is null) | ||
{ | ||
return null; | ||
} | ||
|
||
try | ||
{ | ||
return JsonSerializer.Deserialize(json, type, options); | ||
return JsonSerializer.Deserialize(JsonSerializer.Serialize(dictionary), type, _options); | ||
} | ||
catch | ||
{ | ||
return null; | ||
} | ||
} | ||
|
||
private static JsonSerializerOptions GetJsonSerializerOptions() | ||
{ | ||
var result = new JsonSerializerOptions | ||
{ | ||
PropertyNameCaseInsensitive = true, | ||
NumberHandling = JsonNumberHandling.AllowReadingFromString, | ||
}; | ||
result.Converters.Add(new JsonStringEnumConverter()); | ||
result.Converters.Add(new BooleanConverter()); | ||
|
||
return result; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 0 additions & 60 deletions
60
tests/CommandQuery.Tests/SystemTextJson/Internal/DictionaryExtensionsTests.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters