Esempio n. 1
0
        public void PlayObject()
        {
            var playObj = new PlayObject {
                ["i"]   = 123,
                ["b"]   = true,
                ["str"] = "hello, world"
            };
            var subPlayObj = new PlayObject {
                ["si"]   = 345,
                ["sb"]   = true,
                ["sstr"] = "code"
            };

            playObj.Add("obj", subPlayObj);
            var subPlayArr = new PlayArray {
                666, true, "engineer"
            };

            playObj.Add("arr", subPlayArr);
            var genericValue = CodecUtils.Serialize(playObj);

            Debug.Log(genericValue);
            var newPlayObj = CodecUtils.Deserialize(genericValue) as PlayObject;

            Assert.AreEqual(newPlayObj["i"], 123);
            Assert.AreEqual(newPlayObj["b"], true);
            Assert.AreEqual(newPlayObj["str"], "hello, world");
            var newSubPlayObj = newPlayObj["obj"] as PlayObject;

            Assert.AreEqual(newSubPlayObj["si"], 345);
            Assert.AreEqual(newSubPlayObj["sb"], true);
            Assert.AreEqual(newSubPlayObj["sstr"], "code");
            var newSubPlayArr = newPlayObj["arr"] as PlayArray;

            Assert.AreEqual(newSubPlayArr[0], 666);
            Assert.AreEqual(newSubPlayArr[1], true);
            Assert.AreEqual(newSubPlayArr[2], "engineer");
            // Dictionary to PlayObject
            var dict = new Dictionary <string, int> {
                { "hello", 123 },
                { "world", 456 }
            };
            var dictObj = new PlayObject(dict);

            Assert.AreEqual(dictObj["hello"], 123);
            Assert.AreEqual(dictObj["world"], 456);
        }
Esempio n. 2
0
        public void PlayArray()
        {
            var playArr = new PlayArray {
                123, true, "hello, world",
                new PlayObject {
                    ["i"]   = 23,
                    ["b"]   = true,
                    ["str"] = "hello"
                }
            };
            var genericValue = CodecUtils.Serialize(playArr);

            Debug.Log(genericValue);
            var newPlayArr = CodecUtils.Deserialize(genericValue) as PlayArray;

            Assert.AreEqual(newPlayArr[0], 123);
            Assert.AreEqual(newPlayArr[1], true);
            Assert.AreEqual(newPlayArr[2], "hello, world");
            var subPlayObj = newPlayArr[3] as PlayObject;

            Assert.AreEqual(subPlayObj["i"], 23);
            Assert.AreEqual(subPlayObj["b"], true);
            Assert.AreEqual(subPlayObj["str"], "hello");
            // List to PlayArray
            var iList = new List <int> {
                10, 24
            };
            var iArr = new PlayArray(iList);

            Assert.AreEqual(iArr[0], 10);
            Assert.AreEqual(iArr[1], 24);
            var sList = new List <string> {
                "hello", "world"
            };
            var sArr = new PlayArray(sList);

            Assert.AreEqual(sArr[0], "hello");
            Assert.AreEqual(sArr[1], "world");
        }
Esempio n. 3
0
        /// <summary>
        /// 反序列化
        /// </summary>
        /// <returns>The decode.</returns>
        /// <param name="genericValue">带类型的序列化对象</param>
        public static object Deserialize(GenericCollectionValue genericValue)
        {
            object val = null;

            switch (genericValue.Type)
            {
            case GenericCollectionValue.Types.Type.Null:
                // val = null;
                break;

            case GenericCollectionValue.Types.Type.Bytes:
                val = genericValue.BytesValue.ToByteArray();
                break;

            case GenericCollectionValue.Types.Type.Byte:
                val = (byte)genericValue.IntValue;
                break;

            case GenericCollectionValue.Types.Type.Short:
                val = (short)genericValue.IntValue;
                break;

            case GenericCollectionValue.Types.Type.Int:
                val = genericValue.IntValue;
                break;

            case GenericCollectionValue.Types.Type.Long:
                val = genericValue.LongIntValue;
                break;

            case GenericCollectionValue.Types.Type.Bool:
                val = genericValue.BoolValue;
                break;

            case GenericCollectionValue.Types.Type.Float:
                val = genericValue.FloatValue;
                break;

            case GenericCollectionValue.Types.Type.Double:
                val = genericValue.DoubleValue;
                break;

            case GenericCollectionValue.Types.Type.String:
                val = genericValue.StringValue;
                break;

            case GenericCollectionValue.Types.Type.Map:
                val = DeserializePlayObject(genericValue.BytesValue);
                break;

            case GenericCollectionValue.Types.Type.Array: {
                PlayArray playArray  = new PlayArray();
                var       collection = GenericCollection.Parser.ParseFrom(genericValue.BytesValue);
                foreach (var element in collection.ListValue)
                {
                    playArray.Add(Deserialize(element));
                }
                val = playArray;
            }
            break;

            case GenericCollectionValue.Types.Type.Object: {
                // 自定义类型
                var typeId = genericValue.ObjectTypeId;
                if (typeIdDict.TryGetValue(typeId, out var customType))
                {
                    val = customType.DeserializeMethod(genericValue.BytesValue.ToByteArray());
                }
                else
                {
                    throw new Exception($"type id: {typeId} is not supported");
                }
            }
            break;

            default:
                // 异常
                throw new Exception($"{genericValue.Type} is not supported");
            }
            return(val);
        }