SimpleJSONUnity.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. // renaming the namespace to avoid conflict
  42. namespace OVRSimpleJSON // SimpleJSON
  43. {
  44. public enum JSONContainerType { Array, Object }
  45. public partial class JSONNode
  46. {
  47. public static JSONContainerType VectorContainerType = JSONContainerType.Array;
  48. public static JSONContainerType QuaternionContainerType = JSONContainerType.Array;
  49. public static JSONContainerType RectContainerType = JSONContainerType.Array;
  50. private static JSONNode GetContainer(JSONContainerType aType)
  51. {
  52. if (aType == JSONContainerType.Array)
  53. return new JSONArray();
  54. return new JSONObject();
  55. }
  56. #region implicit conversion operators
  57. public static implicit operator JSONNode(Vector2 aVec)
  58. {
  59. JSONNode n = GetContainer(VectorContainerType);
  60. n.WriteVector2(aVec);
  61. return n;
  62. }
  63. public static implicit operator JSONNode(Vector3 aVec)
  64. {
  65. JSONNode n = GetContainer(VectorContainerType);
  66. n.WriteVector3(aVec);
  67. return n;
  68. }
  69. public static implicit operator JSONNode(Vector4 aVec)
  70. {
  71. JSONNode n = GetContainer(VectorContainerType);
  72. n.WriteVector4(aVec);
  73. return n;
  74. }
  75. public static implicit operator JSONNode(Quaternion aRot)
  76. {
  77. JSONNode n = GetContainer(QuaternionContainerType);
  78. n.WriteQuaternion(aRot);
  79. return n;
  80. }
  81. public static implicit operator JSONNode(Rect aRect)
  82. {
  83. JSONNode n = GetContainer(RectContainerType);
  84. n.WriteRect(aRect);
  85. return n;
  86. }
  87. public static implicit operator JSONNode(RectOffset aRect)
  88. {
  89. JSONNode n = GetContainer(RectContainerType);
  90. n.WriteRectOffset(aRect);
  91. return n;
  92. }
  93. public static implicit operator Vector2(JSONNode aNode)
  94. {
  95. return aNode.ReadVector2();
  96. }
  97. public static implicit operator Vector3(JSONNode aNode)
  98. {
  99. return aNode.ReadVector3();
  100. }
  101. public static implicit operator Vector4(JSONNode aNode)
  102. {
  103. return aNode.ReadVector4();
  104. }
  105. public static implicit operator Quaternion(JSONNode aNode)
  106. {
  107. return aNode.ReadQuaternion();
  108. }
  109. public static implicit operator Rect(JSONNode aNode)
  110. {
  111. return aNode.ReadRect();
  112. }
  113. public static implicit operator RectOffset(JSONNode aNode)
  114. {
  115. return aNode.ReadRectOffset();
  116. }
  117. #endregion implicit conversion operators
  118. #region Vector2
  119. public Vector2 ReadVector2(Vector2 aDefault)
  120. {
  121. if (IsObject)
  122. return new Vector2(this["x"].AsFloat, this["y"].AsFloat);
  123. if (IsArray)
  124. return new Vector2(this[0].AsFloat, this[1].AsFloat);
  125. return aDefault;
  126. }
  127. public Vector2 ReadVector2(string aXName, string aYName)
  128. {
  129. if (IsObject)
  130. {
  131. return new Vector2(this[aXName].AsFloat, this[aYName].AsFloat);
  132. }
  133. return Vector2.zero;
  134. }
  135. public Vector2 ReadVector2()
  136. {
  137. return ReadVector2(Vector2.zero);
  138. }
  139. public JSONNode WriteVector2(Vector2 aVec, string aXName = "x", string aYName = "y")
  140. {
  141. if (IsObject)
  142. {
  143. Inline = true;
  144. this[aXName].AsFloat = aVec.x;
  145. this[aYName].AsFloat = aVec.y;
  146. }
  147. else if (IsArray)
  148. {
  149. Inline = true;
  150. this[0].AsFloat = aVec.x;
  151. this[1].AsFloat = aVec.y;
  152. }
  153. return this;
  154. }
  155. #endregion Vector2
  156. #region Vector3
  157. public Vector3 ReadVector3(Vector3 aDefault)
  158. {
  159. if (IsObject)
  160. return new Vector3(this["x"].AsFloat, this["y"].AsFloat, this["z"].AsFloat);
  161. if (IsArray)
  162. return new Vector3(this[0].AsFloat, this[1].AsFloat, this[2].AsFloat);
  163. return aDefault;
  164. }
  165. public Vector3 ReadVector3(string aXName, string aYName, string aZName)
  166. {
  167. if (IsObject)
  168. return new Vector3(this[aXName].AsFloat, this[aYName].AsFloat, this[aZName].AsFloat);
  169. return Vector3.zero;
  170. }
  171. public Vector3 ReadVector3()
  172. {
  173. return ReadVector3(Vector3.zero);
  174. }
  175. public JSONNode WriteVector3(Vector3 aVec, string aXName = "x", string aYName = "y", string aZName = "z")
  176. {
  177. if (IsObject)
  178. {
  179. Inline = true;
  180. this[aXName].AsFloat = aVec.x;
  181. this[aYName].AsFloat = aVec.y;
  182. this[aZName].AsFloat = aVec.z;
  183. }
  184. else if (IsArray)
  185. {
  186. Inline = true;
  187. this[0].AsFloat = aVec.x;
  188. this[1].AsFloat = aVec.y;
  189. this[2].AsFloat = aVec.z;
  190. }
  191. return this;
  192. }
  193. #endregion Vector3
  194. #region Vector4
  195. public Vector4 ReadVector4(Vector4 aDefault)
  196. {
  197. if (IsObject)
  198. return new Vector4(this["x"].AsFloat, this["y"].AsFloat, this["z"].AsFloat, this["w"].AsFloat);
  199. if (IsArray)
  200. return new Vector4(this[0].AsFloat, this[1].AsFloat, this[2].AsFloat, this[3].AsFloat);
  201. return aDefault;
  202. }
  203. public Vector4 ReadVector4()
  204. {
  205. return ReadVector4(Vector4.zero);
  206. }
  207. public JSONNode WriteVector4(Vector4 aVec)
  208. {
  209. if (IsObject)
  210. {
  211. Inline = true;
  212. this["x"].AsFloat = aVec.x;
  213. this["y"].AsFloat = aVec.y;
  214. this["z"].AsFloat = aVec.z;
  215. this["w"].AsFloat = aVec.w;
  216. }
  217. else if (IsArray)
  218. {
  219. Inline = true;
  220. this[0].AsFloat = aVec.x;
  221. this[1].AsFloat = aVec.y;
  222. this[2].AsFloat = aVec.z;
  223. this[3].AsFloat = aVec.w;
  224. }
  225. return this;
  226. }
  227. #endregion Vector4
  228. #region Quaternion
  229. public Quaternion ReadQuaternion(Quaternion aDefault)
  230. {
  231. if (IsObject)
  232. return new Quaternion(this["x"].AsFloat, this["y"].AsFloat, this["z"].AsFloat, this["w"].AsFloat);
  233. if (IsArray)
  234. return new Quaternion(this[0].AsFloat, this[1].AsFloat, this[2].AsFloat, this[3].AsFloat);
  235. return aDefault;
  236. }
  237. public Quaternion ReadQuaternion()
  238. {
  239. return ReadQuaternion(Quaternion.identity);
  240. }
  241. public JSONNode WriteQuaternion(Quaternion aRot)
  242. {
  243. if (IsObject)
  244. {
  245. Inline = true;
  246. this["x"].AsFloat = aRot.x;
  247. this["y"].AsFloat = aRot.y;
  248. this["z"].AsFloat = aRot.z;
  249. this["w"].AsFloat = aRot.w;
  250. }
  251. else if (IsArray)
  252. {
  253. Inline = true;
  254. this[0].AsFloat = aRot.x;
  255. this[1].AsFloat = aRot.y;
  256. this[2].AsFloat = aRot.z;
  257. this[3].AsFloat = aRot.w;
  258. }
  259. return this;
  260. }
  261. #endregion Quaternion
  262. #region Rect
  263. public Rect ReadRect(Rect aDefault)
  264. {
  265. if (IsObject)
  266. return new Rect(this["x"].AsFloat, this["y"].AsFloat, this["width"].AsFloat, this["height"].AsFloat);
  267. if (IsArray)
  268. return new Rect(this[0].AsFloat, this[1].AsFloat, this[2].AsFloat, this[3].AsFloat);
  269. return aDefault;
  270. }
  271. public Rect ReadRect()
  272. {
  273. return ReadRect(new Rect());
  274. }
  275. public JSONNode WriteRect(Rect aRect)
  276. {
  277. if (IsObject)
  278. {
  279. Inline = true;
  280. this["x"].AsFloat = aRect.x;
  281. this["y"].AsFloat = aRect.y;
  282. this["width"].AsFloat = aRect.width;
  283. this["height"].AsFloat = aRect.height;
  284. }
  285. else if (IsArray)
  286. {
  287. Inline = true;
  288. this[0].AsFloat = aRect.x;
  289. this[1].AsFloat = aRect.y;
  290. this[2].AsFloat = aRect.width;
  291. this[3].AsFloat = aRect.height;
  292. }
  293. return this;
  294. }
  295. #endregion Rect
  296. #region RectOffset
  297. public RectOffset ReadRectOffset(RectOffset aDefault)
  298. {
  299. if (this is JSONObject)
  300. return new RectOffset(this["left"].AsInt, this["right"].AsInt, this["top"].AsInt, this["bottom"].AsInt);
  301. if (this is JSONArray)
  302. return new RectOffset(this[0].AsInt, this[1].AsInt, this[2].AsInt, this[3].AsInt);
  303. return aDefault;
  304. }
  305. public RectOffset ReadRectOffset()
  306. {
  307. return ReadRectOffset(new RectOffset());
  308. }
  309. public JSONNode WriteRectOffset(RectOffset aRect)
  310. {
  311. if (IsObject)
  312. {
  313. Inline = true;
  314. this["left"].AsInt = aRect.left;
  315. this["right"].AsInt = aRect.right;
  316. this["top"].AsInt = aRect.top;
  317. this["bottom"].AsInt = aRect.bottom;
  318. }
  319. else if (IsArray)
  320. {
  321. Inline = true;
  322. this[0].AsInt = aRect.left;
  323. this[1].AsInt = aRect.right;
  324. this[2].AsInt = aRect.top;
  325. this[3].AsInt = aRect.bottom;
  326. }
  327. return this;
  328. }
  329. #endregion RectOffset
  330. #region Matrix4x4
  331. public Matrix4x4 ReadMatrix()
  332. {
  333. Matrix4x4 result = Matrix4x4.identity;
  334. if (IsArray)
  335. {
  336. for (int i = 0; i < 16; i++)
  337. {
  338. result[i] = this[i].AsFloat;
  339. }
  340. }
  341. return result;
  342. }
  343. public JSONNode WriteMatrix(Matrix4x4 aMatrix)
  344. {
  345. if (IsArray)
  346. {
  347. Inline = true;
  348. for (int i = 0; i < 16; i++)
  349. {
  350. this[i].AsFloat = aMatrix[i];
  351. }
  352. }
  353. return this;
  354. }
  355. #endregion Matrix4x4
  356. }
  357. }