88 lines
2.3 KiB
C#
88 lines
2.3 KiB
C#
/*
|
|
* Copyright (c) 2025 LG Electronics Inc.
|
|
* SPDX-License-Identifier: LicenseRef-LGE-Proprietary
|
|
*/
|
|
using Aurora.Api;
|
|
using Hermes;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using System;
|
|
using System.IO;
|
|
|
|
namespace VisionCommon {
|
|
public class JsonManager {
|
|
private static Logger _Logger = new Logger("JsonManager");
|
|
|
|
public static JToken ParseFromFile(string path) {
|
|
try {
|
|
if (File.Exists(path))
|
|
return JToken.Parse(File.ReadAllText(path));
|
|
}
|
|
catch (Exception e) {
|
|
_Logger.FuncE(e.Message);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public static T GetValue<T>(JToken jsonData, string key) {
|
|
JToken token = null;
|
|
|
|
try {
|
|
token = jsonData.SelectToken(key);
|
|
}
|
|
catch (Exception e) {
|
|
_Logger.FuncE(e.Message);
|
|
}
|
|
|
|
if (token == null) {
|
|
return default(T);
|
|
}
|
|
|
|
return token.ToObject<T>();
|
|
}
|
|
|
|
public static T GetValue<T>(string path, string key) {
|
|
if (!File.Exists(path)) {
|
|
_Logger.FuncE("Path exists not.");
|
|
return default(T);
|
|
}
|
|
|
|
JToken jsonData = null;
|
|
JToken token = null;
|
|
|
|
try {
|
|
jsonData = ParseFromFile(path);
|
|
token = jsonData?.SelectToken(key);
|
|
}
|
|
catch (Exception e) {
|
|
_Logger.FuncE(e.Message);
|
|
}
|
|
|
|
if (token == null) {
|
|
return default(T);
|
|
}
|
|
|
|
return token.ToObject<T>();
|
|
}
|
|
|
|
public static int SaveToFile(JToken token, string filePath) {
|
|
try {
|
|
using (var fs = File.Create(filePath))
|
|
using (var sw = new StreamWriter(fs))
|
|
using (var jtw = new JsonTextWriter(sw) {
|
|
Formatting = Formatting.Indented,
|
|
Indentation = 2,
|
|
}) {
|
|
(new JsonSerializer()).Serialize(jtw, token);
|
|
}
|
|
}
|
|
catch (Exception ex) {
|
|
_Logger.E("Exception SaveToFile FileIO : {0}", ex.Message);
|
|
return E1.GENERIC;
|
|
}
|
|
return E1.OK;
|
|
}
|
|
}
|
|
}
|