Esempio n. 1
0
        public Serializer(Type type)
        {
            var info = SerializationInfo[type] = new TypeSerializationInfo(type);

            if (info.IsList)
            {
                SerializationInfo[info.ListItemType] = new TypeSerializationInfo(info.ListItemType);
            }
        }
Esempio n. 2
0
 public void WriteCollection(ICollection collection, TypeSerializationInfo type)
 {
     foreach (object item in collection)
     {
         if (item == null)
         {
             continue;
         }
         Write(item, "i", type.ListItemType != item.GetType());
     }
 }
Esempio n. 3
0
 public Client(Invoker <T, K?> idInvoker, Invoker <T, int?> typeInvoker, int typeId = 0)
 {
     IdInvoker = idInvoker;
     Items.Indexes.Add(IdInvoker);
     TypeInvoker       = typeInvoker;
     TypeId            = typeId;
     SerializationInfo = new TypeSerializationInfo(typeof(T));
     if (typeId == 0)
     {
         downloads = new ConcurrentDictionary <K, T>();
     }
 }
Esempio n. 4
0
 public TypeSerializationInfo GetTypeInfo(Type type)
 {
     if (type == null)
     {
         return(null);
     }
     if (!SerializationInfo.TryGetValue(type, out var info))
     {
         SerializationInfo[type] = info = new TypeSerializationInfo(type, OnlyXmlAttributes);
     }
     return(info);
 }
        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);
            }
        }
Esempio n. 6
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);
            }
        }
Esempio n. 7
0
        public void Write(object element, TypeSerializationInfo info, string name, bool writeType)
        {
            //Debug.WriteLine($"Xml Write {name}");
            if (writeType)
            {
                WriteType(info);
            }
            WriteBegin(name);
            if (info.IsAttribute)
            {
                Writer.WriteValue(info.TextFormat(element));
            }
            else if (Serializer.CheckIFile && element is IFileSerialize)
            {
                var fileSerialize = element as IFileSerialize;
                fileSerialize.Save();
                Writer.WriteElementString("FileName", fileSerialize.FileName);
            }
            else if (element is ISerializableElement)
            {
                ((ISerializableElement)element).Serialize(this);
            }
            else
            {
                if (info.IsList)
                {
                    if (((IList)element).Count > 0)
                    {
                        WriteAttribute("Count", ((IList)element).Count);
                    }
                    if (info.ListDefaulType)
                    {
                        WriteAttribute("DT", info.ListDefaulType);
                    }
                }

                foreach (var attribute in info.GetAttributes())
                {
                    var value = attribute.Invoker.GetValue(element);
                    if (value == null || attribute.CheckDefault(value))
                    {
                        continue;
                    }
                    WriteAttribute(attribute, value);
                }

                foreach (var property in info.GetContents())
                {
                    var value = property.Invoker.GetValue(element);
                    if (value == null)
                    {
                        continue;
                    }

                    var mtype = property.DataType;

                    if (property.IsText)
                    {
                        Writer.WriteElementString(property.Name, property.TextFormat(value));
                    }
                    else
                    {
                        Write(value, property.Name, value.GetType() != mtype && mtype != typeof(Type));
                    }
                }

                if (info.IsDictionary)
                {
                    WriteDictionary((IDictionary)element, info.Type);
                }
                else if (element is INamedList)
                {
                    WriteNamedList((INamedList)element, info);
                }
                else if (info.IsList)
                {
                    WriteCollection((ICollection)element, info);
                }
            }
            WriteEnd();
        }
Esempio n. 8
0
 public void WriteType(TypeSerializationInfo info)
 {
     Writer.WriteComment(info.TypeName);
 }
Esempio n. 9
0
 public void SetTypeInfo(Type type, TypeSerializationInfo info)
 {
     SerializationInfo[type] = info;
 }