Esempio n. 1
0
    public override void ParseJSON(JSONInStream stream)
    {
        if (stream == null || stream.node == null)
        {
            return;
        }

        _propertyData.Clear();
        stream.List(jsonName, (int arg1, JSONInStream subStream) =>
        {
            T obj = ParseArrayElement(subStream);
            if (obj != null)
            {
                _propertyData.Add(obj);
            }
        });
    }
Esempio n. 2
0
    private static object DeserializeObject(JSONInStream stream, Type type)
    {
        //IL_0291: Unknown result type (might be due to invalid IL or missing references)
        //IL_02c4: Unknown result type (might be due to invalid IL or missing references)
        //IL_02f7: Unknown result type (might be due to invalid IL or missing references)
        //IL_032a: Unknown result type (might be due to invalid IL or missing references)
        //IL_035d: Unknown result type (might be due to invalid IL or missing references)
        MethodInfo method = type.GetMethod("FromJSON");

        if (method != null)
        {
            return(method.Invoke(null, new object[1]
            {
                stream
            }));
        }
        object obj = Activator.CreateInstance(type);
        IEnumerable <FieldInfo> targetFields = GetTargetFields(type);

        foreach (FieldInfo item in targetFields)
        {
            switch (item.FieldType.ToString())
            {
            case "System.String":
                if (stream.Has(GetName(item)))
                {
                    stream.Content(GetName(item), out string value7);
                    item.SetValue(obj, value7);
                }
                break;

            case "System.Single":
                if (stream.Has(GetName(item)))
                {
                    stream.Content(GetName(item), out float value12);
                    item.SetValue(obj, value12);
                }
                break;

            case "System.Double":
                if (stream.Has(GetName(item)))
                {
                    stream.Content(GetName(item), out double value16);
                    item.SetValue(obj, value16);
                }
                break;

            case "System.Int32":
                if (stream.Has(GetName(item)))
                {
                    stream.Content(GetName(item), out int value9);
                    item.SetValue(obj, value9);
                }
                break;

            case "System.Boolean":
                if (stream.Has(GetName(item)))
                {
                    stream.Content(GetName(item), out bool value5);
                    item.SetValue(obj, value5);
                }
                break;

            case "UnityEngine.Vector3":
                if (stream.Has(GetName(item)))
                {
                    stream.Content(GetName(item), out Vector3 value14);
                    item.SetValue(obj, value14);
                }
                break;

            case "UnityEngine.Quaternion":
                if (stream.Has(GetName(item)))
                {
                    stream.Content(GetName(item), out Quaternion value10);
                    item.SetValue(obj, value10);
                }
                break;

            case "UnityEngine.Color":
                if (stream.Has(GetName(item)))
                {
                    stream.Content(GetName(item), out Color value8);
                    item.SetValue(obj, value8);
                }
                break;

            case "UnityEngine.Rect":
                if (stream.Has(GetName(item)))
                {
                    stream.Content(GetName(item), out Rect value6);
                    item.SetValue(obj, value6);
                }
                break;

            case "UnityEngine.Vector2":
                if (stream.Has(GetName(item)))
                {
                    stream.Content(GetName(item), out Vector2 value4);
                    item.SetValue(obj, value4);
                }
                break;

            case "XorInt":
                if (stream.Has(GetName(item)))
                {
                    stream.Content(GetName(item), out XorInt value15);
                    item.SetValue(obj, value15);
                }
                break;

            case "XorUInt":
                if (stream.Has(GetName(item)))
                {
                    stream.Content(GetName(item), out XorUInt value13);
                    item.SetValue(obj, value13);
                }
                break;

            case "XorFloat":
                if (stream.Has(GetName(item)))
                {
                    stream.Content(GetName(item), out XorFloat value11);
                    item.SetValue(obj, value11);
                }
                break;

            default:
                if (stream.Has(GetName(item)))
                {
                    if (item.FieldType.IsEnum)
                    {
                        stream.Content(GetName(item), out string value);
                        item.SetValue(obj, Enum.Parse(item.FieldType, value));
                    }
                    else if (item.FieldType.IsGenericType)
                    {
                        Type       containedType  = item.FieldType.GetGenericArguments()[0];
                        Type       typeFromHandle = typeof(List <>);
                        Type       type2          = typeFromHandle.MakeGenericType(containedType);
                        MethodInfo addMethod      = type2.GetMethod("Add");
                        object     list           = Activator.CreateInstance(type2);
                        stream.List(GetName(item), delegate(int i, JSONInStream stream2)
                        {
                            object obj3 = DeserializeListElement(stream2, containedType);
                            addMethod.Invoke(list, new object[1]
                            {
                                obj3
                            });
                        });
                        item.SetValue(obj, list);
                    }
                    else if (item.FieldType.IsArray)
                    {
                        Type       containedType2  = item.FieldType.GetElementType();
                        Type       typeFromHandle2 = typeof(List <>);
                        Type       type3           = typeFromHandle2.MakeGenericType(containedType2);
                        MethodInfo addMethod2      = type3.GetMethod("Add");
                        MethodInfo method2         = type3.GetMethod("ToArray");
                        object     list2           = Activator.CreateInstance(type3);
                        stream.List(GetName(item), delegate(int i, JSONInStream stream2)
                        {
                            object obj2 = DeserializeListElement(stream2, containedType2);
                            addMethod2.Invoke(list2, new object[1]
                            {
                                obj2
                            });
                        });
                        object value2 = method2.Invoke(list2, new object[0]);
                        item.SetValue(obj, value2);
                    }
                    else
                    {
                        stream.Start(GetName(item));
                        object value3 = DeserializeObject(stream, item.FieldType);
                        stream.End();
                        item.SetValue(obj, value3);
                    }
                }
                break;
            }
        }
        return(obj);
    }
Esempio n. 3
0
    //---------------------------------------------------------------------------------
    //  DeserializeObject
    //---------------------------------------------------------------------------------
    private static object DeserializeObject(JSONInStream stream, Type type)
    {
        MethodInfo fromJSON = type.GetMethod("FromJSON");

        if (fromJSON != null)
        {
            return(fromJSON.Invoke(null, new object[] { stream }));
        }

        object data = Activator.CreateInstance(type);

        System.Reflection.FieldInfo[] fieldInfo = type.GetFields();
        foreach (System.Reflection.FieldInfo info in fieldInfo)
        {
            switch (info.FieldType.ToString())
            {
            case "System.String":
                string resS;
                if (stream.Has(info.Name))
                {
                    stream.Content(info.Name, out resS);
                    info.SetValue(data, resS);
                }
                break;

            case "System.Single":
                float resF;
                if (stream.Has(info.Name))
                {
                    stream.Content(info.Name, out resF);
                    info.SetValue(data, resF);
                }
                break;

            case "System.Int32":
                int resI;
                if (stream.Has(info.Name))
                {
                    stream.Content(info.Name, out resI);
                    info.SetValue(data, resI);
                }
                break;

            case "System.Boolean":
                bool resB;
                if (stream.Has(info.Name))
                {
                    stream.Content(info.Name, out resB);
                    info.SetValue(data, resB);
                }
                break;

            case "UnityEngine.Vector3":
                Vector3 resV;
                if (stream.Has(info.Name))
                {
                    stream.Content(info.Name, out resV);
                    info.SetValue(data, resV);
                }
                break;

            case "UnityEngine.Quaternion":
                Quaternion resQ;
                if (stream.Has(info.Name))
                {
                    stream.Content(info.Name, out resQ);
                    info.SetValue(data, resQ);
                }
                break;

            case "UnityEngine.Color":
                Color resC;
                if (stream.Has(info.Name))
                {
                    stream.Content(info.Name, out resC);
                    info.SetValue(data, resC);
                }
                break;

            case "UnityEngine.Rect":
                Rect resR;
                if (stream.Has(info.Name))
                {
                    stream.Content(info.Name, out resR);
                    info.SetValue(data, resR);
                }
                break;

            case "UnityEngine.Vector2":
                Vector2 resV2;
                if (stream.Has(info.Name))
                {
                    stream.Content(info.Name, out resV2);
                    info.SetValue(data, resV2);
                }
                break;

            default:
                if (stream.Has(info.Name))
                {
                    if (info.FieldType.IsEnum) // Enum
                    {
                        string e;
                        stream.Content(info.Name, out e);
                        info.SetValue(data, Enum.Parse(info.FieldType, e));
                    }
                    else if (info.FieldType.IsGenericType) // can only be a List
                    {
                        Type       containedType = info.FieldType.GetGenericArguments()[0];
                        Type       typeList      = typeof(List <>);
                        Type       actualType    = typeList.MakeGenericType(containedType);
                        MethodInfo addMethod     = actualType.GetMethod("Add");
                        object     list          = Activator.CreateInstance(actualType);
                        stream.List(info.Name, delegate(int i, JSONInStream stream2){
                            object o = DeserializeListElement(stream2, containedType);
                            addMethod.Invoke(list, new object[] { o });
                        });
                        info.SetValue(data, list);
                    }
                    else if (info.FieldType.IsArray) // Array
                    {
                        Type       containedType = info.FieldType.GetElementType();
                        Type       typeList      = typeof(List <>);
                        Type       actualType    = typeList.MakeGenericType(containedType);
                        MethodInfo addMethod     = actualType.GetMethod("Add");
                        MethodInfo toArrayMethod = actualType.GetMethod("ToArray");
                        object     list          = Activator.CreateInstance(actualType);
                        stream.List(info.Name, delegate(int i, JSONInStream stream2){
                            object o = DeserializeListElement(stream2, containedType);
                            addMethod.Invoke(list, new object[] { o });
                        });
                        object array = toArrayMethod.Invoke(list, new object[] {});
                        info.SetValue(data, array);
                    }
                    else // Embedded Object
                    {
                        stream.Start(info.Name);
                        object created = DeserializeObject(stream, info.FieldType);
                        stream.End();
                        info.SetValue(data, created);
                    }
                }
                break;
            }
        }
        return(data);
    }