Ejemplo n.º 1
0
        public static object Deserialize(Type rootType, string json)
        {
            object     rootObject = Activator.CreateInstance(rootType);
            JSONObject jsonObject = JSONObject.Parse(json);             // parse JSON file into JSONObject

            if (json == null)
            {
                return(null);
            }
            // Iterate all members of root object
            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)
                {
                    Type      attrFieldType = attrField.type;               // Get the type of the field
                    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 value = attrField.attrType;
                    // Type is either object, item or array.
                    switch (value)
                    {
                    case AttrEnum.AttrObject:
                        JSONObject jsonChildObject = jsonObject.GetObject(data);
                        if (jsonChildObject == null)
                        {
                            continue;
                        }
                        info.SetValue(rootObject, Deserialize(attrFieldType, jsonObject.GetObject(data).ToString()));
                        break;

                    case AttrEnum.AttrItem:
                        SetJSONItem(attrFieldType, jsonObject, data, info, rootObject);
                        break;

                    case AttrEnum.AttrArray:
                        JSONArray jsonArray = jsonObject.GetArray(data);
                        if (jsonArray == null)
                        {
                            continue;
                        }
                        object o = Array.CreateInstance(attrFieldType, jsonArray.Length);
                        SetJSONArray(attrFieldType, jsonArray, o);
                        info.SetValue(rootObject, o);
                        break;
                    }
                }
            }
            return(rootObject);
        }
Ejemplo n.º 2
0
        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;
                    }
                    }
                }
            }
        }
Ejemplo n.º 3
0
 internal JSONAttribute(string data, Type type, AttrEnum attrType)
 {
     this.type     = type;
     this.data     = data;
     this.attrType = attrType;
 }