public static JSONObject FromColor(Color c)
 {
     var cdata = new JSONObject(JSONObject.Type.OBJECT);
     if (c.r != 0)
         cdata.AddField("r", c.r);
     if (c.g != 0)
         cdata.AddField("g", c.g);
     if (c.b != 0)
         cdata.AddField("b", c.b);
     if (c.a != 0)
         cdata.AddField("hbPos", c.a);
     return cdata;
 }
 /*
  * Matrix4x4
  */
 public static JSONObject FromMatrix4x4(Matrix4x4 m)
 {
     var mdata = new JSONObject(JSONObject.Type.OBJECT);
     if (m.m00 != 0)
         mdata.AddField("m00", m.m00);
     if (m.m01 != 0)
         mdata.AddField("m01", m.m01);
     if (m.m02 != 0)
         mdata.AddField("m02", m.m02);
     if (m.m03 != 0)
         mdata.AddField("m03", m.m03);
     if (m.m10 != 0)
         mdata.AddField("m10", m.m10);
     if (m.m11 != 0)
         mdata.AddField("m11", m.m11);
     if (m.m12 != 0)
         mdata.AddField("m12", m.m12);
     if (m.m13 != 0)
         mdata.AddField("m13", m.m13);
     if (m.m20 != 0)
         mdata.AddField("m20", m.m20);
     if (m.m21 != 0)
         mdata.AddField("m21", m.m21);
     if (m.m22 != 0)
         mdata.AddField("m22", m.m22);
     if (m.m23 != 0)
         mdata.AddField("m23", m.m23);
     if (m.m30 != 0)
         mdata.AddField("m30", m.m30);
     if (m.m31 != 0)
         mdata.AddField("m31", m.m31);
     if (m.m32 != 0)
         mdata.AddField("m32", m.m32);
     if (m.m33 != 0)
         mdata.AddField("m33", m.m33);
     return mdata;
 }
        public static void Serialize(object obj, JSONObject jsonObject)
        {
            Type rootType = obj.GetType();

            foreach (var member in rootType.GetMembers())
            {
                // Iterate through all fields with JSONAttribute from root object
                JSONAttribute[] attrFields = (JSONAttribute[])member.GetCustomAttributes(typeof(JSONAttribute), true);
                foreach (JSONAttribute attrField in attrFields)
                {
                    string    data = attrField.data;                        // Get the data name of the field
                    FieldInfo info = rootType.GetField(data);               // Create a connection with the field
                    if (info == null)                                       // If no field next (probably wrong design of the class)
                    {
                        continue;
                    }

                    AttrEnum attrType = attrField.attrType;                        // Get the type of the attribute
                    // Type is either object, item or array.
                    switch (attrType)
                    {
                    case AttrEnum.AttrObject:
                    {
                        JSONObject jsonChild = new JSONObject();
                        jsonObject.Add(data, jsonChild);
                        Serialize(info.GetValue(obj), jsonChild);
                        break;
                    }

                    case AttrEnum.AttrItem:
                    {
                        object val           = info.GetValue(obj);
                        string result        = val.ToString();
                        Type   attrFieldType = attrField.type;
                        if (attrFieldType == typeof(Color))
                        {
                            Color col = (Color)val;
                            result = ((int)(col.r * 255f)).ToString() + ","
                                     + ((int)(col.g * 255f)).ToString() + ","
                                     + ((int)(col.b * 255f)).ToString() + ","
                                     + ((int)(col.a * 255f)).ToString();
                        }
                        else if (attrFieldType == typeof(Vector2))
                        {
                            Vector2 v = (Vector2)val;
                            result = v.x.ToString() + "," + v.y.ToString();
                        }
                        else if (attrFieldType == typeof(Vector3))
                        {
                            Vector3 v = (Vector3)val;
                            result = v.x.ToString() + "," + v.y.ToString() + "," + v.z.ToString();
                        }
                        else if (attrFieldType == typeof(Vector4))
                        {
                            Vector4 v = (Vector4)val;
                            result = v.x.ToString() + "," + v.y.ToString() + "," + v.z.ToString() + v.z.ToString();
                        }
                        JSONValue value = new JSONValue(result);
                        jsonObject.Add(data, value);
                        break;
                    }

                    case AttrEnum.AttrArray:
                    {
                        object[]  val       = (object[])info.GetValue(obj);
                        JSONArray jsonArray = new JSONArray();
                        jsonObject.Add(data, jsonArray);
                        foreach (object v in val)
                        {
                            JSONObject jsonChild = new JSONObject();
                            Serialize(v, jsonChild);
                            jsonArray.Add(jsonChild);
                        }
                        break;
                    }
                    }
                }
            }
        }
 public static JSONObject FromVector2(Vector2 v)
 {
     var vdata = new JSONObject(JSONObject.Type.OBJECT);
     if (v.x != 0)
         vdata.AddField("x", v.x);
     if (v.y != 0)
         vdata.AddField("y", v.y);
     return vdata;
 }
Beispiel #5
0
		/*
	 * The Merge function is experimental. Use at your own risk.
	 */

		public void Merge(JSONObject obj) { MergeRecur(this, obj); }
Beispiel #6
0
		public void AddField(string name, JSONObject obj)
		{
			if (obj)
			{
				//Don't do anything if the object is null
				if (type != Type.OBJECT)
				{
					keys = new List<string>();
					if (type == Type.ARRAY)
						for (var i = 0; i < list.Count; i++)
							keys.Add(i + "");
					else if (list == null)
						list = new List<JSONObject>();
					type = Type.OBJECT; //Congratulations, son, you're an OBJECT now
				}
				keys.Add(name);
				list.Add(obj);
			}
		}
Beispiel #7
0
		public void Absorb(JSONObject obj)
		{
			list.AddRange(obj.list);
			keys.AddRange(obj.keys);
			str = obj.str;
			n = obj.n;
			b = obj.b;
			type = obj.type;
		}
        /// <summary>
        /// Attempt to parse a string into a JSONObject.
        /// </summary>
        /// <param name="jsonString"></param>
        /// <returns>A new JSONObject or null if parsing fails.</returns>
        public static JSONObject Parse(string jsonString)
        {
            if (string.IsNullOrEmpty(jsonString))
            {
                return(null);
            }

            JSONValue currentValue = null;

            var keyList = new List <string>();

            var state = JSONParsingState.Object;

            for (var startPosition = 0; startPosition < jsonString.Length; ++startPosition)
            {
                startPosition = SkipWhitespace(jsonString, startPosition);

                switch (state)
                {
                case JSONParsingState.Object:
                    if (jsonString[startPosition] != '{')
                    {
                        return(Fail('{', startPosition));
                    }

                    JSONValue newObj = new JSONObject();
                    if (currentValue != null)
                    {
                        newObj.Parent = currentValue;
                    }
                    currentValue = newObj;

                    state = JSONParsingState.Key;
                    break;

                case JSONParsingState.EndObject:
                    if (jsonString[startPosition] != '}')
                    {
                        return(Fail('}', startPosition));
                    }

                    if (currentValue.Parent == null)
                    {
                        return(currentValue.Obj);
                    }

                    switch (currentValue.Parent.Type)
                    {
                    case JSONValueType.Object:
                        currentValue.Parent.Obj.values[keyList.Pop()] = new JSONValue(currentValue.Obj);
                        break;

                    case JSONValueType.Array:
                        currentValue.Parent.Array.Add(new JSONValue(currentValue.Obj));
                        break;

                    default:
                        return(Fail("valid object", startPosition));
                    }
                    currentValue = currentValue.Parent;

                    state = JSONParsingState.ValueSeparator;
                    break;

                case JSONParsingState.Key:
                    if (jsonString[startPosition] == '}')
                    {
                        --startPosition;
                        state = JSONParsingState.EndObject;
                        break;
                    }

                    var key = ParseString(jsonString, ref startPosition);
                    if (key == null)
                    {
                        return(Fail("key string", startPosition));
                    }
                    keyList.Add(key);
                    state = JSONParsingState.KeyValueSeparator;
                    break;

                case JSONParsingState.KeyValueSeparator:
                    if (jsonString[startPosition] != ':')
                    {
                        return(Fail(':', startPosition));
                    }
                    state = JSONParsingState.Value;
                    break;

                case JSONParsingState.ValueSeparator:
                    switch (jsonString[startPosition])
                    {
                    case ',':
                        state = currentValue.Type == JSONValueType.Object ? JSONParsingState.Key : JSONParsingState.Value;
                        break;

                    case '}':
                        state = JSONParsingState.EndObject;
                        --startPosition;
                        break;

                    case ']':
                        state = JSONParsingState.EndArray;
                        --startPosition;
                        break;

                    default:
                        return(Fail(", } ]", startPosition));
                    }
                    break;

                case JSONParsingState.Value:
                {
                    var c = jsonString[startPosition];
                    if (c == '"')
                    {
                        state = JSONParsingState.String;
                    }
                    else if (char.IsDigit(c) || c == '-')
                    {
                        state = JSONParsingState.Number;
                    }
                    else
                    {
                        switch (c)
                        {
                        case '{':
                            state = JSONParsingState.Object;
                            break;

                        case '[':
                            state = JSONParsingState.Array;
                            break;

                        case ']':
                            if (currentValue.Type == JSONValueType.Array)
                            {
                                state = JSONParsingState.EndArray;
                            }
                            else
                            {
                                return(Fail("valid array", startPosition));
                            }
                            break;

                        case 'f':
                        case 't':
                            state = JSONParsingState.Boolean;
                            break;


                        case 'n':
                            state = JSONParsingState.Null;
                            break;

                        default:
                            return(Fail("beginning of value", startPosition));
                        }
                    }

                    --startPosition;                             //To re-evaluate this char in the newly selected state
                    break;
                }

                case JSONParsingState.String:
                    var str = ParseString(jsonString, ref startPosition);
                    if (str == null)
                    {
                        return(Fail("string value", startPosition));
                    }

                    switch (currentValue.Type)
                    {
                    case JSONValueType.Object:
                        currentValue.Obj.values[keyList.Pop()] = new JSONValue(str);
                        break;

                    case JSONValueType.Array:
                        currentValue.Array.Add(str);
                        break;

                    default:
                        JSONLogger.Error("Fatal error, current JSON value not valid");
                        return(null);
                    }

                    state = JSONParsingState.ValueSeparator;
                    break;

                case JSONParsingState.Number:
                    var number = ParseNumber(jsonString, ref startPosition);
                    if (double.IsNaN(number))
                    {
                        return(Fail("valid number", startPosition));
                    }

                    switch (currentValue.Type)
                    {
                    case JSONValueType.Object:
                        currentValue.Obj.values[keyList.Pop()] = new JSONValue(number);
                        break;

                    case JSONValueType.Array:
                        currentValue.Array.Add(number);
                        break;

                    default:
                        JSONLogger.Error("Fatal error, current JSON value not valid");
                        return(null);
                    }

                    state = JSONParsingState.ValueSeparator;

                    break;

                case JSONParsingState.Boolean:
                    if (jsonString[startPosition] == 't')
                    {
                        if (jsonString.Length < startPosition + 4 ||
                            jsonString[startPosition + 1] != 'r' ||
                            jsonString[startPosition + 2] != 'u' ||
                            jsonString[startPosition + 3] != 'e')
                        {
                            return(Fail("true", startPosition));
                        }

                        switch (currentValue.Type)
                        {
                        case JSONValueType.Object:
                            currentValue.Obj.values[keyList.Pop()] = new JSONValue(true);
                            break;

                        case JSONValueType.Array:
                            currentValue.Array.Add(new JSONValue(true));
                            break;

                        default:
                            JSONLogger.Error("Fatal error, current JSON value not valid");
                            return(null);
                        }

                        startPosition += 3;
                    }
                    else
                    {
                        if (jsonString.Length < startPosition + 5 ||
                            jsonString[startPosition + 1] != 'a' ||
                            jsonString[startPosition + 2] != 'l' ||
                            jsonString[startPosition + 3] != 's' ||
                            jsonString[startPosition + 4] != 'e')
                        {
                            return(Fail("false", startPosition));
                        }

                        switch (currentValue.Type)
                        {
                        case JSONValueType.Object:
                            currentValue.Obj.values[keyList.Pop()] = new JSONValue(false);
                            break;

                        case JSONValueType.Array:
                            currentValue.Array.Add(new JSONValue(false));
                            break;

                        default:
                            JSONLogger.Error("Fatal error, current JSON value not valid");
                            return(null);
                        }

                        startPosition += 4;
                    }

                    state = JSONParsingState.ValueSeparator;
                    break;

                case JSONParsingState.Array:
                    if (jsonString[startPosition] != '[')
                    {
                        return(Fail('[', startPosition));
                    }

                    JSONValue newArray = new JSONArray();
                    if (currentValue != null)
                    {
                        newArray.Parent = currentValue;
                    }
                    currentValue = newArray;

                    state = JSONParsingState.Value;
                    break;

                case JSONParsingState.EndArray:
                    if (jsonString[startPosition] != ']')
                    {
                        return(Fail(']', startPosition));
                    }

                    if (currentValue.Parent == null)
                    {
                        return(currentValue.Obj);
                    }

                    switch (currentValue.Parent.Type)
                    {
                    case JSONValueType.Object:
                        currentValue.Parent.Obj.values[keyList.Pop()] = new JSONValue(currentValue.Array);
                        break;

                    case JSONValueType.Array:
                        currentValue.Parent.Array.Add(new JSONValue(currentValue.Array));
                        break;

                    default:
                        return(Fail("valid object", startPosition));
                    }
                    currentValue = currentValue.Parent;

                    state = JSONParsingState.ValueSeparator;
                    break;

                case JSONParsingState.Null:
                    if (jsonString[startPosition] == 'n')
                    {
                        if (jsonString.Length < startPosition + 4 ||
                            jsonString[startPosition + 1] != 'u' ||
                            jsonString[startPosition + 2] != 'l' ||
                            jsonString[startPosition + 3] != 'l')
                        {
                            return(Fail("null", startPosition));
                        }

                        switch (currentValue.Type)
                        {
                        case JSONValueType.Object:
                            currentValue.Obj.values[keyList.Pop()] = new JSONValue(JSONValueType.Null);
                            break;

                        case JSONValueType.Array:
                            currentValue.Array.Add(new JSONValue(JSONValueType.Null));
                            break;

                        default:
                            JSONLogger.Error("Fatal error, current JSON value not valid");
                            return(null);
                        }

                        startPosition += 3;
                    }
                    state = JSONParsingState.ValueSeparator;
                    break;
                }
            }
            JSONLogger.Error("Unexpected end of string");
            return(null);
        }
 public static Rect ToRect(JSONObject obj)
 {
     var r = new Rect();
     for (var i = 0; i < obj.Count; i++)
         switch (obj.keys[i])
         {
             case "x":
                 r.x = obj[i].f;
                 break;
             case "y":
                 r.y = obj[i].f;
                 break;
             case "height":
                 r.height = obj[i].f;
                 break;
             case "width":
                 r.width = obj[i].f;
                 break;
         }
     return r;
 }
 public static Quaternion ToQuaternion(JSONObject obj)
 {
     var x = obj["x"] ? obj["x"].f : 0;
     var y = obj["y"] ? obj["y"].f : 0;
     var z = obj["z"] ? obj["z"].f : 0;
     var w = obj["width"] ? obj["width"].f : 0;
     return new Quaternion(x, y, z, w);
 }
 public static Matrix4x4 ToMatrix4x4(JSONObject obj)
 {
     var result = new Matrix4x4();
     if (obj["m00"])
         result.m00 = obj["m00"].f;
     if (obj["m01"])
         result.m01 = obj["m01"].f;
     if (obj["m02"])
         result.m02 = obj["m02"].f;
     if (obj["m03"])
         result.m03 = obj["m03"].f;
     if (obj["m10"])
         result.m10 = obj["m10"].f;
     if (obj["m11"])
         result.m11 = obj["m11"].f;
     if (obj["m12"])
         result.m12 = obj["m12"].f;
     if (obj["m13"])
         result.m13 = obj["m13"].f;
     if (obj["m20"])
         result.m20 = obj["m20"].f;
     if (obj["m21"])
         result.m21 = obj["m21"].f;
     if (obj["m22"])
         result.m22 = obj["m22"].f;
     if (obj["m23"])
         result.m23 = obj["m23"].f;
     if (obj["m30"])
         result.m30 = obj["m30"].f;
     if (obj["m31"])
         result.m31 = obj["m31"].f;
     if (obj["m32"])
         result.m32 = obj["m32"].f;
     if (obj["m33"])
         result.m33 = obj["m33"].f;
     return result;
 }
 public static LayerMask ToLayerMask(JSONObject obj)
 {
     var l = new LayerMask();
     l.value = (int)obj["value"].n;
     return l;
 }
 public static JSONObject TOJSON(object obj)
 {
     //For hbPos generic guess
     if (touched.Add(obj))
     {
         var result = JSONObject.obj;
         //Fields
         var fieldinfo = obj.GetType().GetFields();
         foreach (var fi in fieldinfo)
         {
             var val = JSONObject.nullJO;
             if (!fi.GetValue(obj).Equals(null))
             {
                 var info = typeof(JSONTemplates).GetMethod("From" + fi.FieldType.Name);
                 if (info != null)
                 {
                     var parms = new object[1];
                     parms[0] = fi.GetValue(obj);
                     val = (JSONObject)info.Invoke(null, parms);
                 }
                 else if (fi.FieldType.Equals(typeof(string)))
                     val = new JSONObject { type = JSONObject.Type.STRING, str = fi.GetValue(obj).ToString() };
                 else
                     val = new JSONObject(fi.GetValue(obj).ToString());
             }
             if (val)
                 if (val.type != JSONObject.Type.NULL)
                     result.AddField(fi.Name, val);
                 else
                     Debug.LogWarning("Null for this non-null object, property " + fi.Name + " of class " + obj.GetType().Name + ". Object type is " + fi.FieldType.Name);
         }
         //Properties
         var propertyInfo = obj.GetType().GetProperties();
         foreach (var pi in propertyInfo)
         {
             //This section should mirror part of AssetFactory.AddScripts()
             var val = JSONObject.nullJO;
             if (!pi.GetValue(obj, null).Equals(null))
             {
                 var info = typeof(JSONTemplates).GetMethod("From" + pi.PropertyType.Name);
                 if (info != null)
                 {
                     var parms = new object[1];
                     parms[0] = pi.GetValue(obj, null);
                     val = (JSONObject)info.Invoke(null, parms);
                 }
                 else if (pi.PropertyType.Equals(typeof(string)))
                     val = new JSONObject { type = JSONObject.Type.STRING, str = pi.GetValue(obj, null).ToString() };
                 else
                     val = new JSONObject(pi.GetValue(obj, null).ToString());
             }
             if (val)
                 if (val.type != JSONObject.Type.NULL)
                     result.AddField(pi.Name, val);
                 else
                     Debug.LogWarning("Null for this non-null object, property " + pi.Name + " of class " + obj.GetType().Name + ". Object type is " + pi.PropertyType.Name);
         }
         return result;
     }
     Debug.LogWarning("trying to save the same data twice");
     return JSONObject.nullJO;
 }
 public static Color ToColor(JSONObject obj)
 {
     var c = new Color();
     for (var i = 0; i < obj.Count; i++)
         switch (obj.keys[i])
         {
             case "r":
                 c.r = obj[i].f;
                 break;
             case "g":
                 c.g = obj[i].f;
                 break;
             case "b":
                 c.b = obj[i].f;
                 break;
             case "hbPos":
                 c.a = obj[i].f;
                 break;
         }
     return c;
 }
 /*
  * Vector4
  */
 public static JSONObject FromVector4(Vector4 v)
 {
     var vdata = new JSONObject(JSONObject.Type.OBJECT);
     if (v.x != 0)
         vdata.AddField("x", v.x);
     if (v.y != 0)
         vdata.AddField("y", v.y);
     if (v.z != 0)
         vdata.AddField("z", v.z);
     if (v.w != 0)
         vdata.AddField("width", v.w);
     return vdata;
 }
        // If type is item, it has to be int, float, string, boolean
        private static void SetJSONItem(Type attrFieldType, JSONObject jsonObject, string data, FieldInfo info, object rootObject)
        {
            if (attrFieldType.IsEnum)
            {
                string valueStr = jsonObject.GetString(data);
                if (valueStr != null)
                {
                    try
                    {
                        object result = Enum.Parse(attrFieldType, valueStr);
                        info.SetValue(rootObject, result);
                    }catch (Exception) { }
                    return;
                }
            }
            else if (attrFieldType == typeof(int))
            {
                string valueStr = jsonObject.GetString(data);
                UnityEngine.Debug.Log(valueStr);
                if (valueStr != null)
                {
                    int intResult = 0;
                    if (int.TryParse(valueStr, out intResult) == true)
                    {
                        info.SetValue(rootObject, intResult);
                        return;
                    }
                }
                JSONValue value = jsonObject.GetValue(data);
                if (value != null)
                {
                    info.SetValue(rootObject, (int)value.Number);
                }
            }
            else if (attrFieldType == typeof(string))
            {
                string value = jsonObject.GetString(data);

                if (value != null)
                {
                    info.SetValue(rootObject, value);
                }
            }
            else if (attrFieldType == typeof(float))
            {
                string valueStr = jsonObject.GetString(data);
                if (valueStr != null)
                {
                    float floatResult = 0.0f;

                    if (float.TryParse(valueStr, out floatResult) == true)
                    {
                        info.SetValue(rootObject, floatResult);
                        return;
                    }
                }
                JSONValue value = jsonObject.GetValue(data);
                if (value != null)
                {
                    info.SetValue(rootObject, (float)value.Number); return;
                }
            }
            else if (attrFieldType == typeof(bool))
            {
                bool value = jsonObject.GetBoolean(data);
                info.SetValue(rootObject, value);
            }
            else if (attrFieldType == typeof(Color))
            {
                string value = jsonObject.GetString(data);
                if (value == null)
                {
                    return;
                }
                Color color = Color.white;
                if (OnlyHexInString(value) == true)
                {
                    color = HexToColor(value);
                    color = color / 255f;
                }
                else
                {
                    float [] f = SetVector(value, 4);
                    color = new Color(f[0] / 255f, f[1] / 255f, f[2] / 255f, f[3] / 255f);
                }
                info.SetValue(rootObject, color);
            }
            else if (attrFieldType == typeof(Vector2))
            {
                string value = jsonObject.GetString(data);
                if (value == null)
                {
                    return;
                }
                float [] f = SetVector(value, 2);
                Vector2  v = new Vector2(f[0], f[1]);
                info.SetValue(rootObject, v);
            }
            else if (attrFieldType == typeof(Vector3))
            {
                string value = jsonObject.GetString(data);
                if (value == null)
                {
                    return;
                }
                float [] f = SetVector(value, 3);
                Vector3  v = new Vector3(f[0], f[1], f[2]);
                info.SetValue(rootObject, v);
            }
            else if (attrFieldType == typeof(Vector4))
            {
                string value = jsonObject.GetString(data);
                if (value == null)
                {
                    return;
                }
                float [] f = SetVector(value, 4);
                Vector4  v = new Vector4(f[0], f[1], f[2], f[3]);
                info.SetValue(rootObject, v);
            }
        }
        /// <summary>
        /// Attempt to parse a string as a JSON array.
        /// </summary>
        /// <param name="jsonString"></param>
        /// <returns>A new JSONArray object if successful, null otherwise.</returns>
        public static JSONArray Parse(string jsonString)
        {
            var tempObject = JSONObject.Parse("{ \"array\" :" + jsonString + '}');

            return(tempObject == null ? null : tempObject.GetValue("array").Array);
        }
 public static RectOffset ToRectOffset(JSONObject obj)
 {
     var r = new RectOffset();
     for (var i = 0; i < obj.Count; i++)
         switch (obj.keys[i])
         {
             case "bottom":
                 r.bottom = (int)obj[i].n;
                 break;
             case "left":
                 r.left = (int)obj[i].n;
                 break;
             case "right":
                 r.right = (int)obj[i].n;
                 break;
             case "top":
                 r.top = (int)obj[i].n;
                 break;
         }
     return r;
 }
Beispiel #19
0
		public JSONObject(JSONObject[] objs)
		{
			type = Type.ARRAY;
			list = new List<JSONObject>(objs);
		}
 /*
  * Vector2
  */
 public static Vector2 ToVector2(JSONObject obj)
 {
     var x = obj["x"] ? obj["x"].f : 0;
     var y = obj["y"] ? obj["y"].f : 0;
     return new Vector2(x, y);
 }
Beispiel #21
0
		public void Add(JSONObject obj)
		{
			if (obj)
			{
				//Don't do anything if the object is null
				if (type != Type.ARRAY)
				{
					type = Type.ARRAY; //Congratulations, son, you're an ARRAY now
					if (list == null)
						list = new List<JSONObject>();
				}
				list.Add(obj);
			}
		}
 public static Vector3 ToVector3(JSONObject obj)
 {
     var x = obj["x"] ? obj["x"].f : 0;
     var y = obj["y"] ? obj["y"].f : 0;
     var z = obj["z"] ? obj["z"].f : 0;
     return new Vector3(x, y, z);
 }
Beispiel #23
0
		public void SetField(string name, JSONObject obj)
		{
			if (HasField(name))
			{
				list.Remove(this[name]);
				keys.Remove(name);
			}
			AddField(name, obj);
		}
 public static Vector4 ToVector4(JSONObject obj)
 {
     var x = obj["x"] ? obj["x"].f : 0;
     var y = obj["y"] ? obj["y"].f : 0;
     var z = obj["z"] ? obj["z"].f : 0;
     var w = obj["width"] ? obj["width"].f : 0;
     return new Vector4(x, y, z, w);
 }
Beispiel #25
0
		/// <summary>
		///     Merge object right into left recursively
		/// </summary>
		/// <param name="left">The left (base) object</param>
		/// <param name="right">The right (new) object</param>
		private static void MergeRecur(JSONObject left, JSONObject right)
		{
			if (left.type == Type.NULL)
				left.Absorb(right);
			else if (left.type == Type.OBJECT && right.type == Type.OBJECT)
				for (var i = 0; i < right.list.Count; i++)
				{
					var key = right.keys[i];
					if (right[i].isContainer)
						if (left.HasField(key))
							MergeRecur(left[key], right[i]);
						else
							left.AddField(key, right[i]);
					else if (left.HasField(key))
						left.SetField(key, right[i]);
					else
						left.AddField(key, right[i]);
				}
			else if (left.type == Type.ARRAY && right.type == Type.ARRAY)
			{
				if (right.Count > left.Count)
				{
					Debug.LogError("Cannot merge arrays when right object has more elements");
					return;
				}
				for (var i = 0; i < right.list.Count; i++)
					if (left[i].type == right[i].type) //Only overwrite with the same type
						if (left[i].isContainer)
							MergeRecur(left[i], right[i]);
						else
							left[i] = right[i];
			}
		}
 /*
  * Quaternion
  */
 public static JSONObject FromQuaternion(Quaternion q)
 {
     var qdata = new JSONObject(JSONObject.Type.OBJECT);
     if (q.w != 0)
         qdata.AddField("width", q.w);
     if (q.x != 0)
         qdata.AddField("x", q.x);
     if (q.y != 0)
         qdata.AddField("y", q.y);
     if (q.z != 0)
         qdata.AddField("z", q.z);
     return qdata;
 }