Ejemplo n.º 1
0
        /// <summary>
        /// Deserializes a potentially endless sequence of objects from a stream source
        /// </summary>
        /// <param name="input">a streamed source of objects</param>
        /// <returns>a sequence of objects</returns>
        /// <remarks>
        /// character stream => token stream => object stream
        /// </remarks>
        public IEnumerable ReadMany(TextReader input)
        {
            ITextTokenizer <T> tokenizer = this.GetTokenizer();

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

            ITokenAnalyzer <T> analyzer = this.GetAnalyzer();

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

            try
            {
                // chars stream => token stream => object stream
                return(analyzer.Analyze(tokenizer.GetTokens(input)));
            }
            catch (DeserializationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new DeserializationException(ex.Message, tokenizer.Index, tokenizer.Line, tokenizer.Column, ex);
            }
        }
Ejemplo n.º 2
0
        private object ReadSingle(ITextTokenizer <T> tokenizer, IEnumerable <Token <T> > tokens, Type targetType)
        {
            ITokenAnalyzer <T> analyzer = this.GetAnalyzer();

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

            try
            {
                IEnumerator enumerator = analyzer.Analyze(tokens, targetType).GetEnumerator();
                if (!enumerator.MoveNext())
                {
                    return(null);
                }

                // character stream => token stream => object stream
                object value = enumerator.Current;

                // enforce only one object in stream
                if (!this.Settings.AllowTrailingContent && enumerator.MoveNext())
                {
                    throw new DeserializationException("Invalid trailing content", tokenizer.Index, tokenizer.Line, tokenizer.Column);
                }

                return(value);
            }
            catch (DeserializationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new DeserializationException(ex.Message, tokenizer.Index, tokenizer.Line, tokenizer.Column, ex);
            }
        }
Ejemplo n.º 3
0
 public TokenModel?AnalyzeToken(
     [Service] ITokenAnalyzer analyzer,
     string token)
 {
     return(analyzer.Analyze(token));
 }