Пример #1
0
        private static void WriteType(Writer writer, Type type, string name, object Object)
        {
            ILispSerializer serializer = GetSerializer(type);

            if (serializer == null)
            {
                serializer = CreateRootSerializer(type);
            }

            serializer.Write(writer, name, Object);
        }
Пример #2
0
        public void Write(Writer writer, string name, object Object)
        {
            writer.StartList(name);

            foreach (FieldOrProperty field in FieldOrProperty.GetFieldsAndProperties(type))
            {
                LispChildAttribute ChildAttrib = (LispChildAttribute)
                                                 field.GetCustomAttribute(typeof(LispChildAttribute));
                if (ChildAttrib != null)
                {
                    object Value = field.GetValue(Object);
                    if (Value != null)
                    {
                        if (ChildAttrib.Translatable)
                        {
                            if (!ChildAttrib.Optional || !Value.Equals(ChildAttrib.Default))
                            {
                                writer.WriteTranslatable(ChildAttrib.Name, Value.ToString());
                            }
                        }
                        else
                        {
                            Type childType = field.Type;

                            ILispSerializer serializer = LispSerializer.GetSerializer(childType);
                            if (serializer != null)
                            {
                                serializer.Write(writer, ChildAttrib.Name, Value);
                            }
                            else
                            {
                                if (ChildAttrib.Optional && childType.IsEnum)
                                {
                                    // If it is an enum we need to convert ChildAttrib.Default
                                    // to an enum as ChildAttrib.Default is an Int32 by some (unknown) reason.
                                    Enum Defval = (Enum)Enum.ToObject(childType, ChildAttrib.Default);
                                    if (!Value.Equals(Defval))
                                    {
                                        writer.Write(ChildAttrib.Name, Value);
                                    }
                                }
                                else if (!ChildAttrib.Optional || !Value.Equals(ChildAttrib.Default))
                                {
                                    writer.Write(ChildAttrib.Name, Value);
                                }
                            }
                        }
                    }
                    else
                    {
                        LogManager.Log(LogLevel.DebugWarning, "Field '" + field.Name + "' is null");
                    }
                }

                foreach (LispChildsAttribute childsAttrib in
                         field.GetCustomAttributes(typeof(LispChildsAttribute)))
                {
                    if (childsAttrib != null)
                    {
                        object list = field.GetValue(Object);
                        if (!(list is IEnumerable))
                        {
                            throw new LispException("Field '" + field.Name + "' is not IEnumerable");
                        }

                        ILispSerializer serializer = LispSerializer.GetSerializer(childsAttrib.Type);
                        if (serializer == null)
                        {
                            serializer = LispSerializer.CreateRootSerializer(childsAttrib.Type);
                        }

                        IEnumerable enumerable = (IEnumerable)list;
                        foreach (object childObject in enumerable)
                        {
                            if (childsAttrib.Type.IsAssignableFrom(childObject.GetType()))
                            {
                                serializer.Write(writer, childsAttrib.Name, childObject);
                            }
                        }
                    }
                }
            }

            if (Object is ICustomLispSerializer)
            {
                ICustomLispSerializer custom = (ICustomLispSerializer)Object;
                custom.CustomLispWrite(writer);
            }

            writer.EndList(name);
        }