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