Exemplo n.º 1
0
        public TypeSerializationInfo(Type type, IEnumerable <PropertyInfo> properties)
        {
            Type     = type;
            TypeName = TypeHelper.FormatBinary(Type);

            IsAttribute = TypeHelper.IsSerializeAttribute(Type);
            if (IsAttribute)
            {
                Serialazer = TypeHelper.GetValueSerializer(type);
                return;
            }
            if (!Type.IsInterface && !Type.IsAbstract)
            {
                Constructor = EmitInvoker.Initialize(type, Type.EmptyTypes);
            }

            IsList = TypeHelper.IsList(type);
            if (IsList)
            {
                IsNamedList    = TypeHelper.IsInterface(type, typeof(INamedList));
                ListItemType   = TypeHelper.GetItemType(type);
                ListDefaulType = ListItemType != typeof(object) &&
                                 !ListItemType.IsInterface &&
                                 !type.IsGenericType &&
                                 !type.IsArray &&
                                 !TypeHelper.IsInterface(type, typeof(ISortable));
                ListItemIsAttribute = TypeHelper.IsSerializeAttribute(ListItemType);

                ListConstructor = EmitInvoker.Initialize(type, new[] { typeof(int) });
            }
            IsDictionary = TypeHelper.IsDictionary(type);

            Properties = new NamedList <PropertySerializationInfo>(6, (ListIndex <PropertySerializationInfo, string>)PropertySerializationInfo.NameInvoker.Instance.CreateIndex(false));
            Properties.Indexes.Add(PropertySerializationInfo.IsAttributeInvoker.Instance);
            int order = 0;

            foreach (var property in properties)
            {
                var exist = GetProperty(property.Name);
                if (TypeHelper.IsNonSerialize(property))
                {
                    if (exist != null)
                    {
                        Properties.Remove(exist);
                    }
                    continue;
                }
                //var method = property.GetGetMethod() ?? property.GetSetMethod();
                if (exist != null)// && method.Equals(method.GetBaseDefinition())
                {
                    Properties.Remove(exist);
                }

                Properties.Add(new PropertySerializationInfo(property, ++order));
            }
            Properties.ApplySortInternal(PropertySerializationInfo.OrderInvoker.Instance.CreateComparer <PropertySerializationInfo>());
        }
        public void Write(JsonWriter jwriter, object item, JsonSerializer serializer, TypeSerializationInfo info = null)
        {
            var type = item?.GetType();

            if (type == null || (info?.IsAttribute ?? TypeHelper.IsSerializeAttribute(type)))
            {
                serializer.Serialize(jwriter, item);
            }
            else if (item is IList list)
            {
                WriteArray(jwriter, list, serializer);
            }
            else
            {
                serializer.Serialize(jwriter, item, type);
            }
        }
Exemplo n.º 3
0
        public void Serialize(Utf8JsonWriter jwriter, object item, JsonSerializerOptions options, TypeSerializationInfo info = null)
        {
            var type = item?.GetType();

            if (type == null || (info?.IsAttribute ?? TypeHelper.IsSerializeAttribute(type)))
            {
                JsonSerializer.Serialize(jwriter, item, options);
            }
            else if (item is IList list)
            {
                SerializeArray(jwriter, list, options);
            }
            else
            {
                JsonSerializer.Serialize(jwriter, item, options);
            }
        }
Exemplo n.º 4
0
        public PropertySerializationInfo(PropertyInfo property, int order = -1)
        {
            Property = property;
            Name     = property.Name;
            DataType = Property.PropertyType;
            var keys = PropertySerializationInfoKeys.None;

            if (TypeHelper.IsSerializeText(property))
            {
                keys |= PropertySerializationInfoKeys.Text;
            }
            else if (TypeHelper.IsSerializeAttribute(property))
            {
                keys |= PropertySerializationInfoKeys.Attribute;
            }
            if (TypeHelper.IsSerializeWriteable(property))
            {
                keys |= PropertySerializationInfoKeys.Writeable;
            }
            if (TypeHelper.IsRequired(property))
            {
                keys |= PropertySerializationInfoKeys.Required;
            }
            if (TypeHelper.IsJsonSynchronized(property))
            {
                keys |= PropertySerializationInfoKeys.ChangeSensitive;
            }
            if (TypeHelper.IsReadOnly(property))
            {
                keys |= PropertySerializationInfoKeys.ReadOnly;
            }
            Keys    = keys;
            Order   = TypeHelper.GetOrder(property, order);
            Invoker = EmitInvoker.Initialize(property, true);

            Default = TypeHelper.GetDefault(property);
            if (IsAttribute || IsText)
            {
                Serialazer = TypeHelper.GetValueSerializer(property);
            }
        }