124 lines
4.5 KiB
C#
124 lines
4.5 KiB
C#
// Copyright (c) 2020 LG Electronics Inc.
|
|
// SPDX-License-Identifier: LicenseRef-LGE-Proprietary
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace CoreUnit.Common
|
|
{
|
|
public static class JsonExtensions
|
|
{
|
|
public static bool IsNullOrEmpty(this JToken token)
|
|
{
|
|
return (token == null) ||
|
|
(token.Type == JTokenType.Array && !token.HasValues) ||
|
|
(token.Type == JTokenType.Object && !token.HasValues) ||
|
|
(token.Type == JTokenType.String && token.ToString() == string.Empty) ||
|
|
(token.Type == JTokenType.Null);
|
|
}
|
|
public static void SetByPath(this JToken obj, string path, JToken value)
|
|
{
|
|
JToken token = obj.SelectToken(path);
|
|
token.Replace(value);
|
|
}
|
|
public static List<JToken> FindTokens(this JToken containerToken, string name)
|
|
{
|
|
List<JToken> matches = new List<JToken>();
|
|
FindTokens(containerToken, name, matches);
|
|
return matches;
|
|
}
|
|
public static JToken FirstOrDefault(this JToken containerToken, string name)
|
|
{
|
|
List<JToken> matches = new List<JToken>();
|
|
FindTokens(containerToken, name, matches);
|
|
return matches.FirstOrDefault();
|
|
}
|
|
public static string GetValue(this JToken containerToken, string name, string defVal)
|
|
{
|
|
JToken token = FirstOrDefault(containerToken, name);
|
|
if (token != null)
|
|
return token.Value<string>();
|
|
return defVal;
|
|
}
|
|
public static double GetValue(this JToken containerToken, string name, double defVal)
|
|
{
|
|
JToken token = FirstOrDefault(containerToken, name);
|
|
if (token != null)
|
|
return token.Value<double>();
|
|
return defVal;
|
|
}
|
|
public static int GetValue(this JToken containerToken, string name, int defVal)
|
|
{
|
|
JToken token = FirstOrDefault(containerToken, name);
|
|
if (token != null)
|
|
return token.Value<int>();
|
|
return defVal;
|
|
}
|
|
public static bool GetValue(this JToken containerToken, string name, bool defVal)
|
|
{
|
|
JToken token = FirstOrDefault(containerToken, name);
|
|
if (token != null)
|
|
return token.Value<bool>();
|
|
return defVal;
|
|
}
|
|
public static List<string> GetValueByPath(this JToken containerToken, string path)
|
|
{
|
|
var keyNames = path.Split('/');
|
|
var jKey = containerToken;
|
|
foreach (var keyName in keyNames) {
|
|
jKey = jKey[keyName];
|
|
if (jKey.IsNullOrEmpty())
|
|
return new List<string>();
|
|
}
|
|
if (!jKey.IsNullOrEmpty())
|
|
return jKey.Values<string>().ToList();
|
|
|
|
return new List<string>();
|
|
}
|
|
public static string GetValueByPath(this JToken containerToken, string path, string defVal)
|
|
{
|
|
var keyNames = path.Split('/');
|
|
var jKey = containerToken;
|
|
foreach (var keyName in keyNames) {
|
|
jKey = jKey[keyName];
|
|
if (jKey.IsNullOrEmpty())
|
|
return defVal;
|
|
}
|
|
if (!jKey.IsNullOrEmpty())
|
|
return jKey.Value<string>();
|
|
|
|
return defVal;
|
|
}
|
|
public static bool GetValueByPath(this JToken containerToken, string path, bool defVal)
|
|
{
|
|
var keyNames = path.Split('/');
|
|
var jKey = containerToken;
|
|
foreach (var keyName in keyNames) {
|
|
jKey = jKey[keyName];
|
|
if (jKey.IsNullOrEmpty())
|
|
return defVal;
|
|
}
|
|
if (!jKey.IsNullOrEmpty())
|
|
return jKey.Value<bool>();
|
|
|
|
return defVal;
|
|
}
|
|
private static void FindTokens(JToken containerToken, string name, List<JToken> matches)
|
|
{
|
|
if (containerToken.Type == JTokenType.Object) {
|
|
foreach (JProperty child in containerToken.Children<JProperty>()) {
|
|
if (child.Name == name) {
|
|
matches.Add(child.Value);
|
|
}
|
|
FindTokens(child.Value, name, matches);
|
|
}
|
|
} else if (containerToken.Type == JTokenType.Array) {
|
|
foreach (JToken child in containerToken.Children()) {
|
|
FindTokens(child, name, matches);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|