|
@@ -1,72 +1,72 @@
|
|
|
-using System;
|
|
|
-using System.IO;
|
|
|
-using System.Text.Json;
|
|
|
-
|
|
|
-namespace WingNotificationModule.Adapter
|
|
|
-{
|
|
|
- /// <summary>
|
|
|
- /// This is a utilities class for json serializer and deserializer.
|
|
|
- /// </summary>
|
|
|
- public class JsonAdapter
|
|
|
- {
|
|
|
- /// <summary>
|
|
|
- /// Deserializer from json result.
|
|
|
- /// </summary>
|
|
|
- /// <typeparam name="T">Type of object.</typeparam>
|
|
|
- /// <param name="jsonResult">Json string</param>
|
|
|
- /// <returns>Instance of T.</returns>
|
|
|
- public static T DeserializerJson<T>(string jsonResult, JsonSerializerOptions jsonSerializerOptions = null) where T : class
|
|
|
- {
|
|
|
- return JsonSerializer.Deserialize(jsonResult, typeof(T), jsonSerializerOptions) as T;
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// Deserializer from object result.
|
|
|
- /// </summary>
|
|
|
- /// <typeparam name="T">Type of object.</typeparam>
|
|
|
- /// <param name="jsonResult">Json string</param>
|
|
|
- /// <returns>Instance of T.</returns>
|
|
|
- public static T DeserializerObject<T>(object jsonObj) where T : class
|
|
|
- {
|
|
|
- var jsonDoc = JsonDocument.Parse(SerializerToJson(jsonObj));
|
|
|
- return DeserializerJson<T>(jsonDoc.RootElement.GetRawText());
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// Serialize from instance to json string
|
|
|
- /// </summary>
|
|
|
- /// <typeparam name="T">Type of object.</typeparam>
|
|
|
- /// <param name="instance">Instance need to serialize.</param>
|
|
|
- /// <param name="useJsonFormatter"></param>
|
|
|
- /// <returns>The json string</returns>
|
|
|
- public static string SerializerToJson<T>(T instance, JsonSerializerOptions jsonSerializerOptions = null)
|
|
|
- {
|
|
|
- return JsonSerializer.Serialize(instance);
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// Deserializer json to object with json file
|
|
|
- /// </summary>
|
|
|
- /// <typeparam name="T">Convert type</typeparam>
|
|
|
- /// <param name="jsonFilePath">Json file path</param>
|
|
|
- /// <returns></returns>
|
|
|
- public static T DeserializerJsonFile<T>(string jsonFilePath) where T : class
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
- using (var stream = new FileStream(jsonFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
|
|
|
- {
|
|
|
- using (var sr = new StreamReader(stream))
|
|
|
- {
|
|
|
- return DeserializerJson<T>(sr.ReadToEnd());
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- catch (Exception exception)
|
|
|
- {
|
|
|
- Console.WriteLine($"JsonHelper_DeserializerJsonFile error:{exception}");
|
|
|
- return default(T);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+using System;
|
|
|
+using System.IO;
|
|
|
+using System.Text.Json;
|
|
|
+
|
|
|
+namespace WingNotificationModule.Adapter
|
|
|
+{
|
|
|
+ /// <summary>
|
|
|
+ /// This is a utilities class for json serializer and deserializer.
|
|
|
+ /// </summary>
|
|
|
+ public class JsonAdapter
|
|
|
+ {
|
|
|
+ /// <summary>
|
|
|
+ /// Deserializer from json result.
|
|
|
+ /// </summary>
|
|
|
+ /// <typeparam name="T">Type of object.</typeparam>
|
|
|
+ /// <param name="jsonResult">Json string</param>
|
|
|
+ /// <returns>Instance of T.</returns>
|
|
|
+ public static T DeserializerJson<T>(string jsonResult, JsonSerializerOptions jsonSerializerOptions = null) where T : class
|
|
|
+ {
|
|
|
+ return JsonSerializer.Deserialize(jsonResult, typeof(T), jsonSerializerOptions) as T;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Deserializer from object result.
|
|
|
+ /// </summary>
|
|
|
+ /// <typeparam name="T">Type of object.</typeparam>
|
|
|
+ /// <param name="jsonResult">Json string</param>
|
|
|
+ /// <returns>Instance of T.</returns>
|
|
|
+ public static T DeserializerObject<T>(object jsonObj) where T : class
|
|
|
+ {
|
|
|
+ var jsonDoc = JsonDocument.Parse(SerializerToJson(jsonObj));
|
|
|
+ return DeserializerJson<T>(jsonDoc.RootElement.GetRawText());
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Serialize from instance to json string
|
|
|
+ /// </summary>
|
|
|
+ /// <typeparam name="T">Type of object.</typeparam>
|
|
|
+ /// <param name="instance">Instance need to serialize.</param>
|
|
|
+ /// <param name="useJsonFormatter"></param>
|
|
|
+ /// <returns>The json string</returns>
|
|
|
+ public static string SerializerToJson<T>(T instance, JsonSerializerOptions jsonSerializerOptions = null)
|
|
|
+ {
|
|
|
+ return JsonSerializer.Serialize(instance);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Deserializer json to object with json file
|
|
|
+ /// </summary>
|
|
|
+ /// <typeparam name="T">Convert type</typeparam>
|
|
|
+ /// <param name="jsonFilePath">Json file path</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static T DeserializerJsonFile<T>(string jsonFilePath) where T : class
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ using (var stream = new FileStream(jsonFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
|
|
|
+ {
|
|
|
+ using (var sr = new StreamReader(stream))
|
|
|
+ {
|
|
|
+ return DeserializerJson<T>(sr.ReadToEnd());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception exception)
|
|
|
+ {
|
|
|
+ Console.WriteLine($"JsonHelper_DeserializerJsonFile error:{exception}");
|
|
|
+ return default(T);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|