PageRenderTime 54ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/Factories/JsonFactory.cs

https://gitlab.com/Ontology/OntoWebCore
C# | 327 lines | 227 code | 93 blank | 7 comment | 34 complexity | 63a98cd4ff7b5a6626647f6a8851d35a MD5 | raw file
  1. using Newtonsoft.Json;
  2. using OntologyAppDBConnector;
  3. using OntologyClasses.BaseClasses;
  4. using OntologyClasses.DataClasses;
  5. using OntoMsg_Module.Attributes;
  6. using OntoMsg_Module.Models;
  7. using OntoWebCore.Attributes;
  8. using System;
  9. using System.Collections;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Reflection;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. namespace OntoWebCore.Factories
  16. {
  17. public class JsonFactory
  18. {
  19. private clsLogStates logStates = new clsLogStates();
  20. private object entryItem;
  21. private string currentNameSpace;
  22. public clsOntologyItem CreateJsonFileOfItemList(Type itemType, List<object> itemList, SessionFile sessionFile)
  23. {
  24. var result = logStates.LogState_Success.Clone();
  25. var entryNamespace = itemType.Namespace;
  26. currentNameSpace = itemType.Namespace;
  27. var propAttributes = itemType.GetProperties().Cast<PropertyInfo>().Select(prop =>
  28. {
  29. var jsonAttribute = (JsonAttribute)prop.GetCustomAttribute(typeof(JsonAttribute));
  30. return new { Property = prop, JsonAttribute = jsonAttribute };
  31. }).ToList();
  32. if (!propAttributes.Any()) return logStates.LogState_Error.Clone();
  33. using (sessionFile.StreamWriter)
  34. {
  35. using (var jsonStream = new Newtonsoft.Json.JsonTextWriter(sessionFile.StreamWriter))
  36. {
  37. jsonStream.WriteStartArray();
  38. itemList.ForEach(item =>
  39. {
  40. jsonStream.WriteStartObject();
  41. propAttributes.ForEach(propAtt =>
  42. {
  43. bool isList = propAtt.Property.PropertyType.IsGenericType
  44. && propAtt.Property.PropertyType.GetGenericTypeDefinition() == typeof(IList<>);
  45. if (isList)
  46. {
  47. var value = propAtt.Property.GetValue(item);
  48. var collection = (IEnumerable)value;
  49. foreach (var colItem in collection)
  50. {
  51. result = WriteItemToJson(colItem, jsonStream, entryNamespace);
  52. }
  53. }
  54. else
  55. {
  56. var value = propAtt.Property.GetValue(item);
  57. if (value != null && propAtt.JsonAttribute != null)
  58. {
  59. var propertyName = propAtt.Property.Name;
  60. if (!string.IsNullOrEmpty(propAtt.JsonAttribute.Property))
  61. {
  62. propertyName = propAtt.JsonAttribute.Property;
  63. }
  64. if (propAtt.JsonAttribute.PropertyNameToLowercase)
  65. {
  66. propertyName = propertyName.ToLower();
  67. }
  68. if (propAtt.JsonAttribute.FirstCharLowercase && propertyName.Length > 0)
  69. {
  70. propertyName = propertyName.Substring(0, 1).ToLower() + propertyName.Substring(1);
  71. }
  72. jsonStream.WritePropertyName(propertyName);
  73. if (value.GetType().IsEnum)
  74. {
  75. var enumName = Enum.GetName(value.GetType(), value);
  76. var enumValue = (int)value;
  77. if (propAtt.JsonAttribute.EnumToString)
  78. {
  79. var propertyValue = enumName;
  80. if (propAtt.JsonAttribute.EnumStringToLowercase)
  81. {
  82. propertyValue = propertyValue.ToLower();
  83. }
  84. if (propAtt.JsonAttribute.EnumStringFirstCharToLowercase)
  85. {
  86. propertyValue = propertyValue.Substring(0, 1).ToLower() + propertyValue.Substring(1);
  87. }
  88. jsonStream.WriteValue(propertyValue);
  89. }
  90. else
  91. {
  92. jsonStream.WriteValue(enumValue);
  93. }
  94. }
  95. else
  96. {
  97. var valueNameSpace = value.GetType().Namespace;
  98. if (valueNameSpace.StartsWith(entryNamespace) ||
  99. valueNameSpace.StartsWith(currentNameSpace))
  100. {
  101. result = WriteItemToJson(value, jsonStream, entryNamespace);
  102. }
  103. else
  104. {
  105. jsonStream.WriteValue(value);
  106. }
  107. }
  108. }
  109. }
  110. });
  111. jsonStream.WriteEndObject();
  112. });
  113. jsonStream.WriteEndArray();
  114. }
  115. }
  116. return result;
  117. }
  118. public clsOntologyItem CreateJsonFileOfJsonItem(object item, SessionFile sessionFile)
  119. {
  120. var result = logStates.LogState_Success.Clone();
  121. entryItem = item;
  122. var entryNamespace = item.GetType().Namespace;
  123. try
  124. {
  125. using (sessionFile.StreamWriter)
  126. {
  127. using (var jsonWriter = new JsonTextWriter(sessionFile.StreamWriter))
  128. {
  129. result = WriteItemToJson(item, jsonWriter, entryNamespace);
  130. }
  131. }
  132. }
  133. catch (Exception ex)
  134. {
  135. result = logStates.LogState_Success.Clone();
  136. }
  137. return result;
  138. }
  139. public clsOntologyItem WriteItemToJson(object item, JsonTextWriter jsonTextWriter, string entryNamespace)
  140. {
  141. var result = logStates.LogState_Success.Clone();
  142. currentNameSpace = item.GetType().Namespace;
  143. //try
  144. //{
  145. var propAttributes = item.GetType().GetProperties().Cast<PropertyInfo>().Select(prop =>
  146. {
  147. var jsonAttribute = (JsonAttribute)prop.GetCustomAttribute(typeof(JsonAttribute));
  148. return new { Property = prop, JsonAttribute = jsonAttribute };
  149. }).ToList();
  150. if (!propAttributes.Any()) return logStates.LogState_Error.Clone();
  151. jsonTextWriter.WriteStartObject();
  152. propAttributes.ForEach(propAtt =>
  153. {
  154. bool isList = propAtt.Property.PropertyType.IsGenericType
  155. && propAtt.Property.PropertyType.GetGenericTypeDefinition() == typeof(IList<>);
  156. if (isList)
  157. {
  158. var value = propAtt.Property.GetValue(item);
  159. var collection = (IEnumerable)value;
  160. foreach (var colItem in collection)
  161. {
  162. result = WriteItemToJson(colItem, jsonTextWriter, entryNamespace);
  163. }
  164. }
  165. else
  166. {
  167. var value = propAtt.Property.GetValue(item);
  168. if (value != null && propAtt.JsonAttribute != null)
  169. {
  170. var propertyName = propAtt.Property.Name;
  171. if (!string.IsNullOrEmpty(propAtt.JsonAttribute.Property))
  172. {
  173. propertyName = propAtt.JsonAttribute.Property;
  174. }
  175. if (propAtt.JsonAttribute.PropertyNameToLowercase)
  176. {
  177. propertyName = propertyName.ToLower();
  178. }
  179. if (propAtt.JsonAttribute.FirstCharLowercase && propertyName.Length > 0)
  180. {
  181. propertyName = propertyName.Substring(0, 1).ToLower() + propertyName.Substring(1);
  182. }
  183. jsonTextWriter.WritePropertyName(propertyName);
  184. if (value.GetType().IsEnum)
  185. {
  186. var enumName = Enum.GetName(value.GetType(), value);
  187. var enumValue = (int)value;
  188. if (propAtt.JsonAttribute.EnumToString)
  189. {
  190. var propertyValue = enumName;
  191. if (propAtt.JsonAttribute.EnumStringToLowercase)
  192. {
  193. propertyValue = propertyValue.ToLower();
  194. }
  195. if (propAtt.JsonAttribute.EnumStringFirstCharToLowercase)
  196. {
  197. propertyValue = propertyValue.Substring(0, 1).ToLower() + propertyValue.Substring(1);
  198. }
  199. jsonTextWriter.WriteValue(propertyValue);
  200. }
  201. else
  202. {
  203. jsonTextWriter.WriteValue(enumValue);
  204. }
  205. }
  206. else
  207. {
  208. var valueNameSpace = value.GetType().Namespace;
  209. if (valueNameSpace.StartsWith(entryNamespace) ||
  210. valueNameSpace.StartsWith(currentNameSpace))
  211. {
  212. result = WriteItemToJson(value, jsonTextWriter, entryNamespace);
  213. }
  214. else
  215. {
  216. jsonTextWriter.WriteValue(value);
  217. }
  218. }
  219. }
  220. }
  221. });
  222. jsonTextWriter.WriteEndObject();
  223. //}
  224. //catch (Exception ex)
  225. //{
  226. // result = globals.LState_Error.Clone();
  227. //}
  228. return result;
  229. }
  230. public JsonFactory()
  231. {
  232. }
  233. }
  234. }