Exemplo n.º 1
0
        /// <summary>
        /// Deserializes an object from the specified <see cref="EventReader" /> with an expected specific type.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="expectedType">The expected type, maybe null.</param>
        /// <param name="existingObject">An existing object, may be null.</param>
        /// <param name="contextSettings">The context settings.</param>
        /// <param name="context">The context used to deserialize the object.</param>
        /// <returns>A deserialized object.</returns>
        /// <exception cref="System.ArgumentNullException">reader</exception>
        public object Deserialize(EventReader reader, Type expectedType, object existingObject, SerializerContextSettings contextSettings, out SerializerContext context)
        {
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            var hasStreamStart   = reader.Allow <StreamStart>() != null;
            var hasDocumentStart = reader.Allow <DocumentStart>() != null;

            context = null;

            object result = null;

            if (!reader.Accept <DocumentEnd>() && !reader.Accept <StreamEnd>())
            {
                context = new SerializerContext(this, contextSettings)
                {
                    Reader = reader
                };
                var node = context.Reader.Parser.Current;
                try
                {
                    var objectContext = new ObjectContext(context, existingObject, context.FindTypeDescriptor(expectedType));
                    result = context.Serializer.ObjectSerializer.ReadYaml(ref objectContext);
                }
                catch (YamlException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    ex = ex.Unwrap();
                    throw new YamlException(node.Start, node.End, $"Error while deserializing node [{node}]:\n{ex.Message}", ex);
                }
            }

            if (hasDocumentStart)
            {
                reader.Expect <DocumentEnd>();
            }

            if (hasStreamStart)
            {
                reader.Expect <StreamEnd>();
            }

            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Serializes the specified object.
        /// </summary>
        /// <param name="emitter">The <see cref="IEmitter" /> where to serialize the object.</param>
        /// <param name="graph">The object to serialize.</param>
        /// <param name="type">The static type of the object to serialize.</param>
        /// <param name="contextSettings">The context settings.</param>
        public void Serialize(IEmitter emitter, object graph, Type type, SerializerContextSettings contextSettings = null)
        {
            if (emitter == null)
            {
                throw new ArgumentNullException(nameof(emitter));
            }

            if (graph == null && type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            // Configure the emitter
            // TODO the current emitter is not enough configurable to format its output
            // This should be improved
            var defaultEmitter = emitter as Emitter;

            if (defaultEmitter != null)
            {
                defaultEmitter.ForceIndentLess = Settings.IndentLess;
            }

            var context = new SerializerContext(this, contextSettings)
            {
                Emitter = emitter, Writer = CreateEmitter(emitter)
            };

            // Serialize the document
            context.Writer.StreamStart();
            context.Writer.DocumentStart();
            var objectContext = new ObjectContext(context, graph, context.FindTypeDescriptor(type))
            {
                Style = DataStyle.Any
            };

            context.Serializer.ObjectSerializer.WriteYaml(ref objectContext);
            context.Writer.DocumentEnd();
            context.Writer.StreamEnd();
        }