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>();
                ValuePromise valuePromise;
                if (!aliasState.TryGetValue(alias.Value, out valuePromise))
                {
                    valuePromise = new ValuePromise(alias);
                    aliasState.Add(alias.Value, valuePromise);
                }

                return(valuePromise.HasValue ? valuePromise.Value : valuePromise);
            }

            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>();

                ValuePromise valuePromise;
                if (!aliasState.TryGetValue(anchor, out valuePromise))
                {
                    aliasState.Add(anchor, new ValuePromise(value));
                }
                else if (!valuePromise.HasValue)
                {
                    valuePromise.Value = value;
                }
                else
                {
                    throw new DuplicateAnchorException(alias.Start, alias.End, string.Format(
                                                           "Anchor '{0}' already defined",
                                                           alias.Value
                                                           ));
                }
            }

            return(value);
        }
Exemple #2
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>
        /// <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>())
            {
                using (var state = new SerializerState())
                {
                    result = valueDeserializer.DeserializeValue(reader, type, state, valueDeserializer);
                }
            }

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

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

            return(result);
        }
        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);
        }
        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
                          )
                      );
        }