Ejemplo n.º 1
0
        private static void Serialize(object obj, Packer packer)
        {
            if (obj == null)
            {
                packer.PackNull();

                return;
            }

            var type = obj.GetType();

            if (type.IsSimpleType())
            {
                packer.Pack(obj);
            }
            else if (obj is IDictionary)
            {
                var dict = (IDictionary)obj;

                packer.PackMapHeader(dict.Count);

                foreach (var key in dict.Keys)
                {
                    Serialize(key, packer);
                    Serialize(dict[key], packer);
                }
            }
            else if (obj is IDictionary <string, object> ) // special case for ExpandoObject
            {
                var dict = (IDictionary <string, object>)obj;

                packer.PackMapHeader(dict.Count);

                foreach (var kvp in dict)
                {
                    Serialize(kvp.Key, packer);
                    Serialize(kvp.Value, packer);
                }
            }
            else if (obj is IList)
            {
                var list = (IList)obj;

                packer.PackArrayHeader(list.Count);

                foreach (var item in list)
                {
                    Serialize(item, packer);
                }
            }
            else if (obj is IEnumerable enu)
            {
                var list = new List <object>();

                foreach (var item in enu)
                {
                    list.Add(item);
                }

                packer.PackArrayHeader(list.Count);

                list.ForEach(a => Serialize(a, packer));
            }
            else if (obj is IPackable packable)
            {
                packable.PackToMessage(packer, null);
            }
            else if (obj is Delegate deleg)
            {
                var serializer = new DelegateSerializer();
                serializer.PackTo(packer, deleg);
            }
            else
            {
                var properties = type.GetProperties();
                var dict       = new Dictionary <string, object>();

                foreach (var property in properties)
                {
                    dict[property.Name] = property.GetValue(obj, null);
                }

                Serialize(dict, packer);
            }
        }
Ejemplo n.º 2
0
        private static void Serialize(object obj, Packer packer)
        {
            if (obj == null)
            {
                packer.PackNull();

                return;
            }

            var type = obj.GetType();

            if (type.IsSimpleType())
            {
                packer.Pack(obj);
            }
            else if (type.IsEnum)
            {
                var eTypeInfo  = type.GetTypeInfo();
                var eValueType = eTypeInfo.DeclaredFields.First(x => x.Name == "value__").FieldType;
                var eAsNumber  = Convert.ChangeType(obj, eValueType);
                packer.Pack(eAsNumber);
            }
            else if (obj is byte[] bytes)
            {
                packer.Pack(bytes);
            }
            else if (obj is IDictionary)
            {
                var dict = (IDictionary)obj;

                packer.PackMapHeader(dict.Count);

                foreach (var key in dict.Keys)
                {
                    Serialize(key, packer);
                    Serialize(dict[key], packer);
                }
            }
            else if (obj is IDictionary <string, object> ) // special case for ExpandoObject
            {
                var dict = (IDictionary <string, object>)obj;

                packer.PackMapHeader(dict.Count);

                foreach (var kvp in dict)
                {
                    Serialize(kvp.Key, packer);
                    Serialize(kvp.Value, packer);
                }
            }
            else if (obj is IList)
            {
                var list = (IList)obj;

                packer.PackArrayHeader(list.Count);

                foreach (var item in list)
                {
                    Serialize(item, packer);
                }
            }
            else if (obj is IEnumerable enu)
            {
                var list = new List <object>();

                foreach (var item in enu)
                {
                    list.Add(item);
                }

                packer.PackArrayHeader(list.Count);

                list.ForEach(a => Serialize(a, packer));
            }
            else if (obj is IPackable packable)
            {
                packable.PackToMessage(packer, null);
            }
            else if (obj is Delegate deleg)
            {
                var serializer = new DelegateSerializer();
                serializer.PackTo(packer, deleg);
            }
            else if (obj is Vector2 vec2)
            {
                var serializer = new Vector2Serializer();
                serializer.PackTo(packer, vec2);
            }
            else if (obj is Vector3 vec3)
            {
                var serializer = new Vector3Serializer();
                serializer.PackTo(packer, vec3);
            }
            else if (obj is Vector4 vec4)
            {
                var serializer = new Vector4Serializer();
                serializer.PackTo(packer, vec4);
            }
            else if (obj is Quaternion quat)
            {
                var serializer = new QuaternionSerializer();
                serializer.PackTo(packer, quat);
            }
            else
            {
                var properties = type.GetProperties();
                var dict       = new Dictionary <string, object>();

                foreach (var property in properties)
                {
                    dict[property.Name] = property.GetValue(obj, null);
                }

                Serialize(dict, packer);
            }
        }