Binder.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO.Abstractions;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text.Json;
  7. using System.Text.Json.Serialization;
  8. namespace Quadarax.Foundation.Core.Json
  9. {
  10. public class Binder
  11. {
  12. private IFileSystem _fileSystem;
  13. public JsonSerializerOptions SerializerOptions {get;private set;}
  14. public Binder(JsonSerializerOptions options): this(new FileSystem(), options)
  15. {
  16. }
  17. public Binder(IFileSystem fileSystemAbstraction, JsonSerializerOptions options)
  18. {
  19. _fileSystem = fileSystemAbstraction ?? throw new ArgumentNullException(nameof(fileSystemAbstraction));
  20. SerializerOptions = options;
  21. }
  22. public Binder(IFileSystem fileSystemAbstraction, bool isPrettyPrint = true, bool isIgnoringNullValues = true, int maxDepth = 100)
  23. {
  24. _fileSystem = fileSystemAbstraction ?? throw new ArgumentNullException(nameof(fileSystemAbstraction));
  25. var jsonOpt = new JsonSerializerOptions
  26. {
  27. IgnoreNullValues = isIgnoringNullValues,
  28. WriteIndented = isPrettyPrint,
  29. PropertyNameCaseInsensitive = true,
  30. PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
  31. MaxDepth = maxDepth
  32. };
  33. jsonOpt.Converters.Add(new JsonStringEnumConverter());
  34. SerializerOptions = jsonOpt;
  35. }
  36. public TObject Load<TObject>(string fileName)
  37. {
  38. return (TObject) Load(fileName, typeof(TObject));
  39. }
  40. public object Load(string fileName, Type targetType)
  41. {
  42. if (targetType == null)
  43. throw new ArgumentNullException(nameof(targetType));
  44. if (string.IsNullOrEmpty(fileName))
  45. throw new ArgumentNullException(nameof(fileName));
  46. using (var reader = _fileSystem.File.OpenText(fileName))
  47. {
  48. var result = JsonSerializer.Deserialize(reader.ReadToEnd(),targetType, SerializerOptions);
  49. return result;
  50. }
  51. }
  52. public object LoadFromString(string jsonString, Type targetType)
  53. {
  54. if (targetType == null)
  55. throw new ArgumentNullException(nameof(targetType));
  56. if (string.IsNullOrEmpty(jsonString))
  57. throw new ArgumentNullException(nameof(jsonString));
  58. var result = JsonSerializer.Deserialize(jsonString,targetType, SerializerOptions);
  59. return result;
  60. }
  61. public void LoadTo(string fileName, object target, Type targetType)
  62. {
  63. if (targetType == null)
  64. throw new ArgumentNullException(nameof(targetType));
  65. if (target == null)
  66. throw new ArgumentNullException(nameof(target));
  67. var shadow = Load(fileName, targetType);
  68. foreach (var property in shadow.GetType().GetProperties())
  69. {
  70. if (target.GetType().GetProperties().Any(x => x.Name == property.Name && property.GetValue(shadow) != null))
  71. {
  72. if (property.GetType().IsClass && property.PropertyType.Assembly.FullName == targetType.Assembly.FullName)
  73. {
  74. var mapMethod = typeof(Binder).GetMethod("LoadTo");
  75. var genericMethod = mapMethod.MakeGenericMethod(property.GetValue(shadow).GetType());
  76. var obj2 = genericMethod.Invoke(null, new object[] { property.GetValue(shadow), JsonSerializer.Serialize(property.GetValue(shadow)) });
  77. foreach (var property2 in obj2.GetType().GetProperties())
  78. {
  79. if (property2.GetValue(obj2) != null)
  80. {
  81. property.GetValue(target).GetType().GetProperty(property2.Name).SetValue(property.GetValue(target), property2.GetValue(obj2));
  82. }
  83. }
  84. }
  85. else if (property.CanWrite)
  86. {
  87. property.SetValue(target, property.GetValue(shadow));
  88. }
  89. }
  90. }
  91. }
  92. public void LoadFromStringTo(string jsonString, object target, Type targetType)
  93. {
  94. if (targetType == null)
  95. throw new ArgumentNullException(nameof(targetType));
  96. if (target == null)
  97. throw new ArgumentNullException(nameof(target));
  98. var shadow = LoadFromString(jsonString, targetType);
  99. foreach (var property in shadow.GetType().GetProperties())
  100. {
  101. if (target.GetType().GetProperties().Any(x => x.Name == property.Name && property.GetValue(shadow) != null))
  102. {
  103. if (property.GetType().IsClass && property.PropertyType.Assembly.FullName == targetType.Assembly.FullName)
  104. {
  105. var mapMethod = typeof(Binder).GetMethod("LoadFromStringTo");
  106. var genericMethod = mapMethod.MakeGenericMethod(property.GetValue(shadow).GetType());
  107. var obj2 = genericMethod.Invoke(null, new object[] { property.GetValue(shadow), JsonSerializer.Serialize(property.GetValue(shadow)) });
  108. foreach (var property2 in obj2.GetType().GetProperties())
  109. {
  110. if (property2.GetValue(obj2) != null)
  111. {
  112. property.GetValue(target).GetType().GetProperty(property2.Name).SetValue(property.GetValue(target), property2.GetValue(obj2));
  113. }
  114. }
  115. }
  116. else if (property.CanWrite)
  117. {
  118. property.SetValue(target, property.GetValue(shadow));
  119. }
  120. }
  121. }
  122. }
  123. public void LoadTo<TObject>(string fileName, TObject target)
  124. {
  125. LoadTo(fileName, target, typeof(TObject));
  126. }
  127. public void Save<TObject>(string fileName, TObject bindingObject)
  128. {
  129. if (string.IsNullOrEmpty(fileName))
  130. throw new ArgumentNullException(nameof(fileName));
  131. if (bindingObject==null)
  132. throw new ArgumentNullException(nameof(bindingObject));
  133. var configuration = JsonSerializer.SerializeToUtf8Bytes(bindingObject,SerializerOptions);
  134. using (var writer = _fileSystem.File.Create(fileName))
  135. {
  136. writer.Write(configuration);
  137. writer.Flush();
  138. }
  139. }
  140. public string SaveToString<TObject>(TObject bindingObject)
  141. {
  142. if (bindingObject==null)
  143. throw new ArgumentNullException(nameof(bindingObject));
  144. return JsonSerializer.Serialize(bindingObject,bindingObject.GetType());
  145. }
  146. }
  147. }