RealWorldTerrainJsonValue.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /* INFINITY CODE 2013-2019 */
  2. /* http://www.infinity-code.com */
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. using System.Reflection;
  7. using System.Text;
  8. using InfinityCode.RealWorldTerrain.Utils;
  9. using UnityEngine;
  10. namespace InfinityCode.RealWorldTerrain.JSON
  11. {
  12. /// <summary>
  13. /// The wrapper for JSON value.
  14. /// </summary>
  15. public class RealWorldTerrainJsonValue : RealWorldTerrainJsonItem
  16. {
  17. private ValueType _type;
  18. private object _value;
  19. public override RealWorldTerrainJsonItem this[string key]
  20. {
  21. get { return null; }
  22. }
  23. public override RealWorldTerrainJsonItem this[int index]
  24. {
  25. get { return null; }
  26. }
  27. /// <summary>
  28. /// Gets / sets the current value
  29. /// </summary>
  30. public object value
  31. {
  32. get { return _value; }
  33. set
  34. {
  35. #if !UNITY_WP_8_1 || UNITY_EDITOR
  36. if (value == null || value is DBNull)
  37. #else
  38. if (value == null)
  39. #endif
  40. {
  41. _type = ValueType.NULL;
  42. _value = value;
  43. }
  44. else if (value is string)
  45. {
  46. _type = ValueType.STRING;
  47. _value = value;
  48. }
  49. else if (value is double)
  50. {
  51. _type = ValueType.DOUBLE;
  52. _value = (double) value;
  53. }
  54. else if (value is float)
  55. {
  56. _type = ValueType.DOUBLE;
  57. _value = (double) (float) value;
  58. }
  59. else if (value is bool)
  60. {
  61. _type = ValueType.BOOLEAN;
  62. _value = value;
  63. }
  64. else if (value is long)
  65. {
  66. _type = ValueType.LONG;
  67. _value = value;
  68. }
  69. else if (value is int || value is short || value is byte)
  70. {
  71. _type = ValueType.LONG;
  72. _value = Convert.ChangeType(value, typeof(long));
  73. }
  74. else throw new Exception("Unknown type of value.");
  75. }
  76. }
  77. /// <summary>
  78. /// Get the type of value
  79. /// </summary>
  80. public ValueType type
  81. {
  82. get { return _type; }
  83. }
  84. /// <summary>
  85. /// Constructor
  86. /// </summary>
  87. /// <param name="value">Value</param>
  88. public RealWorldTerrainJsonValue(object value)
  89. {
  90. this.value = value;
  91. }
  92. /// <summary>
  93. /// Constructor
  94. /// </summary>
  95. /// <param name="value">Value</param>
  96. /// <param name="type">Type of value</param>
  97. public RealWorldTerrainJsonValue(object value, ValueType type)
  98. {
  99. _value = value;
  100. _type = type;
  101. }
  102. public override object Deserialize(Type type, BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public)
  103. {
  104. return Value(type);
  105. }
  106. public override RealWorldTerrainJsonItem GetAll(string key)
  107. {
  108. return null;
  109. }
  110. public override void ToJSON(StringBuilder b)
  111. {
  112. if (_type == ValueType.STRING) WriteString(b);
  113. else if (_type == ValueType.NULL) b.Append("null");
  114. else if (_type == ValueType.BOOLEAN) b.Append((bool) _value ? "true" : "false");
  115. else if (_type == ValueType.DOUBLE) b.Append(((double) value).ToString(RealWorldTerrainCultureInfo.cultureInfo));
  116. else b.Append(value);
  117. }
  118. public override IEnumerator<RealWorldTerrainJsonItem> GetEnumerator()
  119. {
  120. yield return this;
  121. }
  122. public override string ToString()
  123. {
  124. if (type == ValueType.DOUBLE) return ((double) value).ToString(RealWorldTerrainCultureInfo.cultureInfo);
  125. return value.ToString();
  126. }
  127. public override object Value(Type t)
  128. {
  129. if (_type == ValueType.NULL || _value == null)
  130. {
  131. if (RealWorldTerrainReflectionHelper.IsValueType(t)) return Activator.CreateInstance(t);
  132. return null;
  133. }
  134. if (t == typeof(string)) return Convert.ChangeType(_value, t);
  135. if (_type == ValueType.BOOLEAN)
  136. {
  137. if (t == typeof(bool)) return Convert.ChangeType(_value, t);
  138. }
  139. else if (_type == ValueType.DOUBLE)
  140. {
  141. if (t == typeof(double)) return Convert.ChangeType(_value, t, RealWorldTerrainCultureInfo.numberFormat);
  142. if (t == typeof(float)) return Convert.ChangeType((double) _value, t, RealWorldTerrainCultureInfo.numberFormat);
  143. }
  144. else if (_type == ValueType.LONG)
  145. {
  146. if (t == typeof(long)) return Convert.ChangeType(_value, t);
  147. #if UNITY_EDITOR
  148. if (t.IsSubclassOf(typeof(UnityEngine.Object)))
  149. {
  150. return UnityEditor.EditorUtility.InstanceIDToObject((int) (long) _value);
  151. }
  152. #endif
  153. try
  154. {
  155. return Convert.ChangeType((long) _value, t);
  156. }
  157. catch (Exception e)
  158. {
  159. Debug.Log(e.Message + "\n" + e.StackTrace);
  160. return null;
  161. }
  162. }
  163. else if (_type == ValueType.STRING)
  164. {
  165. MethodInfo method = RealWorldTerrainReflectionHelper.GetMethod(t, "Parse", new[] {typeof(string), typeof(IFormatProvider)});
  166. if (method != null) return method.Invoke(null, new object[] {value, RealWorldTerrainCultureInfo.numberFormat});
  167. method = RealWorldTerrainReflectionHelper.GetMethod(t, "Parse", new[] {typeof(string)});
  168. return method.Invoke(null, new[] {value});
  169. }
  170. StringBuilder builder = new StringBuilder();
  171. ToJSON(builder);
  172. throw new InvalidCastException(t.FullName + "\n" + builder);
  173. }
  174. private void WriteString(StringBuilder b)
  175. {
  176. b.Append('\"');
  177. string s = value as string;
  178. int runIndex = -1;
  179. int l = s.Length;
  180. for (var index = 0; index < l; ++index)
  181. {
  182. var c = s[index];
  183. if (c >= ' ' && c < 128 && c != '\"' && c != '\\')
  184. {
  185. if (runIndex == -1) runIndex = index;
  186. continue;
  187. }
  188. if (runIndex != -1)
  189. {
  190. b.Append(s, runIndex, index - runIndex);
  191. runIndex = -1;
  192. }
  193. switch (c)
  194. {
  195. case '\t':
  196. b.Append("\\t");
  197. break;
  198. case '\r':
  199. b.Append("\\r");
  200. break;
  201. case '\n':
  202. b.Append("\\n");
  203. break;
  204. case '"':
  205. case '\\':
  206. b.Append('\\');
  207. b.Append(c);
  208. break;
  209. default:
  210. b.Append("\\u");
  211. b.Append(((int) c).ToString("X4", NumberFormatInfo.InvariantInfo));
  212. break;
  213. }
  214. }
  215. if (runIndex != -1) b.Append(s, runIndex, s.Length - runIndex);
  216. b.Append('\"');
  217. }
  218. public static implicit operator string(RealWorldTerrainJsonValue val)
  219. {
  220. return val.ToString();
  221. }
  222. /// <summary>
  223. /// Type of value
  224. /// </summary>
  225. public enum ValueType
  226. {
  227. DOUBLE,
  228. LONG,
  229. STRING,
  230. BOOLEAN,
  231. NULL
  232. }
  233. }
  234. }