コード例 #1
0
        /// <summary>
        /// Deserializes an object of the specified type.
        /// </summary>
        /// <param name="reader">The <see cref="EventReader" /> where to deserialize the object.</param>
        /// <param name="type">The static type of the object to deserialize.</param>
        /// <param name="options">Options that control how the deserialization is to be performed.</param>
        /// <returns>Returns the deserialized object.</returns>
        public object Deserialize(EventReader reader, Type type, DeserializationOptions options = DeserializationOptions.None)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

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

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



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

            if (hasStreamStart)
            {
                reader.Expect <StreamEnd>();
            }
        }
コード例 #2
0
        public void NotSpecifyingObjectFactoryUsesDefault()
        {
            var serializer = new YamlSerializer();
            var options = new DeserializationOptions();
            options.Mappings.Add("!foo", typeof(FooBase));
            var result = serializer.Deserialize(new StringReader("!foo {}"), options);

            Assert.IsType<FooBase>(result);
        }
コード例 #3
0
        public void ObjectFactoryIsInvoked()
        {
            var serializer = new YamlSerializer();
            var options = new DeserializationOptions();
            options.Mappings.Add("!foo", typeof(FooBase));

            options.ObjectFactory = new LambdaObjectFactory(t => new FooDerived());

            var result = serializer.Deserialize(new StringReader("!foo {}"), options);

            Assert.IsType<FooDerived>(result);
        }
コード例 #4
0
        public void CustomTags()
        {
            DeserializationOptions options = new DeserializationOptions();
            options.Mappings.Add("tag:yaml.org,2002:point", typeof(Point));

            YamlSerializer serializer = new YamlSerializer();
            object result = serializer.Deserialize(YamlFile("tags.yaml"), options);

            Assert.AreEqual(typeof(Point), result.GetType(), "The deserializer should have used the correct type.");

            Point value = (Point)result;
            Assert.AreEqual(10, value.X, "The property X has the wrong value.");
            Assert.AreEqual(20, value.Y, "The property Y has the wrong value.");
        }
コード例 #5
0
        public void CustomTags()
        {
            DeserializationOptions options = new DeserializationOptions();
            options.Mappings.Add("tag:yaml.org,2002:point", typeof(Point));

            YamlSerializer serializer = new YamlSerializer();
            object result = serializer.Deserialize(YamlFile("tags.yaml"), options);

            Assert.Equal(typeof(Point), result.GetType());

            Point value = (Point)result;
            Assert.Equal(10, value.X);
            Assert.Equal(20, value.Y);
        }
コード例 #6
0
        /// <summary>
        /// Deserializes an object of the specified type.
        /// </summary>
        /// <param name="reader">The <see cref="EventReader" /> where to deserialize the object.</param>
        /// <param name="type">The static type of the object to deserialize.</param>
        /// <param name="options">Options that control how the deserialization is to be performed.</param>
        /// <returns>Returns the deserialized object.</returns>
        public object Deserialize(EventReader reader, Type type, DeserializationOptions options = DeserializationOptions.None)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

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

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

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

            if (hasStreamStart)
            {
                reader.Expect<StreamEnd>();
            }
        }
コード例 #7
0
        public void Overrides()
        {
            DeserializationOptions options = new DeserializationOptions();
            options.Overrides.Add(typeof(Z), "aaa", (t, reader) => ((Z)t).aaa = reader.Expect<Scalar>().Value.ToUpperInvariant());

            YamlSerializer serializer = new YamlSerializer();
            object result = serializer.Deserialize(YamlFile("explicitType.yaml"), options);

            Assert.IsTrue(typeof(Z).IsAssignableFrom(result.GetType()), "The deserializer should have used the correct type.");
            Assert.AreEqual("BBB", ((Z)result).aaa, "The property has the wrong value.");
        }
コード例 #8
0
        public void Overrides()
        {
            DeserializationOptions options = new DeserializationOptions();
            options.Overrides.Add(typeof(Z), "aaa", (t, reader) => ((Z)t).aaa = reader.Expect<Scalar>().Value.ToUpperInvariant());

            YamlSerializer serializer = new YamlSerializer();
            object result = serializer.Deserialize(YamlFile("explicitType.yaml"), options);

            Assert.True(typeof(Z).IsAssignableFrom(result.GetType()));
            Assert.Equal("BBB", ((Z)result).aaa);
        }