Esempio n. 1
0
        /// <summary>
        /// Serializes the specified object to a string.
        /// </summary>
        /// <param name="graph">The graph.</param>
        /// <param name="expectedType">The expected type.</param>
        /// <param name="contextSettings">The context settings.</param>
        /// <returns>A YAML string of the object.</returns>
        public string Serialize(object graph, Type expectedType, SerializerContextSettings contextSettings = null)
        {
            var stringWriter = new StringWriter();

            Serialize(stringWriter, graph, expectedType, contextSettings);
            return(stringWriter.ToString());
        }
Esempio n. 2
0
        /// <summary>
        /// Deserializes an object from the specified <see cref="Stream" /> with an expected specific type.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="expectedType">The expected type.</param>
        /// <param name="contextSettings">The context settings.</param>
        /// <param name="context">The context used to deserialize this object.</param>
        /// <returns>A deserialized object.</returns>
        /// <exception cref="System.ArgumentNullException">stream</exception>
        public object Deserialize(Stream stream, Type expectedType, SerializerContextSettings contextSettings, out SerializerContext context)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            return(Deserialize(new StreamReader(stream), expectedType, null, contextSettings, out context));
        }
Esempio n. 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SerializerContext"/> class.
        /// </summary>
        /// <param name="serializer">The serializer.</param>
        /// <param name="serializerContextSettings">The serializer context settings.</param>
        internal SerializerContext(Serializer serializer, SerializerContextSettings serializerContextSettings)
        {
            Serializer              = serializer;
            ObjectFactory           = serializer.Settings.ObjectFactory;
            ObjectSerializerBackend = serializer.Settings.ObjectSerializerBackend;
            var contextSettings = serializerContextSettings ?? SerializerContextSettings.Default;

            Logger     = contextSettings.Logger;
            MemberMask = contextSettings.MemberMask;
            Properties = contextSettings.Properties;
        }
Esempio n. 4
0
        /// <summary>
        ///   Serializes the specified object.
        /// </summary>
        /// <param name="stream">The stream to which to serialize the object.</param>
        /// <param name="expectedType">The expected type.</param>
        /// <param name="obj">The object to serialize.</param>
        /// <param name="contextSettings">The context settings. Specify <c>null</c> to use the defailt settings.</param>
        public void Serialize(Stream stream, object obj, Type expectedType, SerializerContextSettings contextSettings = null)
        {
            var writer = new StreamWriter(stream);

            try
            {
                Serialize(writer, obj, expectedType, contextSettings);
            }
            finally
            {
                try
                {
                    writer.Flush();
                }
                catch { }
            }
        }
Esempio n. 5
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();
        }
Esempio n. 6
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, ex);
                }
            }

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

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

            return(result);
        }
Esempio n. 7
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>
        /// <returns>A deserialized object.</returns>
        /// <exception cref="System.ArgumentNullException">reader</exception>
        public object Deserialize(EventReader reader, Type expectedType, object existingObject = null, SerializerContextSettings contextSettings = null)
        {
            SerializerContext context;

            return(Deserialize(reader, expectedType, existingObject, contextSettings, out context));
        }
Esempio n. 8
0
 /// <summary>
 /// Deserializes an object from the specified <see cref="TextReader" /> with an expected specific type.
 /// </summary>
 /// <param name="reader">The reader.</param>
 /// <param name="expectedType">The expected type.</param>
 /// <param name="existingObject">The object to deserialize into. If null (the default) then a new object will be created</param>
 /// <param name="contextSettings">The context settings.</param>
 /// <param name="context">The context used to deserialize this object.</param>
 /// <returns>A deserialized object.</returns>
 /// <exception cref="System.ArgumentNullException">reader</exception>
 public object Deserialize(TextReader reader, Type expectedType, object existingObject, SerializerContextSettings contextSettings, out SerializerContext context)
 {
     if (reader == null)
     {
         throw new ArgumentNullException(nameof(reader));
     }
     return(Deserialize(new EventReader(new Parser(reader)), expectedType, existingObject, contextSettings, out context));
 }
Esempio n. 9
0
 /// <summary>
 /// Serializes the specified object.
 /// </summary>
 /// <param name="writer">The <see cref="TextWriter" /> 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(TextWriter writer, object graph, Type type, SerializerContextSettings contextSettings = null)
 {
     Serialize(new Emitter(writer, Settings.PreferredIndent), graph, type, contextSettings);
 }