first commit
This commit is contained in:
139
Classes/JsonHelper.cs
Normal file
139
Classes/JsonHelper.cs
Normal file
@@ -0,0 +1,139 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace EFCDesk.Classes
|
||||
{
|
||||
public static class JsonHelper
|
||||
{
|
||||
//public static bool TryGetProperty(JsonElement element, string propertyName, out JsonElement value)
|
||||
//{
|
||||
// if (element.ValueKind == JsonValueKind.Object &&
|
||||
// element.TryGetProperty(propertyName, out value))
|
||||
// {
|
||||
// return true;
|
||||
// }
|
||||
|
||||
// value = default;
|
||||
// return false;
|
||||
//}
|
||||
|
||||
//public static string? GetString(JsonElement element, string propertyName)
|
||||
//{
|
||||
// return TryGetProperty(element, propertyName, out var value) && value.ValueKind == JsonValueKind.String
|
||||
// ? value.GetString()
|
||||
// : null;
|
||||
//}
|
||||
|
||||
//public static bool? GetBool(JsonElement element, string propertyName)
|
||||
//{
|
||||
// return TryGetProperty(element, propertyName, out var value) && value.ValueKind == JsonValueKind.True || value.ValueKind == JsonValueKind.False
|
||||
// ? value.GetBoolean()
|
||||
// : null;
|
||||
//}
|
||||
|
||||
//public static int? GetInt(JsonElement element, string propertyName)
|
||||
//{
|
||||
// return TryGetProperty(element, propertyName, out var value) && value.ValueKind == JsonValueKind.Number
|
||||
// ? value.GetInt32()
|
||||
// : null;
|
||||
//}
|
||||
|
||||
//public static JsonElement? GetArray(JsonElement element, string propertyName)
|
||||
//{
|
||||
// return TryGetProperty(element, propertyName, out var value) && value.ValueKind == JsonValueKind.Array
|
||||
// ? value
|
||||
// : null;
|
||||
//}
|
||||
|
||||
//public static JsonElement? GetObject(JsonElement element, string propertyName)
|
||||
//{
|
||||
// return TryGetProperty(element, propertyName, out var value) && value.ValueKind == JsonValueKind.Object
|
||||
// ? value
|
||||
// : null;
|
||||
//}
|
||||
|
||||
// ----------------------------
|
||||
// PUBLIC METHODS
|
||||
// ----------------------------
|
||||
|
||||
public static Dictionary<string, object?>? ToDictionary(JsonElement element)
|
||||
{
|
||||
if (element.ValueKind != JsonValueKind.Object)
|
||||
return null;
|
||||
|
||||
var dict = new Dictionary<string, object?>();
|
||||
|
||||
foreach (var prop in element.EnumerateObject())
|
||||
{
|
||||
dict[prop.Name] = ConvertElement(prop.Value);
|
||||
}
|
||||
|
||||
return dict;
|
||||
}
|
||||
|
||||
public static List<object?>? ToList(JsonElement element)
|
||||
{
|
||||
if (element.ValueKind != JsonValueKind.Array)
|
||||
return null;
|
||||
|
||||
var list = new List<object?>();
|
||||
|
||||
foreach (var item in element.EnumerateArray())
|
||||
{
|
||||
list.Add(ConvertElement(item));
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
public static object? Get(JsonElement element, string key)
|
||||
{
|
||||
return element.TryGetProperty(key, out var value)
|
||||
? ConvertElement(value)
|
||||
: null;
|
||||
}
|
||||
|
||||
// ----------------------------
|
||||
// INTERNAL CONVERTER
|
||||
// ----------------------------
|
||||
private static object? ConvertElement(JsonElement element)
|
||||
{
|
||||
return element.ValueKind switch
|
||||
{
|
||||
JsonValueKind.String => element.GetString(),
|
||||
JsonValueKind.Number => element.TryGetInt64(out long l) ? l : element.GetDouble(),
|
||||
JsonValueKind.True => true,
|
||||
JsonValueKind.False => false,
|
||||
JsonValueKind.Object => ToDictionary(element),
|
||||
JsonValueKind.Array => ToList(element),
|
||||
JsonValueKind.Null => null,
|
||||
JsonValueKind.Undefined => null,
|
||||
_ => element.ToString()
|
||||
};
|
||||
}
|
||||
|
||||
public static bool TryParse(string json, out JsonElement root)
|
||||
{
|
||||
root = default;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(json))
|
||||
return false;
|
||||
|
||||
try
|
||||
{
|
||||
var doc = JsonDocument.Parse(json);
|
||||
root = doc.RootElement.Clone(); // Clonamos para que no se deseche
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user