RealWorldTerrainJsonItem.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* INFINITY CODE 2013-2019 */
  2. /* http://www.infinity-code.com */
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Reflection;
  7. using System.Text;
  8. namespace InfinityCode.RealWorldTerrain.JSON
  9. {
  10. /// <summary>
  11. /// The base class of JSON elements.
  12. /// </summary>
  13. public abstract class RealWorldTerrainJsonItem : IEnumerable<RealWorldTerrainJsonItem>
  14. {
  15. /// <summary>
  16. /// Get the element by index
  17. /// </summary>
  18. /// <param name="index">Index of element</param>
  19. /// <returns>Element</returns>
  20. public abstract RealWorldTerrainJsonItem this[int index] { get; }
  21. /// <summary>
  22. /// Get the element by key.\n
  23. /// Supports XPath like selectors:\n
  24. /// ["key"] - get element by key.\n
  25. /// ["key1/key2"] - get element key2, which is a child of the element key1.\n
  26. /// ["key/N"] - where N is number. Get array element by index N, which is a child of the element key1.\n
  27. /// ["key/*"] - get all array elements, which is a child of the element key1.\n
  28. /// ["//key"] - get all elements with the key on the first or the deeper levels of the current element. \n
  29. /// </summary>
  30. /// <param name="key">Element key</param>
  31. /// <returns>Element</returns>
  32. public abstract RealWorldTerrainJsonItem this[string key] { get; }
  33. /// <summary>
  34. /// Serializes the object and adds to the current json node.
  35. /// </summary>
  36. /// <param name="obj">Object</param>
  37. /// <returns>Current json node</returns>
  38. public virtual RealWorldTerrainJsonItem AppendObject(object obj)
  39. {
  40. throw new Exception("AppendObject is only allowed for RealWorldTerrainJsonObject.");
  41. }
  42. /// <summary>
  43. /// Returns the value of the child element, converted to the specified type.
  44. /// </summary>
  45. /// <typeparam name="T">Type of variable</typeparam>
  46. /// <param name="childName">Child element key</param>
  47. /// <returns>Value</returns>
  48. public T ChildValue<T>(string childName)
  49. {
  50. RealWorldTerrainJsonItem el = this[childName];
  51. if (el == null) return default(T);
  52. return el.Value<T>();
  53. }
  54. /// <summary>
  55. /// Deserializes current element
  56. /// </summary>
  57. /// <typeparam name="T">Type</typeparam>
  58. /// <returns>Object</returns>
  59. public T Deserialize<T>(BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public)
  60. {
  61. return (T) Deserialize(typeof(T), bindingFlags);
  62. }
  63. /// <summary>
  64. /// Deserializes current element
  65. /// </summary>
  66. /// <param name="type">Type</param>
  67. /// <returns>Object</returns>
  68. public abstract object Deserialize(Type type, BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public);
  69. /// <summary>
  70. /// Get all elements with the key on the first or the deeper levels of the current element.
  71. /// </summary>
  72. /// <param name="key">Key</param>
  73. /// <returns>Elements</returns>
  74. public abstract RealWorldTerrainJsonItem GetAll(string key);
  75. /// <summary>
  76. /// Converts the current and the child elements to JSON string.
  77. /// </summary>
  78. /// <param name="b">StringBuilder instance</param>
  79. public abstract void ToJSON(StringBuilder b);
  80. IEnumerator IEnumerable.GetEnumerator()
  81. {
  82. return GetEnumerator();
  83. }
  84. public virtual IEnumerator<RealWorldTerrainJsonItem> GetEnumerator()
  85. {
  86. return null;
  87. }
  88. public override string ToString()
  89. {
  90. StringBuilder b = new StringBuilder();
  91. ToJSON(b);
  92. return b.ToString();
  93. }
  94. /// <summary>
  95. /// Returns the value of the element, converted to the specified type.
  96. /// </summary>
  97. /// <param name="type">Type of variable</param>
  98. /// <returns>Value</returns>
  99. public abstract object Value(Type type);
  100. /// <summary>
  101. /// Returns the value of the element, converted to the specified type.
  102. /// </summary>
  103. /// <typeparam name="T">Type of variable</typeparam>
  104. /// <returns>Value</returns>
  105. public virtual T Value<T>()
  106. {
  107. return (T) Value(typeof(T));
  108. }
  109. /// <summary>
  110. /// Returns the value of the element, converted to the specified type.
  111. /// </summary>
  112. /// <typeparam name="T">Type of variable</typeparam>
  113. /// <returns>Value</returns>
  114. public T V<T>()
  115. {
  116. return Value<T>();
  117. }
  118. /// <summary>
  119. /// Returns the value of the child element, converted to the specified type.
  120. /// </summary>
  121. /// <typeparam name="T">Type of variable</typeparam>
  122. /// <param name="childName">Child element key</param>
  123. /// <returns>Value</returns>
  124. public T V<T>(string childName)
  125. {
  126. return ChildValue<T>(childName);
  127. }
  128. }
  129. }