PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/PingORM.Redis/JsonSerializer/JsonSerializer.cs

https://github.com/MattyIce/PingORM
C# | 137 lines | 111 code | 15 blank | 11 comment | 10 complexity | 7289e5de0b1a0e0866fbd144464ee606 MD5 | raw file
  1. // --------------------------------
  2. // <copyright file="JsonSerializer.cs" company="Facebook C# SDK">
  3. // Microsoft Public License (Ms-PL)
  4. // </copyright>
  5. // <author>Nathan Totten (ntotten.com) and Jim Zimmerman (jimzimmerman.com)</author>
  6. // <license>Released under the terms of the Microsoft Public License (Ms-PL)</license>
  7. // <website>http://facebooksdk.codeplex.com</website>
  8. // ---------------------------------
  9. using System;
  10. using System.Linq;
  11. using System.Collections.Generic;
  12. using System.Diagnostics.Contracts;
  13. using System.IO;
  14. using System.Text.RegularExpressions;
  15. using System.Xml.Linq;
  16. using Newtonsoft.Json;
  17. using Newtonsoft.Json.Converters;
  18. using Newtonsoft.Json.Linq;
  19. using Newtonsoft.Json.Serialization;
  20. using System.Globalization;
  21. namespace PingORM.Redis.Json
  22. {
  23. public static class JsonSerializer
  24. {
  25. private static JsonSerializerSettings SerializerSettings
  26. {
  27. get
  28. {
  29. var isoDate = new IsoDateTimeConverter();
  30. isoDate.DateTimeFormat = "yyyy-MM-ddTHH:mm:sszzz";
  31. var settings = new JsonSerializerSettings();
  32. settings.Converters = settings.Converters ?? new List<JsonConverter>();
  33. settings.Converters.Add(isoDate);
  34. settings.MissingMemberHandling = MissingMemberHandling.Ignore;
  35. settings.NullValueHandling = NullValueHandling.Include;
  36. settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
  37. settings.TypeNameHandling = TypeNameHandling.None;
  38. settings.ConstructorHandling = ConstructorHandling.Default;
  39. return settings;
  40. }
  41. }
  42. public static string SerializeObject(object value)
  43. {
  44. return JsonConvert.SerializeObject(value, Formatting.None, SerializerSettings);
  45. }
  46. public static object DeserializeObject(Stream stream)
  47. {
  48. Contract.Requires(stream != null);
  49. object result;
  50. using (var reader = new StreamReader(stream))
  51. {
  52. string json = reader.ReadToEnd();
  53. result = DeserializeObject(json);
  54. }
  55. return result;
  56. }
  57. public static object DeserializeObject(string json)
  58. {
  59. return DeserializeObject(json, null);
  60. }
  61. public static object DeserializeObject(string json, Type type)
  62. {
  63. if (string.IsNullOrEmpty(json))
  64. {
  65. return null;
  66. }
  67. else
  68. {
  69. object obj;
  70. try
  71. {
  72. obj = JsonConvert.DeserializeObject(json, type, SerializerSettings);
  73. }
  74. catch (JsonSerializationException ex)
  75. {
  76. throw new System.Runtime.Serialization.SerializationException(ex.Message, ex);
  77. }
  78. // If the object is a JToken we want to
  79. // convert it to dynamic, it if is any
  80. // other type we just return it.
  81. var jToken = obj as JToken;
  82. if (jToken != null)
  83. {
  84. return ConvertJTokenToDictionary(jToken);
  85. }
  86. else
  87. {
  88. return obj;
  89. }
  90. }
  91. }
  92. private static object ConvertJTokenToDictionary(JToken token)
  93. {
  94. if (token == null)
  95. {
  96. return null;
  97. }
  98. var jValue = token as JValue;
  99. if (jValue != null)
  100. {
  101. return jValue.Value;
  102. }
  103. var jContainer = token as JArray;
  104. if (jContainer != null)
  105. {
  106. var jsonList = new JsonArray();
  107. foreach (JToken arrayItem in jContainer)
  108. {
  109. jsonList.Add(ConvertJTokenToDictionary(arrayItem));
  110. }
  111. return jsonList;
  112. }
  113. var jsonObject = new JsonObject();
  114. var jsonDict = (IDictionary<string, object>)jsonObject;
  115. (from childToken in token where childToken is JProperty select childToken as JProperty).ToList().ForEach(property =>
  116. {
  117. jsonDict.Add(property.Name, ConvertJTokenToDictionary(property.Value));
  118. });
  119. return jsonObject;
  120. }
  121. }
  122. }