PageRenderTime 55ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/Atlassian.Jira/Remote/CustomFieldValueSerializers.cs

https://bitbucket.org/farmas/atlassian.net-sdk
C# | 192 lines | 164 code | 26 blank | 2 comment | 16 complexity | 85f7b50e7d4229518e7106e5a9bb7964 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using Newtonsoft.Json;
  6. using Newtonsoft.Json.Linq;
  7. namespace Atlassian.Jira.Remote
  8. {
  9. public class SingleObjectCustomFieldValueSerializer : ICustomFieldValueSerializer
  10. {
  11. private readonly string _propertyName;
  12. public SingleObjectCustomFieldValueSerializer(string propertyName)
  13. {
  14. this._propertyName = propertyName;
  15. }
  16. public string[] FromJson(JToken json)
  17. {
  18. return new string[1] { json[this._propertyName]?.ToString() };
  19. }
  20. public JToken ToJson(string[] values)
  21. {
  22. return new JObject(new JProperty(this._propertyName, values[0]));
  23. }
  24. }
  25. public class MultiObjectCustomFieldValueSerializer : ICustomFieldValueSerializer
  26. {
  27. private readonly string _propertyName;
  28. public MultiObjectCustomFieldValueSerializer(string propertyName)
  29. {
  30. this._propertyName = propertyName;
  31. }
  32. public string[] FromJson(JToken json)
  33. {
  34. return ((JArray)json).Select(j => j[_propertyName].ToString()).ToArray();
  35. }
  36. public JToken ToJson(string[] values)
  37. {
  38. return JArray.FromObject(values.Select(v => new JObject(new JProperty(_propertyName, v))).ToArray());
  39. }
  40. }
  41. public class FloatCustomFieldValueSerializer : ICustomFieldValueSerializer
  42. {
  43. public string[] FromJson(JToken json)
  44. {
  45. return new string[1] { json.ToObject<string>() };
  46. }
  47. public JToken ToJson(string[] values)
  48. {
  49. return float.Parse(values[0], CultureInfo.InvariantCulture);
  50. }
  51. }
  52. public class MultiStringCustomFieldValueSerializer : ICustomFieldValueSerializer
  53. {
  54. public string[] FromJson(JToken json)
  55. {
  56. return JsonConvert.DeserializeObject<string[]>(json.ToString());
  57. }
  58. public JToken ToJson(string[] values)
  59. {
  60. return JArray.FromObject(values);
  61. }
  62. }
  63. public class CascadingSelectCustomFieldValueSerializer : ICustomFieldValueSerializer
  64. {
  65. public string[] FromJson(JToken json)
  66. {
  67. var parentOption = json["value"];
  68. var childOption = json["child"];
  69. if (parentOption == null)
  70. {
  71. throw new InvalidOperationException(String.Format(
  72. "Unable to deserialize custom field as a cascading select list. The parent value is required. Json: {0}",
  73. json.ToString()));
  74. }
  75. else if (childOption == null || childOption["value"] == null)
  76. {
  77. return new string[] { parentOption.ToString() };
  78. }
  79. else
  80. {
  81. return new string[2] { parentOption.ToString(), childOption["value"].ToString() };
  82. }
  83. }
  84. public JToken ToJson(string[] values)
  85. {
  86. if (values == null || values.Length < 1)
  87. {
  88. throw new InvalidOperationException("Unable to serialize the custom field as a cascading select list. At least the parent value is required.");
  89. }
  90. else if (values.Length == 1)
  91. {
  92. return JToken.FromObject(new { value = values[0] });
  93. }
  94. else
  95. {
  96. return JToken.FromObject(new
  97. {
  98. value = values[0],
  99. child = new
  100. {
  101. value = values[1]
  102. }
  103. });
  104. }
  105. }
  106. }
  107. public class GreenhopperSprintCustomFieldValueSerialiser : ICustomFieldValueSerializer
  108. {
  109. private readonly string _propertyName;
  110. public GreenhopperSprintCustomFieldValueSerialiser(string propertyName)
  111. {
  112. this._propertyName = propertyName;
  113. }
  114. // Sprint field is malformed
  115. // See https://ecosystem.atlassian.net/browse/ACJIRA-918 for more information
  116. public string[] FromJson(JToken json)
  117. {
  118. return json.ToString()
  119. .Split(new char[] { '{', '}', '[', ']', ',' })
  120. .Where(x => x.StartsWith(_propertyName))
  121. .Select(x => x.Split(new char[] { '=' })[1])
  122. .ToArray();
  123. }
  124. public JToken ToJson(string[] values)
  125. {
  126. string val = values != null ? values.FirstOrDefault() : null;
  127. int id = 0;
  128. if (int.TryParse(val, out id))
  129. {
  130. return id;
  131. }
  132. return val;
  133. }
  134. }
  135. public class GreenhopperSprintJsonCustomFieldValueSerialiser : ICustomFieldValueSerializer
  136. {
  137. public string[] FromJson(JToken json)
  138. {
  139. return JsonConvert
  140. .DeserializeObject<List<Sprint>>(json.ToString())
  141. .OrderByDescending(x => x.endDate)
  142. .Select(x => x.name)
  143. .ToArray();
  144. }
  145. public JToken ToJson(string[] values)
  146. {
  147. var val = values?.FirstOrDefault();
  148. if (int.TryParse(val, out var id))
  149. {
  150. return id;
  151. }
  152. return val;
  153. }
  154. }
  155. internal class Sprint
  156. {
  157. public int id { get; set; }
  158. public string name { get; set; }
  159. public string state { get; set; }
  160. public int boardId { get; set; }
  161. public string goal { get; set; }
  162. public DateTime startDate { get; set; }
  163. public DateTime endDate { get; set; }
  164. public DateTime completeDate { get; set; }
  165. }
  166. }