コード例 #1
0
 public override void SaveToJSON(JSONOutStream stream)
 {
     stream.List(jsonName);
     for (int i = 0; i < _propertyData.Count; ++i)
     {
         SaveArrayElement(i, stream);
     }
     stream.End();
 }
コード例 #2
0
    private static void SerializeObject(JSONOutStream stream, Type type, object message)
    {
        //IL_0216: Unknown result type (might be due to invalid IL or missing references)
        //IL_0234: Unknown result type (might be due to invalid IL or missing references)
        //IL_0252: Unknown result type (might be due to invalid IL or missing references)
        //IL_0270: Unknown result type (might be due to invalid IL or missing references)
        //IL_028e: Unknown result type (might be due to invalid IL or missing references)
        MethodInfo method = type.GetMethod("ToJSON");

        if (method != null)
        {
            method.Invoke(message, new object[1]
            {
                stream
            });
        }
        else
        {
            IEnumerable <FieldInfo> targetFields = GetTargetFields(type);
            foreach (FieldInfo item in targetFields)
            {
                switch (item.FieldType.ToString())
                {
                case "System.String":
                    stream.Content(GetName(item), (string)item.GetValue(message));
                    break;

                case "System.Single":
                    stream.Content(GetName(item), (XorFloat)(float)item.GetValue(message));
                    break;

                case "System.Double":
                    stream.Content(GetName(item), (double)item.GetValue(message));
                    break;

                case "System.Int32":
                    stream.Content(GetName(item), (int)item.GetValue(message));
                    break;

                case "System.Boolean":
                    stream.Content(GetName(item), (bool)item.GetValue(message));
                    break;

                case "UnityEngine.Vector3":
                    stream.Content(GetName(item), (Vector3)item.GetValue(message));
                    break;

                case "UnityEngine.Quaternion":
                    stream.Content(GetName(item), (Quaternion)item.GetValue(message));
                    break;

                case "UnityEngine.Color":
                    stream.Content(GetName(item), (Color)item.GetValue(message));
                    break;

                case "UnityEngine.Rect":
                    stream.Content(GetName(item), (Rect)item.GetValue(message));
                    break;

                case "UnityEngine.Vector2":
                    stream.Content(GetName(item), (Vector2)item.GetValue(message));
                    break;

                case "XorInt":
                    stream.Content(GetName(item), item.GetValue(message) as XorInt);
                    break;

                case "XorUInt":
                    stream.Content(GetName(item), item.GetValue(message) as XorUInt);
                    break;

                case "XorFloat":
                    stream.Content(GetName(item), item.GetValue(message) as XorFloat);
                    break;

                default:
                    if (item.FieldType.IsEnum)
                    {
                        stream.Content(GetName(item), item.GetValue(message).ToString());
                    }
                    else if (item.FieldType.IsGenericType)
                    {
                        Type         type2          = item.FieldType.GetGenericArguments()[0];
                        Type         typeFromHandle = typeof(List <>);
                        Type         type3          = typeFromHandle.MakeGenericType(type2);
                        PropertyInfo property       = type3.GetProperty("Count");
                        PropertyInfo property2      = type3.GetProperty("Item");
                        int          num            = (int)property.GetValue(item.GetValue(message), new object[0]);
                        stream.List(GetName(item));
                        for (int i = 0; i < num; i++)
                        {
                            object value = property2.GetValue(item.GetValue(message), new object[1]
                            {
                                i
                            });
                            SerializeListElement(stream, type2, value, i);
                        }
                        stream.End();
                    }
                    else if (item.FieldType.IsArray)
                    {
                        object[] array = ToObjectArray((IEnumerable)item.GetValue(message));
                        Type     type4 = Type.GetTypeArray(array)[0];
                        stream.List(GetName(item));
                        for (int j = 0; j < array.Length; j++)
                        {
                            object message2 = array[j];
                            SerializeListElement(stream, type4, message2, j);
                        }
                        stream.End();
                    }
                    else
                    {
                        stream.Start(GetName(item));
                        SerializeObject(stream, item.FieldType, item.GetValue(message));
                        stream.End();
                    }
                    break;
                }
            }
        }
    }
コード例 #3
0
ファイル: JSONSerializer.cs プロジェクト: savrang/dfFramework
    //---------------------------------------------------------------------------------
    // static SerializeObject
    //---------------------------------------------------------------------------------
    private static void SerializeObject(JSONOutStream stream, Type type, object message)
    {
        MethodInfo toJSON = type.GetMethod("ToJSON");

        if (toJSON != null)
        {
            toJSON.Invoke(message, new object[] { stream });
            return;
        }
        System.Reflection.FieldInfo[] fieldInfo = type.GetFields();
        foreach (System.Reflection.FieldInfo info in fieldInfo)
        {
            switch (info.FieldType.ToString())
            {
            case "System.String":
                stream.Content(info.Name, (string)info.GetValue(message));
                break;

            case "System.Single":
                stream.Content(info.Name, (float)info.GetValue(message));
                break;

            case "System.Int32":
                stream.Content(info.Name, (int)info.GetValue(message));
                break;

            case "System.Boolean":
                stream.Content(info.Name, (bool)info.GetValue(message));
                break;

            case "UnityEngine.Vector3":
                stream.Content(info.Name, (Vector3)info.GetValue(message));
                break;

            case "UnityEngine.Quaternion":
                stream.Content(info.Name, (Quaternion)info.GetValue(message));
                break;

            case "UnityEngine.Color":
                stream.Content(info.Name, (Color)info.GetValue(message));
                break;

            case "UnityEngine.Rect":
                stream.Content(info.Name, (Rect)info.GetValue(message));
                break;

            case "UnityEngine.Vector2":
                stream.Content(info.Name, (Vector2)info.GetValue(message));
                break;

            default:
                if (info.FieldType.IsEnum) // Enum
                {
                    stream.Content(info.Name, (string)info.GetValue(message).ToString());
                }
                else if (info.FieldType.IsGenericType) // List
                {
                    Type         containedType = info.FieldType.GetGenericArguments()[0];
                    Type         typeList      = typeof(List <>);
                    Type         actualType    = typeList.MakeGenericType(containedType);
                    PropertyInfo countMethod   = actualType.GetProperty("Count");
                    PropertyInfo itemMethod    = actualType.GetProperty("Item");
                    int          count         = (int)countMethod.GetValue(info.GetValue(message), new object[] {});
                    stream.List(info.Name);
                    for (int i = 0; i < count; ++i)
                    {
                        object o = itemMethod.GetValue(info.GetValue(message), new object[] { i });
                        SerializeListElement(stream, containedType, o, i);
                    }
                    stream.End();
                }
                else if (info.FieldType.IsArray) // Array
                {
                    object[] content       = ToObjectArray((IEnumerable)info.GetValue(message));
                    Type     containedType = Type.GetTypeArray(content)[0];
                    stream.List(info.Name);
                    for (int i = 0; i < content.Length; ++i)
                    {
                        object o = content[i];
                        SerializeListElement(stream, containedType, o, i);
                    }
                    stream.End();
                }
                else // object
                {
                    stream.Start(info.Name);
                    SerializeObject(stream, info.FieldType, info.GetValue(message));
                    stream.End();
                }
                break;
            }
        }
    }