SimpleJSONUnity.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. #region License and information
  2. /* * * * *
  3. *
  4. * Unity extension for the SimpleJSON framework. It does only work together with
  5. * the SimpleJSON.cs
  6. * It provides several helpers and conversion operators to serialize/deserialize
  7. * common Unity types such as Vector2/3/4, Rect, RectOffset, Quaternion and
  8. * Matrix4x4 as JSONObject or JSONArray.
  9. * This extension will add 3 static settings to the JSONNode class:
  10. * ( VectorContainerType, QuaternionContainerType, RectContainerType ) which
  11. * control what node type should be used for serializing the given type. So a
  12. * Vector3 as array would look like [12,32,24] and {"x":12, "y":32, "z":24} as
  13. * object.
  14. *
  15. *
  16. * The MIT License (MIT)
  17. *
  18. * Copyright (c) 2012-2017 Markus Göbel (Bunny83)
  19. *
  20. * Permission is hereby granted, free of charge, to any person obtaining a copy
  21. * of this software and associated documentation files (the "Software"), to deal
  22. * in the Software without restriction, including without limitation the rights
  23. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  24. * copies of the Software, and to permit persons to whom the Software is
  25. * furnished to do so, subject to the following conditions:
  26. *
  27. * The above copyright notice and this permission notice shall be included in all
  28. * copies or substantial portions of the Software.
  29. *
  30. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  31. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  32. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  33. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  34. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  35. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  36. * SOFTWARE.
  37. *
  38. * * * * */
  39. #endregion License and information
  40. using UnityEngine;
  41. namespace SimpleJSON
  42. {
  43. public enum JSONContainerType { Array, Object }
  44. public partial class JSONNode
  45. {
  46. public static JSONContainerType VectorContainerType = JSONContainerType.Array;
  47. public static JSONContainerType QuaternionContainerType = JSONContainerType.Array;
  48. public static JSONContainerType RectContainerType = JSONContainerType.Array;
  49. private static JSONNode GetContainer(JSONContainerType aType)
  50. {
  51. if (aType == JSONContainerType.Array)
  52. return new JSONArray();
  53. return new JSONObject();
  54. }
  55. #region implicit conversion operators
  56. public static implicit operator JSONNode(Vector2 aVec)
  57. {
  58. JSONNode n = GetContainer(VectorContainerType);
  59. n.WriteVector2(aVec);
  60. return n;
  61. }
  62. public static implicit operator JSONNode(Vector3 aVec)
  63. {
  64. JSONNode n = GetContainer(VectorContainerType);
  65. n.WriteVector3(aVec);
  66. return n;
  67. }
  68. public static implicit operator JSONNode(Vector4 aVec)
  69. {
  70. JSONNode n = GetContainer(VectorContainerType);
  71. n.WriteVector4(aVec);
  72. return n;
  73. }
  74. public static implicit operator JSONNode(Quaternion aRot)
  75. {
  76. JSONNode n = GetContainer(QuaternionContainerType);
  77. n.WriteQuaternion(aRot);
  78. return n;
  79. }
  80. public static implicit operator JSONNode(Rect aRect)
  81. {
  82. JSONNode n = GetContainer(RectContainerType);
  83. n.WriteRect(aRect);
  84. return n;
  85. }
  86. public static implicit operator JSONNode(RectOffset aRect)
  87. {
  88. JSONNode n = GetContainer(RectContainerType);
  89. n.WriteRectOffset(aRect);
  90. return n;
  91. }
  92. public static implicit operator Vector2(JSONNode aNode)
  93. {
  94. return aNode.ReadVector2();
  95. }
  96. public static implicit operator Vector3(JSONNode aNode)
  97. {
  98. return aNode.ReadVector3();
  99. }
  100. public static implicit operator Vector4(JSONNode aNode)
  101. {
  102. return aNode.ReadVector4();
  103. }
  104. public static implicit operator Quaternion(JSONNode aNode)
  105. {
  106. return aNode.ReadQuaternion();
  107. }
  108. public static implicit operator Rect(JSONNode aNode)
  109. {
  110. return aNode.ReadRect();
  111. }
  112. public static implicit operator RectOffset(JSONNode aNode)
  113. {
  114. return aNode.ReadRectOffset();
  115. }
  116. #endregion implicit conversion operators
  117. #region Vector2
  118. public Vector2 ReadVector2(Vector2 aDefault)
  119. {
  120. if (IsObject)
  121. return new Vector2(this["x"].AsFloat, this["y"].AsFloat);
  122. if (IsArray)
  123. return new Vector2(this[0].AsFloat, this[1].AsFloat);
  124. return aDefault;
  125. }
  126. public Vector2 ReadVector2(string aXName, string aYName)
  127. {
  128. if (IsObject)
  129. {
  130. return new Vector2(this[aXName].AsFloat, this[aYName].AsFloat);
  131. }
  132. return Vector2.zero;
  133. }
  134. public Vector2 ReadVector2()
  135. {
  136. return ReadVector2(Vector2.zero);
  137. }
  138. public JSONNode WriteVector2(Vector2 aVec, string aXName = "x", string aYName = "y")
  139. {
  140. if (IsObject)
  141. {
  142. Inline = true;
  143. this[aXName].AsFloat = aVec.x;
  144. this[aYName].AsFloat = aVec.y;
  145. }
  146. else if (IsArray)
  147. {
  148. Inline = true;
  149. this[0].AsFloat = aVec.x;
  150. this[1].AsFloat = aVec.y;
  151. }
  152. return this;
  153. }
  154. #endregion Vector2
  155. #region Vector3
  156. public Vector3 ReadVector3(Vector3 aDefault)
  157. {
  158. if (IsObject)
  159. return new Vector3(this["x"].AsFloat, this["y"].AsFloat, this["z"].AsFloat);
  160. if (IsArray)
  161. return new Vector3(this[0].AsFloat, this[1].AsFloat, this[2].AsFloat);
  162. return aDefault;
  163. }
  164. public Vector3 ReadVector3(string aXName, string aYName, string aZName)
  165. {
  166. if (IsObject)
  167. return new Vector3(this[aXName].AsFloat, this[aYName].AsFloat, this[aZName].AsFloat);
  168. return Vector3.zero;
  169. }
  170. public Vector3 ReadVector3()
  171. {
  172. return ReadVector3(Vector3.zero);
  173. }
  174. public JSONNode WriteVector3(Vector3 aVec, string aXName = "x", string aYName = "y", string aZName = "z")
  175. {
  176. if (IsObject)
  177. {
  178. Inline = true;
  179. this[aXName].AsFloat = aVec.x;
  180. this[aYName].AsFloat = aVec.y;
  181. this[aZName].AsFloat = aVec.z;
  182. }
  183. else if (IsArray)
  184. {
  185. Inline = true;
  186. this[0].AsFloat = aVec.x;
  187. this[1].AsFloat = aVec.y;
  188. this[2].AsFloat = aVec.z;
  189. }
  190. return this;
  191. }
  192. #endregion Vector3
  193. #region Vector4
  194. public Vector4 ReadVector4(Vector4 aDefault)
  195. {
  196. if (IsObject)
  197. return new Vector4(this["x"].AsFloat, this["y"].AsFloat, this["z"].AsFloat, this["w"].AsFloat);
  198. if (IsArray)
  199. return new Vector4(this[0].AsFloat, this[1].AsFloat, this[2].AsFloat, this[3].AsFloat);
  200. return aDefault;
  201. }
  202. public Vector4 ReadVector4()
  203. {
  204. return ReadVector4(Vector4.zero);
  205. }
  206. public JSONNode WriteVector4(Vector4 aVec)
  207. {
  208. if (IsObject)
  209. {
  210. Inline = true;
  211. this["x"].AsFloat = aVec.x;
  212. this["y"].AsFloat = aVec.y;
  213. this["z"].AsFloat = aVec.z;
  214. this["w"].AsFloat = aVec.w;
  215. }
  216. else if (IsArray)
  217. {
  218. Inline = true;
  219. this[0].AsFloat = aVec.x;
  220. this[1].AsFloat = aVec.y;
  221. this[2].AsFloat = aVec.z;
  222. this[3].AsFloat = aVec.w;
  223. }
  224. return this;
  225. }
  226. #endregion Vector4
  227. #region Quaternion
  228. public Quaternion ReadQuaternion(Quaternion aDefault)
  229. {
  230. if (IsObject)
  231. return new Quaternion(this["x"].AsFloat, this["y"].AsFloat, this["z"].AsFloat, this["w"].AsFloat);
  232. if (IsArray)
  233. return new Quaternion(this[0].AsFloat, this[1].AsFloat, this[2].AsFloat, this[3].AsFloat);
  234. return aDefault;
  235. }
  236. public Quaternion ReadQuaternion()
  237. {
  238. return ReadQuaternion(Quaternion.identity);
  239. }
  240. public JSONNode WriteQuaternion(Quaternion aRot)
  241. {
  242. if (IsObject)
  243. {
  244. Inline = true;
  245. this["x"].AsFloat = aRot.x;
  246. this["y"].AsFloat = aRot.y;
  247. this["z"].AsFloat = aRot.z;
  248. this["w"].AsFloat = aRot.w;
  249. }
  250. else if (IsArray)
  251. {
  252. Inline = true;
  253. this[0].AsFloat = aRot.x;
  254. this[1].AsFloat = aRot.y;
  255. this[2].AsFloat = aRot.z;
  256. this[3].AsFloat = aRot.w;
  257. }
  258. return this;
  259. }
  260. #endregion Quaternion
  261. #region Rect
  262. public Rect ReadRect(Rect aDefault)
  263. {
  264. if (IsObject)
  265. return new Rect(this["x"].AsFloat, this["y"].AsFloat, this["width"].AsFloat, this["height"].AsFloat);
  266. if (IsArray)
  267. return new Rect(this[0].AsFloat, this[1].AsFloat, this[2].AsFloat, this[3].AsFloat);
  268. return aDefault;
  269. }
  270. public Rect ReadRect()
  271. {
  272. return ReadRect(new Rect());
  273. }
  274. public JSONNode WriteRect(Rect aRect)
  275. {
  276. if (IsObject)
  277. {
  278. Inline = true;
  279. this["x"].AsFloat = aRect.x;
  280. this["y"].AsFloat = aRect.y;
  281. this["width"].AsFloat = aRect.width;
  282. this["height"].AsFloat = aRect.height;
  283. }
  284. else if (IsArray)
  285. {
  286. Inline = true;
  287. this[0].AsFloat = aRect.x;
  288. this[1].AsFloat = aRect.y;
  289. this[2].AsFloat = aRect.width;
  290. this[3].AsFloat = aRect.height;
  291. }
  292. return this;
  293. }
  294. #endregion Rect
  295. #region RectOffset
  296. public RectOffset ReadRectOffset(RectOffset aDefault)
  297. {
  298. if (this is JSONObject)
  299. return new RectOffset(this["left"].AsInt, this["right"].AsInt, this["top"].AsInt, this["bottom"].AsInt);
  300. if (this is JSONArray)
  301. return new RectOffset(this[0].AsInt, this[1].AsInt, this[2].AsInt, this[3].AsInt);
  302. return aDefault;
  303. }
  304. public RectOffset ReadRectOffset()
  305. {
  306. return ReadRectOffset(new RectOffset());
  307. }
  308. public JSONNode WriteRectOffset(RectOffset aRect)
  309. {
  310. if (IsObject)
  311. {
  312. Inline = true;
  313. this["left"].AsInt = aRect.left;
  314. this["right"].AsInt = aRect.right;
  315. this["top"].AsInt = aRect.top;
  316. this["bottom"].AsInt = aRect.bottom;
  317. }
  318. else if (IsArray)
  319. {
  320. Inline = true;
  321. this[0].AsInt = aRect.left;
  322. this[1].AsInt = aRect.right;
  323. this[2].AsInt = aRect.top;
  324. this[3].AsInt = aRect.bottom;
  325. }
  326. return this;
  327. }
  328. #endregion RectOffset
  329. #region Matrix4x4
  330. public Matrix4x4 ReadMatrix()
  331. {
  332. Matrix4x4 result = Matrix4x4.identity;
  333. if (IsArray)
  334. {
  335. for (int i = 0; i < 16; i++)
  336. {
  337. result[i] = this[i].AsFloat;
  338. }
  339. }
  340. return result;
  341. }
  342. public JSONNode WriteMatrix(Matrix4x4 aMatrix)
  343. {
  344. if (IsArray)
  345. {
  346. Inline = true;
  347. for (int i = 0; i < 16; i++)
  348. {
  349. this[i].AsFloat = aMatrix[i];
  350. }
  351. }
  352. return this;
  353. }
  354. #endregion Matrix4x4
  355. }
  356. }