public static JSONArray FromString(string str) { JSONArray result = new JSONArray(); str = str.Trim(); if (str.IsStrStartEndByChar('[', ']') == true) { str = str.CutOffFirstAndLast(); string[] objs = str.Split(','); if (objs.Length > 0) { foreach (string index in objs) { string indexStr = index.Trim(); Object obj = new object(); if (ClassFuncInjecter.TryParseObjFromStr(indexStr, out obj)) { result.Add(obj); } } } } return(result); }
public static JSONNode Deserialize(System.IO.BinaryReader aReader) { JSONBinaryTag type = (JSONBinaryTag)aReader.ReadByte(); switch (type) { case JSONBinaryTag.Array: { int count = aReader.ReadInt32(); JSONArray tmp = new JSONArray(); for (int i = 0; i < count; i++) { tmp.Add(Deserialize(aReader)); } return(tmp); } case JSONBinaryTag.Class: { int count = aReader.ReadInt32(); JSONClass tmp = new JSONClass(); for (int i = 0; i < count; i++) { string key = aReader.ReadString(); var val = Deserialize(aReader); tmp.Add(key, val); } return(tmp); } case JSONBinaryTag.Value: { return(new JSONData(aReader.ReadString())); } case JSONBinaryTag.IntValue: { return(new JSONData(aReader.ReadInt32())); } case JSONBinaryTag.DoubleValue: { return(new JSONData(aReader.ReadDouble())); } case JSONBinaryTag.BoolValue: { return(new JSONData(aReader.ReadBoolean())); } case JSONBinaryTag.FloatValue: { return(new JSONData(aReader.ReadSingle())); } default: { throw new Exception("Error deserializing JSON. Unknown tag: " + type); } } }
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; } } } } }