Esempio n. 1
0
        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(nodeEvent.Start, nodeEvent.End, string.Format(
                                                           "Anchor '{0}' already defined",
                                                           anchor
                                                           ));
                }
            }

            return(value);
        }
        public object DeserializeValue(IParser parser, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
        {
            object value;
            var    alias = parser.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 = parser.Peek <NodeEvent>();

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

            value = innerDeserializer.DeserializeValue(parser, 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
                {
                    aliasState[anchor] = new ValuePromise(value);
                }
            }

            return(value);
        }
        public object?DeserializeValue(IParser parser, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
        {
            object?value;

            if (parser.TryConsume <AnchorAlias>(out var alias))
            {
                var aliasState = state.Get <AliasState>();
                if (!aliasState.TryGetValue(alias.Value, out var valuePromise))
                {
                    throw new AnchorNotFoundException(alias.Start, alias.End, $"Alias ${alias.Value} cannot precede anchor declaration");
                }

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

            var anchor = AnchorName.Empty;

            if (parser.Accept <NodeEvent>(out var nodeEvent) && !nodeEvent.Anchor.IsEmpty)
            {
                anchor = nodeEvent.Anchor;
                var aliasState = state.Get <AliasState>();
                if (!aliasState.ContainsKey(anchor))
                {
                    aliasState[anchor] = new ValuePromise(new AnchorAlias(anchor));
                }
            }

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

            if (!anchor.IsEmpty)
            {
                var aliasState = state.Get <AliasState>();

                if (!aliasState.TryGetValue(anchor, out var valuePromise))
                {
                    aliasState.Add(anchor, new ValuePromise(value));
                }
                else if (!valuePromise.HasValue)
                {
                    valuePromise.Value = value;
                }
                else
                {
                    aliasState[anchor] = new ValuePromise(value);
                }
            }

            return(value);
        }
        public object DeserializeValue(EventReader reader, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
        {
            AnchorAlias alias = reader.Allow <AnchorAlias>();

            if (alias != null)
            {
                ValuePromise promise;
                AliasState   state2 = state.Get <AliasState>();
                if (!state2.TryGetValue(alias.Value, out promise))
                {
                    promise = new ValuePromise(alias);
                    state2.Add(alias.Value, promise);
                }
                return(!promise.HasValue ? promise : promise.Value);
            }
            string    key    = null;
            NodeEvent event2 = reader.Peek <NodeEvent>();

            if ((event2 != null) && !string.IsNullOrEmpty(event2.Anchor))
            {
                key = event2.Anchor;
            }
            object obj2 = this.innerDeserializer.DeserializeValue(reader, expectedType, state, nestedObjectDeserializer);

            if (key != null)
            {
                ValuePromise promise2;
                AliasState   state3 = state.Get <AliasState>();
                if (!state3.TryGetValue(key, out promise2))
                {
                    state3.Add(key, new ValuePromise(obj2));
                }
                else
                {
                    if (promise2.HasValue)
                    {
                        throw new DuplicateAnchorException(event2.Start, event2.End, $"Anchor '{key}' already defined");
                    }
                    promise2.Value = obj2;
                }
            }
            return(obj2);
        }
        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);
        }