public override bool Enter(IObjectDescriptor value, IEmitter context)
        {
            IYamlTypeConverter yamlTypeConverter = typeConverters.FirstOrDefault((IYamlTypeConverter t) => t.Accepts(value.Type));

            if (yamlTypeConverter != null)
            {
                yamlTypeConverter.WriteYaml(context, value.Value, value.Type);
                return(false);
            }
            IYamlConvertible yamlConvertible = value.Value as IYamlConvertible;

            if (yamlConvertible != null)
            {
                yamlConvertible.Write(context, nestedObjectSerializer);
                return(false);
            }
            IYamlSerializable yamlSerializable = value.Value as IYamlSerializable;

            if (yamlSerializable != null)
            {
                yamlSerializable.WriteYaml(context);
                return(false);
            }
            return(base.Enter(value, context));
        }
        /// <summary>
        /// Sets the serializer to chain with this instance.
        /// </summary>
        /// <param name="other">The serializer to chain with this instance.</param>
        public void PrependTo(IYamlSerializable other)
        {
            if (next != null)
                throw new InvalidOperationException("This serializer already have a succeeding serializer");

            next = other;
        }
        public void Register(Type type, IYamlSerializable serializable)
        {
            if (type == null) throw new ArgumentNullException("type");
            if (serializable == null) throw new ArgumentNullException("serializable");

            typeToSerializable[type] = serializable;
        }
Exemplo n.º 4
0
        private static object DeserializeYamlSerializable(EventReader reader, Type type)
        {
            IYamlSerializable result = (IYamlSerializable)Activator.CreateInstance(type);

            result.ReadYaml(reader.Parser);
            return(result);
        }
Exemplo n.º 5
0
 public ChainedSerializer(IYamlSerializable next)
 {
     if (next == null)
     {
         throw new ArgumentNullException("next");
     }
     this.next = next;
 }
Exemplo n.º 6
0
        /// <summary>
        /// Sets the serializer to chain with this instance.
        /// </summary>
        /// <param name="other">The serializer to chain with this instance.</param>
        public void PrependTo(IYamlSerializable other)
        {
            if (next != null)
            {
                throw new InvalidOperationException("This serializer already have a succeeding serializer");
            }

            next = other;
        }
Exemplo n.º 7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Serializer"/> class.
        /// </summary>
        /// <param name="settings">The settings.</param>
        public Serializer(SerializerSettings settings)
        {
            this.settings         = settings ?? new SerializerSettings();
            TypeDescriptorFactory = CreateTypeDescriptorFactory();
            RoutingSerializer routingSerializer;

            ObjectSerializer  = CreateProcessor(out routingSerializer);
            RoutingSerializer = routingSerializer;
        }
Exemplo n.º 8
0
 public bool Deserialize(IParser parser, Type expectedType, Func <IParser, Type, object> nestedObjectDeserializer, out object value)
 {
     if (typeof(IYamlSerializable).IsAssignableFrom(expectedType))
     {
         IYamlSerializable yamlSerializable = (IYamlSerializable)objectFactory.Create(expectedType);
         yamlSerializable.ReadYaml(parser);
         value = yamlSerializable;
         return(true);
     }
     value = null;
     return(false);
 }
Exemplo n.º 9
0
 /// <summary>
 /// Adds a custom serializer for the specified type.
 /// </summary>
 /// <param name="type">The type.</param>
 /// <param name="serializer">The serializer.</param>
 /// <exception cref="System.ArgumentNullException">
 /// type
 /// or
 /// serializer
 /// </exception>
 public void RegisterSerializer(Type type, IYamlSerializable serializer)
 {
     if (type == null)
     {
         throw new ArgumentNullException("type");
     }
     if (serializer == null)
     {
         throw new ArgumentNullException("serializer");
     }
     serializers[type] = serializer;
 }
Exemplo n.º 10
0
        public void Register(Type type, IYamlSerializable serializable)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (serializable == null)
            {
                throw new ArgumentNullException("serializable");
            }

            typeToSerializable[type] = serializable;
        }
Exemplo n.º 11
0
        public YamlFluentSerializer Add(string key, IYamlSerializable value)
        {
            // Create a floating node and have the child serialize into it
            this.Begin(YamlContainerType.List, true);
            value.Serialize(this);
            YamlNode valueNode = this.parentStack.Peek();

            this.End();

            // Then add the floating node to the document
            this.AddNodeToParent(key, valueNode);

            return(this);
        }
		bool IObjectGraphVisitor<Nothing>.Enter(IObjectDescriptor value, Nothing context)
		{
			IYamlTypeConverter yamlTypeConverter = typeConverters.FirstOrDefault((IYamlTypeConverter t) => t.Accepts(value.Type));
			if (yamlTypeConverter != null)
			{
				return false;
			}
			IYamlConvertible yamlConvertible = value.Value as IYamlConvertible;
			if (yamlConvertible != null)
			{
				return false;
			}
			IYamlSerializable yamlSerializable = value.Value as IYamlSerializable;
			if (yamlSerializable != null)
			{
				return false;
			}
			return Enter(value);
		}
Exemplo n.º 13
0
        private IYamlSerializable CreateProcessor(out RoutingSerializer routingSerializer)
        {
            routingSerializer = new RoutingSerializer(Settings.SerializerFactorySelector);

            IYamlSerializable serializer = routingSerializer;

            if (Settings.PreSerializer != null)
            {
                serializer = ChainedSerializer.Prepend(Settings.PreSerializer, routingSerializer);
            }
            serializer = ChainedSerializer.Prepend(new TagTypeSerializer(), serializer);
            if (Settings.EmitAlias)
            {
                serializer = ChainedSerializer.Prepend(new AnchorSerializer(), serializer);
            }
            if (Settings.PostSerializer != null)
            {
                serializer = ChainedSerializer.Prepend(Settings.PostSerializer, serializer);
            }
            return(serializer);
        }
Exemplo n.º 14
0
		public ChainedSerializer(IYamlSerializable next)
		{
			if (next == null) throw new ArgumentNullException("next");
			this.next = next;
		}
Exemplo n.º 15
0
 /// <summary>
 /// Sets the serializer to chain with an instance of <see cref="ChainedSerializer"/>.
 /// </summary>
 /// <param name="chained">The chained serializer.</param>
 /// <param name="serializer">The serializer to chain.</param>
 /// <returns>The chained argument passed in the <paramref name="chained"/> parameter.</returns>
 public static ChainedSerializer Prepend(ChainedSerializer chained, IYamlSerializable serializer)
 {
     chained.PrependTo(serializer);
     return chained;
 }
Exemplo n.º 16
0
 public void AddSerializer(Type type, IYamlSerializable serializer)
 {
     serializers[type] = serializer;
 }
Exemplo n.º 17
0
 public TagTypeSerializer(IYamlSerializable next) : base(next)
 {
 }
Exemplo n.º 18
0
 /// <summary>
 /// Registers a <see cref="IYamlSerializable"/> for the specified type.
 /// </summary>
 /// <param name="type">The type.</param>
 /// <param name="serializable">The serializable.</param>
 public void Register(Type type, IYamlSerializable serializable)
 {
     Serializable.Register(type, serializable);
 }
 public TagTypeSerializer(IYamlSerializable next)
     : base(next)
 {
 }
Exemplo n.º 20
0
 public AnchorSerializer(IYamlSerializable next, SerializerSettings.AliasDelegate aliasGenerator) : base(next)
 {
     aliasToObject         = new Dictionary <string, object>();
     objectToAlias         = new Dictionary <object, string>(new IdentityEqualityComparer <object>());
     this.TryGenerateAlias = aliasGenerator ?? this.DefaultAliasGenerator;
 }
Exemplo n.º 21
0
		public AnchorSerializer(IYamlSerializable next) : base(next)
		{
            aliasToObject = new Dictionary<string, object>();
		    objectToAlias = new Dictionary<object, string>(new IdentityEqualityComparer<object>());
		}
Exemplo n.º 22
0
 /// <summary>
 /// Sets the serializer to chain with an instance of <see cref="ChainedSerializer"/>.
 /// </summary>
 /// <param name="chained">The chained serializer.</param>
 /// <param name="serializer">The serializer to chain.</param>
 /// <returns>The chained argument passed in the <paramref name="chained"/> parameter.</returns>
 public static ChainedSerializer Prepend(ChainedSerializer chained, IYamlSerializable serializer)
 {
     chained.PrependTo(serializer);
     return(chained);
 }
 public void AddSerializer(Type type, IYamlSerializable serializer)
 {
     serializers[type] = serializer;
 }
Exemplo n.º 24
0
 public void Read(int key, IYamlSerializable value)
 {
     this.BeginRead(key);
     value.Deserialize(this);
     this.EndRead();
 }
Exemplo n.º 25
0
        /// <summary>
        /// Serializes the specified value.
        /// </summary>
        /// <param name="emitter">The emitter.</param>
        /// <param name="type">The type.</param>
        /// <param name="value">The value.</param>
        private void SerializeValue(Emitter emitter, Type type, object value)
        {
            if (value == null)
            {
                emitter.Emit(new Scalar(null, "tag:yaml.org,2002:null", "", ScalarStyle.Plain, false, false));
                return;
            }

            IYamlSerializable serializable = value as IYamlSerializable;

            if (serializable != null)
            {
                serializable.WriteYaml(emitter);
                return;
            }

            string     anchor = null;
            ObjectInfo info;

            if (!DisableAliases && anchors.TryGetValue(value, out info) && info != null)
            {
                if (info.serialized)
                {
                    emitter.Emit(new AnchorAlias(info.anchor));
                    return;
                }

                info.serialized = true;
                anchor          = info.anchor;
            }


            TypeCode typeCode = Type.GetTypeCode(type);

            switch (typeCode)
            {
            case TypeCode.Boolean:
                emitter.Emit(new Scalar(anchor, "tag:yaml.org,2002:bool", value.ToString()));
                break;

            case TypeCode.Byte:
            case TypeCode.Int16:
            case TypeCode.Int32:
            case TypeCode.Int64:
            case TypeCode.SByte:
            case TypeCode.UInt16:
            case TypeCode.UInt32:
            case TypeCode.UInt64:
                emitter.Emit(new Scalar(anchor, "tag:yaml.org,2002:int", Convert.ToString(value, numberFormat)));
                break;

            case TypeCode.Single:
            case TypeCode.Double:
            case TypeCode.Decimal:
                emitter.Emit(new Scalar(anchor, "tag:yaml.org,2002:float", Convert.ToString(value, numberFormat)));
                break;

            case TypeCode.String:
            case TypeCode.Char:
                emitter.Emit(new Scalar(anchor, "tag:yaml.org,2002:str", value.ToString()));
                break;

            case TypeCode.DateTime:
                emitter.Emit(new Scalar(anchor, "tag:yaml.org,2002:timestamp", ((DateTime)value).ToString("o", CultureInfo.InvariantCulture)));
                break;

            case TypeCode.DBNull:
            case TypeCode.Empty:
                throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture, "TypeCode.{0} is not supported.", typeCode));

            default:
                SerializeObject(emitter, type, value, anchor);
                break;
            }
        }
Exemplo n.º 26
0
 public AnchorSerializer(IYamlSerializable next) : base(next)
 {
     aliasToObject = new Dictionary <string, object>();
     objectToAlias = new Dictionary <object, string>(new IdentityEqualityComparer <object>());
 }
Exemplo n.º 27
0
 /// <summary>
 /// Adds a custom serializer for the specified type.
 /// </summary>
 /// <param name="type">The type.</param>
 /// <param name="serializer">The serializer.</param>
 /// <exception cref="System.ArgumentNullException">
 /// type
 /// or
 /// serializer
 /// </exception>
 public void RegisterSerializer(Type type, IYamlSerializable serializer)
 {
     if (type == null) throw new ArgumentNullException("type");
     if (serializer == null) throw new ArgumentNullException("serializer");
     serializers[type] = serializer;
 }
Exemplo n.º 28
0
 // -------------------------------------------------------------------
 // Public
 // -------------------------------------------------------------------
 public YamlFluentSerializer Add(int key, IYamlSerializable value)
 {
     return(this.Add(key.ToString(CultureInfo.InvariantCulture), value));
 }