示例#1
0
    public static string Serialize(object message, Type type)
    {
        JSONOutStream jSONOutStream = new JSONOutStream();

        SerializeObject(jSONOutStream, type, message);
        return(jSONOutStream.Serialize());
    }
    /// <summary>
    /// 保存reslist.json的內容
    /// </summary>
    /// <param name="fileInfo"></param>
    /// <param name="data"></param>
    /// <param name="reslistVersion"></param>
    static private void SaveReslist(FileInfo fileInfo,
                                    Dictionary <string, AssetBundleInfo> data,
                                    int reslistVersion)
    {
        //生成新的json文件
        JSONOutStream jOutStream = new JSONOutStream();

        jOutStream.Content("version", reslistVersion)
        .List("assets");
        foreach (var pair in data)
        {
            AssetBundleInfo info = pair.Value;
            jOutStream.Start(0)
            .Content("assetbundlename", info.Path)
            .Content("version", info.Version)
            .Content("hash", info.Hash)
            .Content("size", info.Size)
            .Content("packaged", info.Packaged ? 1 : 0)
            .End();
        }

        jOutStream.End();
        //寫入reslist.json
        File.WriteAllText(fileInfo.FullName, jOutStream.Serialize());
    }
示例#3
0
    void ExampleBasicJSON()
    {
        // Let's serialize a simple address book

        JSONOutStream outStream = new JSONOutStream();

        outStream.Content("a", 1)
        .Content("b", 2)
        .Start("c")
        .Content("d", 3)
        .End();

        string serialized = outStream.Serialize();

        // serialized outputs this JSON structure:


        // {a:1, b: 2, c: {d: 3}}



        // Deserialize it
        JSONInStream inStream = new JSONInStream(serialized);
        int          a, b, d;

        inStream.Content("a", out a)
        .Content("b", out b)
        .Start("c")
        .Content("d", out d)
        .End();

        Debug.Log("SERIALIZED JSON STRING: " + serialized);
        Debug.Log("JSON DESERIALIZATION of a:" + a + ", b: " + b + ", d: " + d);
    }
示例#4
0
    public string Serialize()
    {
        JSONOutStream outStream = new JSONOutStream();

        SerializeData(outStream);
        return(outStream.Serialize());
    }
示例#5
0
  void ExampleBasicJSON()
  {
    // Let's serialize a simple address book

    JSONOutStream outStream = new JSONOutStream();
    outStream.Content("a", 1)
             .Content("b", 2)
             .Start("c")
              .Content("d", 3)
             .End();

    string serialized = outStream.Serialize();

    // serialized outputs this JSON structure:
  
  
    // {a:1, b: 2, c: {d: 3}}

    

    // Deserialize it
    JSONInStream inStream = new JSONInStream(serialized);
    int a, b, d;
    inStream.Content("a", out a)
            .Content("b", out b)
            .Start("c")
              .Content("d", out d)
            .End();

    Debug.Log("SERIALIZED JSON STRING: " + serialized);
    Debug.Log("JSON DESERIALIZATION of a:" + a + ", b: " + b + ", d: " + d );
  } 
示例#6
0
    public static string Serialize <T>(T message)
    {
        JSONOutStream jSONOutStream = new JSONOutStream();

        SerializeObject(jSONOutStream, typeof(T), message);
        return(jSONOutStream.Serialize());
    }
示例#7
0
 public override void SaveToJSON(JSONOutStream stream)
 {
     stream.List(jsonName);
     for (int i = 0; i < _propertyData.Count; ++i)
     {
         SaveArrayElement(i, stream);
     }
     stream.End();
 }
示例#8
0
 //---------------------------------------------------------------------------------
 // static Serialize
 //---------------------------------------------------------------------------------
 public static string Serialize <T>(T message)
 {
 #if UNITY_FLASH && !UNITY_EDITOR
     Debug.LogError("JSONSerializer do not work with Flash !");
     return(null);
 #else
     JSONOutStream stream = new JSONOutStream();
     SerializeObject(stream, typeof(T), message);
     return(stream.Serialize());
 #endif
 }
示例#9
0
    //---------------------------------------------------------------------------------
    // SerializeListElement
    //---------------------------------------------------------------------------------
    private static void SerializeListElement(JSONOutStream stream, Type type, object message, int i)
    {
        if (type.IsEnum)
        {
            stream.Content(i, (int)message);
            return;
        }

        switch (type.ToString())
        {
        case "System.String":
            stream.Content(i, (string)message);
            break;

        case "System.Single":
            stream.Content(i, (float)message);
            break;

        case "System.Int32":
            stream.Content(i, (int)message);
            break;

        case "System.Boolean":
            stream.Content(i, (bool)message);
            break;

        case "UnityEngine.Vector3":
            stream.Content(i, (Vector3)message);
            break;

        case "UnityEngine.Quaternion":
            stream.Content(i, (Quaternion)message);
            break;

        case "UnityEngine.Color":
            stream.Content(i, (Color)message);
            break;

        case "UnityEngine.Rect":
            stream.Content(i, (Rect)message);
            break;

        case "UnityEngine.Vector2":
            stream.Content(i, (Vector2)message);
            break;

        default:
            stream.Start(i);
            SerializeObject(stream, type, message);
            stream.End();
            break;
        }
    }
示例#10
0
    protected virtual void SerializeData(JSONOutStream stream)
    {
        playerName.SaveToJSON(stream);
        playerColor.SaveToJSON(stream);

        if (saveData != null)
        {
            stream.Start("saveData");
            saveData.SerializeData(stream);
            stream.End();
        }
    }
    public virtual void SerializeData(JSONOutStream stream)
    {
        if (!init)
        {
            UnityEngine.Debug.LogError("UH OH! you need to call InitSaveProperties in your custom PlayerSaveData! or it won't work!");
            return;
        }

        foreach (SavePropertyBase prop in saveProperties)
        {
            prop.SaveToJSON(stream);
        }
    }
示例#12
0
    private static void SerializeListElement(JSONOutStream stream, Type type, object message, int i)
    {
        //IL_0197: Unknown result type (might be due to invalid IL or missing references)
        //IL_01aa: Unknown result type (might be due to invalid IL or missing references)
        //IL_01bd: Unknown result type (might be due to invalid IL or missing references)
        //IL_01d0: Unknown result type (might be due to invalid IL or missing references)
        //IL_01e3: Unknown result type (might be due to invalid IL or missing references)
        if (!type.IsEnum)
        {
            switch (type.ToString())
            {
            case "System.String":
                stream.Content(i, (string)message);
                break;

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

            case "System.Double":
                stream.Content(i, (double)message);
                break;

            case "System.Int32":
                stream.Content(i, (int)message);
                break;

            case "System.Boolean":
                stream.Content(i, (bool)message);
                break;

            case "UnityEngine.Vector3":
                stream.Content(i, (Vector3)message);
                break;

            case "UnityEngine.Quaternion":
                stream.Content(i, (Quaternion)message);
                break;

            case "UnityEngine.Color":
                stream.Content(i, (Color)message);
                break;

            case "UnityEngine.Rect":
                stream.Content(i, (Rect)message);
                break;

            case "UnityEngine.Vector2":
                stream.Content(i, (Vector2)message);
                break;

            case "XorInt":
                stream.Content(i, new XorInt((int)message));
                break;

            case "XorUInt":
                stream.Content(i, new XorUInt((uint)message));
                break;

            case "XorFloat":
                stream.Content(i, new XorFloat((float)message));
                break;

            default:
                stream.Start(i);
                SerializeObject(stream, type, message);
                stream.End();
                break;
            }
        }
        else
        {
            stream.Content(i, (int)message);
        }
    }
示例#13
0
 public abstract void Serialize(JSONOutStream stream);
示例#14
0
    //---------------------------------------------------------------------------------
    // 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;
            }
        }
    }
示例#15
0
 public abstract void SaveToJSON(JSONOutStream stream);
示例#16
0
 protected abstract void SaveArrayElement(int index, JSONOutStream stream);
示例#17
0
 protected override void SaveArrayElement(int index, JSONOutStream stream)
 {
     stream.Content(_propertyData[index]);
 }
示例#18
0
 public override void SaveToJSON(JSONOutStream stream)
 {
     stream.Content(jsonName, _propertyData);
 }
示例#19
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;
                }
            }
        }
    }
示例#20
0
 public override void SaveToJSON(JSONOutStream stream)
 {
     stream.Start(jsonName);
     _propertyData.Serialize(stream);
     stream.End();
 }