コード例 #1
0
        public static string SerializeJson(object obj)
        {
            var        writer     = new Siren.Protocol.Json.JsonWriter();
            Serializer serializer = new Serializer(writer);

            serializer.SerializeHelper(obj, SirenFactory.FindClass(obj.GetType()));
            return(writer.FlushToString());
        }
コード例 #2
0
        public static ArraySegment <byte> Serialize <TWriter>(object obj)
            where TWriter : BaseProtocolWriter, new()
        {
            var        writer     = new TWriter();
            Serializer serializer = new Serializer(writer);

            serializer.SerializeHelper(obj, SirenFactory.FindClass(obj.GetType()));
            return(writer.ToBuffer());
        }
コード例 #3
0
ファイル: SirenClass.cs プロジェクト: johndpope/Medusa
 public ushort GetBasePropertyCount()
 {
     if (Type.BaseType != typeof(object))
     {
         var baseClass = SirenFactory.FindClass(Type.BaseType);
         if (baseClass != null)
         {
             return((ushort)(baseClass.GetBasePropertyCount() + (ushort)baseClass.Properties.Count));
         }
     }
     return(0);
 }
コード例 #4
0
        public static T Deserialize <T, TReader>(ArraySegment <byte> data)
            where T : class, new()
            where TReader : BaseProtocolReader, new()
        {
            var reader = new TReader();

            reader.Accept(data);
            Deserializer deserializer = new Deserializer(reader);

            object obj = new T();

            deserializer.DeserializeHelper(typeof(T), ref obj, SirenFactory.FindClass(typeof(T)));
            return((T)obj);
        }
コード例 #5
0
        public static T DeserializeJson <T>(string str)
            where T : class, new()
        {
            var reader = new Siren.Protocol.Json.JsonReader();

            reader.Accept(str);
            Deserializer deserializer = new Deserializer(reader);

            object obj = new T();

            deserializer.DeserializeHelper(typeof(T), ref obj, SirenFactory.FindClass(typeof(T)));
            return((T)obj);


            //JsonSerializerSettings settings = new JsonSerializerSettings
            //{
            //    ContractResolver = Serializer.SirenContractResolver.Instance
            //};
            //return JsonConvert.DeserializeObject<T>(str, settings);
        }
コード例 #6
0
ファイル: SirenClass.cs プロジェクト: johndpope/Medusa
        public bool Initialzie()
        {
            //get template
            Template = SirenFactory.Create(Attribute.Template) as ISirenTemplate;
            if (Template == null)
            {
                return(false);
            }

            if (Type.IsEnum)
            {
                return(true);
            }

            //get methods
            SerializeMethodInfo   = Type.GetMethod("Serialize", BindingFlags.Public | BindingFlags.Instance);
            DeserializeMethodInfo = Type.GetMethod("Deserialize", BindingFlags.Public | BindingFlags.Instance);
            RegisterMethodInfo    = Type.GetMethod("Register", BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
            if (RegisterMethodInfo != null && !Type.ContainsGenericParameters)
            {
                RegisterMethodInfo.Invoke(null, null);
            }


            //get properties
            uint   index      = 0;
            ushort id         = GetBasePropertyCount();
            var    properties = Type.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);

            foreach (var propertyInfo in properties)
            {
                SirenPropertyAttribute propertyAttribute = propertyInfo.GetCustomAttribute <SirenPropertyAttribute>();
                if (propertyAttribute != null)
                {
                    SirenProperty property = new SirenProperty(this, propertyInfo, propertyAttribute, index++, id++);
                    Properties.Add(property);
                    PropertyIdDict.Add(property.Id, property);
                    PropertyNameDict.Add(property.Name, property);
                }
            }

            if (Properties.Count == 0)
            {
                if (Type.BaseType == typeof(object) || Type.BaseType.IsValueType)
                {
                    return(false);
                }
            }


            //get include types
            foreach (var property in Properties)
            {
                var type = property.Type;
                if (type.IsGenericType)
                {
                    if (type.Name.StartsWith("List"))
                    {
                        var valueType = type.GenericTypeArguments[0];
                        AddIncludeType(valueType);
                    }
                    else if (type.Name.StartsWith("Dictionary"))
                    {
                        var keyType   = type.GenericTypeArguments[0];
                        var valueType = type.GenericTypeArguments[1];
                        AddIncludeType(keyType);
                        AddIncludeType(valueType);
                    }
                    else
                    {
                        AddIncludeType(type);
                    }
                }
                else
                {
                    AddIncludeType(type);
                }
            }

            if (Type.BaseType != typeof(object) && !Type.BaseType.IsValueType)
            {
                //has base type
                if (!IncludeTypes.Contains(Type.BaseType))
                {
                    IncludeTypes.Add(Type.BaseType);
                }

                BaseSirenClass = SirenFactory.FindClass(Type.BaseType);
                if (BaseSirenClass != null)
                {
                    //add base properties
                    foreach (var sirenProperty in BaseSirenClass.PropertyIdDict)
                    {
                        PropertyIdDict.Add(sirenProperty.Key, sirenProperty.Value);
                    }

                    foreach (var sirenProperty in BaseSirenClass.PropertyNameDict)
                    {
                        PropertyNameDict.Add(sirenProperty.Key, sirenProperty.Value);
                    }
                }
            }


            return(true);
        }
コード例 #7
0
        public SirenProperty(SirenClass parentSirenClass, PropertyInfo info, SirenPropertyAttribute attribute, uint index, ushort id)
        {
            ParentSirenClass = parentSirenClass;
            Info             = info;
            Id         = id;
            Attribute  = attribute;
            Index      = index;
            FieldType  = SirenPropertyFieldType.Struct;
            DataType   = SirenFactory.GetDataType(Type);
            MethodType = SirenPropertyMethodType.Unsupported;


            if (Type.IsGenericType)
            {
                if (Type.Name.StartsWith("List"))
                {
                    MethodType = SirenPropertyMethodType.List;
                    FieldType  = SirenPropertyFieldType.List;

                    ValueType       = Type.GenericTypeArguments[0];
                    ValueSirenClass = SirenFactory.FindClass(ValueType);
                }
                else if (Type.Name.StartsWith("Dictionary"))
                {
                    MethodType = SirenPropertyMethodType.Dictionary;
                    FieldType  = SirenPropertyFieldType.Dictionary;

                    KeyType         = Type.GenericTypeArguments[0];
                    ValueType       = Type.GenericTypeArguments[1];
                    KeySirenClass   = SirenFactory.FindClass(KeyType);
                    ValueSirenClass = SirenFactory.FindClass(ValueType);
                }
                else
                {
                    ValueSirenClass = SirenFactory.FindClass(Type);

                    if (Attribute.Modifier == SirenPropertyModifier.Required)
                    {
                        MethodType = SirenPropertyMethodType.Value;
                        FieldType  = SirenPropertyFieldType.Struct;
                    }
                    else
                    {
                        MethodType = SirenPropertyMethodType.Pointer;
                        FieldType  = SirenPropertyFieldType.Pointer;
                    }
                }

                DefaultValueString = String.Empty;
            }
            else
            {
                if (Type.IsValueType || Type == typeof(string) || Type == typeof(byte[]))
                {
                    MethodType = SirenPropertyMethodType.Value;

                    if (Type == typeof(string))
                    {
                        FieldType = SirenPropertyFieldType.String;
                    }
                    else if (Type == typeof(byte[]))
                    {
                        FieldType = SirenPropertyFieldType.Blob;
                    }
                    else
                    {
                        FieldType = SirenPropertyFieldType.Value;
                    }
                }
                else
                {
                    if (Attribute.Modifier == SirenPropertyModifier.Required)
                    {
                        MethodType = SirenPropertyMethodType.Value;
                        FieldType  = SirenPropertyFieldType.Struct;
                    }
                    else
                    {
                        MethodType = SirenPropertyMethodType.Pointer;
                        FieldType  = SirenPropertyFieldType.Pointer;
                    }

                    ValueSirenClass = SirenFactory.FindClass(Type);
                }

                DefaultValueString = String.Empty;
                if (Type.IsValueType)
                {
                    if (Attribute.DefaultValue != null)
                    {
                        if (Type.IsEnum)
                        {
                            DefaultValueString = String.Format("({0}){1}", Type.Name, Convert.ToUInt32(Attribute.DefaultValue));
                        }
                        else
                        {
                            DefaultValueString = Attribute.DefaultValue.ToString();
                        }
                    }
                    else
                    {
                        if (Type == typeof(bool))
                        {
                            DefaultValueString = "false";
                        }
                        else if (Type == typeof(float))
                        {
                            DefaultValueString = "0.f";
                        }
                        else if (Type == typeof(double))
                        {
                            DefaultValueString = "0.0";
                        }
                        else
                        {
                            if (Type.IsEnum)
                            {
                                DefaultValueString = String.Format("({0})0", Type.Name);
                            }
                            else
                            {
                                DefaultValueString = "0";
                            }
                        }
                    }
                }
            }
        }