-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Update json deserialization to use System.Text.Json * Update Readme and fix type of array object * Change namespace * Fix line endings
- Loading branch information
Showing
5 changed files
with
142 additions
and
8 deletions.
There are no files selected for viewing
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
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
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Text.Json; | ||
|
||
namespace Duo.Extensions | ||
{ | ||
internal static class JsonElementExtensions | ||
{ | ||
/// <summary> | ||
/// Converts a value contained within a System.Text.Json.JsonElement | ||
/// into an object of the contained type | ||
/// </summary> | ||
/// <param name="element"></param> | ||
/// <returns></returns> | ||
/// <exception cref="InvalidOperationException"></exception> | ||
internal static object ConvertToObject(this JsonElement element) | ||
{ | ||
switch (element.ValueKind) | ||
{ | ||
case JsonValueKind.Undefined: | ||
case JsonValueKind.Null: | ||
return null; | ||
case JsonValueKind.String: | ||
return element.GetString(); | ||
case JsonValueKind.Number: | ||
if (element.TryGetInt32(out int intValue)) | ||
{ | ||
return intValue; | ||
} | ||
if (element.TryGetInt64(out long longValue)) | ||
{ | ||
return longValue; | ||
} | ||
if (element.TryGetDecimal(out decimal decimalValue)) | ||
{ | ||
return decimalValue; | ||
} | ||
return element.GetDouble(); | ||
case JsonValueKind.True: | ||
case JsonValueKind.False: | ||
return element.GetBoolean(); | ||
case JsonValueKind.Object: | ||
var sourceDict = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(element.GetRawText()); | ||
var targetDict = new Dictionary<string, object>(); | ||
foreach (var kvp in sourceDict) | ||
{ | ||
targetDict.Add(kvp.Key, kvp.Value.ConvertToObject()); | ||
} | ||
return targetDict; | ||
case JsonValueKind.Array: | ||
// ArrayList was returned by the older serializer, so make sure to keep the type | ||
var list = new ArrayList(); | ||
foreach (var item in element.EnumerateArray()) | ||
{ | ||
list.Add(item.ConvertToObject()); | ||
} | ||
return list; | ||
default: | ||
throw new InvalidOperationException("Unexpected JSON value kind: " + element.ValueKind); | ||
} | ||
} | ||
} | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<packages> | ||
<package id="Microsoft.Bcl.AsyncInterfaces" version="6.0.0" targetFramework="net48" /> | ||
<package id="System.Buffers" version="4.5.1" targetFramework="net48" /> | ||
<package id="System.Memory" version="4.5.4" targetFramework="net48" /> | ||
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net48" /> | ||
<package id="System.Runtime.CompilerServices.Unsafe" version="6.0.0" targetFramework="net48" /> | ||
<package id="System.Text.Encodings.Web" version="6.0.0" targetFramework="net48" /> | ||
<package id="System.Text.Json" version="6.0.2" targetFramework="net48" /> | ||
<package id="System.Threading.Tasks.Extensions" version="4.5.4" targetFramework="net48" /> | ||
<package id="System.ValueTuple" version="4.5.0" targetFramework="net48" /> | ||
</packages> |