Guest User

Untitled

a guest
Feb 13th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using Newtonsoft.Json;
  4. using Newtonsoft.Json.Linq;
  5.  
  6. namespace MacUistin.Serialization
  7. {
  8. // I wrote this to help when creating new JsonConverter
  9. public static class JsonSerializationHelper
  10. {
  11. #region Retrieve values
  12.  
  13. /*
  14. Can simplify the code for retreiving and defaulting values:
  15.  
  16. public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
  17. {
  18. if (reader.TokenType == JsonToken.StartObject)
  19. {
  20. JObject item = JObject.Load(reader);
  21.  
  22. if (item["Items"] == null)
  23. return null;
  24.  
  25. var items = item.GetValueOrDefault<MyCustomList>("Items", serializer);
  26. string listField1 = item.GetValueOrDefault("ListField1", "xyz");
  27. int listField2 = item.GetValueOrDefault("ListField2", 123);
  28.  
  29. return new MyCustomList(items, listField1, listField2);
  30. }
  31.  
  32. throw new JsonSerializationException($"Unexpected format deserializing MyCustomList. Token type: {reader.TokenType}");
  33. }
  34. */
  35.  
  36. public static T GetValueOrDefault<T>(this JObject jObject, string key, JsonSerializer serializer, T defaultValue = null) where T : class
  37. {
  38. var value = jObject[key];
  39.  
  40. if (value == null)
  41. return defaultValue ?? new T();
  42.  
  43. try
  44. {
  45. return value.ToObject<T>();
  46. }
  47. catch
  48. {
  49. return defaultValue ?? new T();
  50. }
  51. }
  52.  
  53. public static string GetValueOrDefault(this JObject jObject, string key, string defaultValue = "")
  54. {
  55. var value = jObject[key];
  56.  
  57. if (value == null)
  58. return defaultValue;
  59.  
  60. try
  61. {
  62. return value.Value<string>();
  63. }
  64. catch
  65. {
  66. return defaultValue;
  67. }
  68. }
  69.  
  70. public static T GetValueOrDefault<T>(this JObject jObject, string key, T defaultValue = default(T)) where T : struct
  71. {
  72. var value = jObject[key];
  73.  
  74. if (value == null)
  75. return defaultValue;
  76.  
  77. try
  78. {
  79. return value.Value<T>();
  80. }
  81. catch
  82. {
  83. return defaultValue;
  84. }
  85. }
  86.  
  87. #endregion Retrieve values
  88.  
  89. #region Set values
  90.  
  91. /*
  92. Can simplify the writing of values to JSON:
  93.  
  94. public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
  95. {
  96. var items = (MyCustomList)value;
  97.  
  98. writer.WriteObject(() =>
  99. {
  100. writer.WriteField("ListField1", items.ListField1);
  101. writer.WriteField("ListField2", items.ListField2);
  102.  
  103. writer.WriteArray("Items", items, serializer);
  104. });
  105. }
  106.  
  107. */
  108.  
  109. public static void WriteField<T>(this JsonWriter writer, string fieldName, T fieldValue)
  110. {
  111. writer.WritePropertyName(fieldName);
  112. writer.WriteValue(fieldValue);
  113. }
  114.  
  115. public static void WriteArray(this JsonWriter writer, string fieldName, Action action)
  116. {
  117. writer.WritePropertyName(fieldName);
  118. writer.WriteStartArray();
  119. action();
  120. writer.WriteEndArray();
  121. }
  122.  
  123. public static void WriteArray(this JsonWriter writer, string fieldName, IEnumerable enumerable, JsonSerializer serializer)
  124. {
  125. writer.WriteArray(fieldName, () =>
  126. {
  127. foreach (var item in enumerable)
  128. {
  129. serializer.Serialize(writer, item);
  130. }
  131. });
  132. }
  133.  
  134. public static void WriteObject(this JsonWriter writer, Action action)
  135. {
  136. writer.WriteStartObject();
  137. action();
  138. writer.WriteEndObject();
  139. }
  140.  
  141. #endregion Set values
  142. }
  143. }
Add Comment
Please, Sign In to add comment