예제 #1
0
        public object DeserializeValue(EventReader reader, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
        {
            var nodeEvent = reader.Peek<NodeEvent>();

            var nodeType = GetTypeFromEvent(nodeEvent, expectedType);

            try
            {
                foreach (var deserializer in deserializers)
                {
                    object value;
                    if (deserializer.Deserialize(reader, nodeType, (r, t) => nestedObjectDeserializer.DeserializeValue(r, t, state, nestedObjectDeserializer), out value))
                    {
                        return value;
                    }
                }
            }
            catch (YamlException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new YamlException(nodeEvent.Start, nodeEvent.End, "Exception during deserialization", ex);
            }

            throw new YamlException(
                nodeEvent.Start,
                nodeEvent.End,
                string.Format(
                    "No node deserializer was able to deserialize the node into type {0}",
                    expectedType.AssemblyQualifiedName
                )
            );
        }
        public object Deserialize(IParser parser, Type type)
        {
            if (parser == null)
            {
                throw new ArgumentNullException("reader");
            }
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            bool   flag   = parser.Allow <StreamStart>() != null;
            bool   flag2  = parser.Allow <DocumentStart>() != null;
            object result = null;

            if (!parser.Accept <DocumentEnd>() && !parser.Accept <StreamEnd>())
            {
                using (SerializerState serializerState = new SerializerState())
                {
                    result = valueDeserializer.DeserializeValue(parser, type, serializerState, valueDeserializer);
                    serializerState.OnDeserialization();
                }
            }
            if (flag2)
            {
                parser.Expect <DocumentEnd>();
            }
            if (flag)
            {
                parser.Expect <StreamEnd>();
            }
            return(result);
        }
        public object?DeserializeValue(IParser parser, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
        {
            parser.Accept <NodeEvent>(out var nodeEvent);
            var nodeType = GetTypeFromEvent(nodeEvent, expectedType);

            try
            {
                foreach (var deserializer in deserializers)
                {
                    if (deserializer.Deserialize(parser, nodeType, (r, t) => nestedObjectDeserializer.DeserializeValue(r, t, state, nestedObjectDeserializer), out var value))
                    {
                        return(TypeConverter.ChangeType(value, expectedType));
                    }
                }
            }
            catch (YamlException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new YamlException(
                          nodeEvent?.Start ?? Mark.Empty,
                          nodeEvent?.End ?? Mark.Empty,
                          "Exception during deserialization",
                          ex
                          );
            }

            throw new YamlException(
                      nodeEvent?.Start ?? Mark.Empty,
                      nodeEvent?.End ?? Mark.Empty,
                      $"No node deserializer was able to deserialize the node into type {expectedType.AssemblyQualifiedName}"
                      );
        }
예제 #4
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)
        {
            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;

            object result = null;

            if (!reader.Accept <DocumentEnd>() && !reader.Accept <StreamEnd>())
            {
                result = valueDeserializer.DeserializeValue(reader, type, new SerializerState(), valueDeserializer);
            }

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

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

            return(result);
        }
예제 #5
0
            /// <summary>
            /// AWARENESS: not thread safe
            /// </summary>
            /// <param name="reader"></param>
            /// <param name="expectedType"></param>
            /// <param name="state"></param>
            /// <param name="nestedObjectDeserializer"></param>
            /// <returns></returns>
            public object DeserializeValue(EventReader reader, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
            {
                object value = _innerDeserializer.DeserializeValue(reader, expectedType, state, nestedObjectDeserializer);

                var str = value as string;

                if (str != null && str.Trim() == Constants.ContentPlaceholder)
                {
                    ContainPlaceholder = true;
                    return(_replacer);
                }

                return(value);
            }
        public object DeserializeValue(IParser parser, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
        {
            AnchorAlias anchorAlias = parser.Allow <AnchorAlias>();

            if (anchorAlias != null)
            {
                AliasState aliasState = state.Get <AliasState>();
                if (!aliasState.TryGetValue(anchorAlias.Value, out ValuePromise value))
                {
                    value = new ValuePromise(anchorAlias);
                    aliasState.Add(anchorAlias.Value, value);
                }
                return((!value.HasValue) ? value : value.Value);
            }
            string    text      = null;
            NodeEvent nodeEvent = parser.Peek <NodeEvent>();

            if (nodeEvent != null && !string.IsNullOrEmpty(nodeEvent.Anchor))
            {
                text = nodeEvent.Anchor;
            }
            object obj = innerDeserializer.DeserializeValue(parser, expectedType, state, nestedObjectDeserializer);

            if (text != null)
            {
                AliasState aliasState2 = state.Get <AliasState>();
                if (!aliasState2.TryGetValue(text, out ValuePromise value2))
                {
                    aliasState2.Add(text, new ValuePromise(obj));
                }
                else if (!value2.HasValue)
                {
                    value2.Value = obj;
                }
                else
                {
                    aliasState2[text] = new ValuePromise(obj);
                }
            }
            return(obj);
        }
        public object DeserializeValue(EventReader reader, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
        {
            object value;
            var    alias = reader.Allow <AnchorAlias>();

            if (alias != null)
            {
                var aliasState = state.Get <AliasState>();
                if (aliasState.TryGetValue(alias.Value, out value))
                {
                    return(value);
                }

                throw new AnchorNotFoundException(alias.Start, alias.End, string.Format(
                                                      "Anchor '{0}' not found",
                                                      alias.Value
                                                      ));
            }

            string anchor = null;

            var nodeEvent = reader.Peek <NodeEvent>();

            if (nodeEvent != null && !string.IsNullOrEmpty(nodeEvent.Anchor))
            {
                anchor = nodeEvent.Anchor;
            }

            value = innerDeserializer.DeserializeValue(reader, expectedType, state, nestedObjectDeserializer);

            if (anchor != null)
            {
                var aliasState = state.Get <AliasState>();
                aliasState.Add(anchor, value);
            }

            return(value);
        }
예제 #8
0
		public object DeserializeValue (EventReader reader, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
		{
			var nodeEvent = reader.Peek<NodeEvent>();

			var nodeType = GetTypeFromEvent(nodeEvent, expectedType);

			foreach (var deserializer in deserializers)
			{
				object value;
				if (deserializer.Deserialize(reader, nodeType, (r, t) => nestedObjectDeserializer.DeserializeValue(r, t, state, nestedObjectDeserializer), out value))
				{
					return value;
				}
			}

			throw new SerializationException(
				string.Format(
					"No node deserializer was able to deserialize the node at {0} into type {1}",
					reader.Parser.Current.Start,
					expectedType.AssemblyQualifiedName
				)
			);
		}
예제 #9
0
        public object DeserializeValue(IParser parser, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
        {
            NodeEvent nodeEvent     = parser.Peek <NodeEvent>();
            Type      typeFromEvent = GetTypeFromEvent(nodeEvent, expectedType);

            try
            {
                foreach (INodeDeserializer deserializer in deserializers)
                {
                    if (deserializer.Deserialize(parser, typeFromEvent, (IParser r, Type t) => nestedObjectDeserializer.DeserializeValue(r, t, state, nestedObjectDeserializer), out object value))
                    {
                        return(TypeConverter.ChangeType(value, expectedType));
                    }
                }
            }
            catch (YamlException)
            {
                throw;
            }
            catch (Exception innerException)
            {
                throw new YamlException(nodeEvent.Start, nodeEvent.End, "Exception during deserialization", innerException);
            }
            throw new YamlException(nodeEvent.Start, nodeEvent.End, $"No node deserializer was able to deserialize the node into type {expectedType.AssemblyQualifiedName}");
        }