Ejemplo n.º 1
0
 public Reader(Stream input, IImmutableDictionary <string, IReadHandler> handlers, IDefaultReadHandler <object> defaultHandler)
 {
     this.initialized    = false;
     this.input          = input;
     this.handlers       = handlers;
     this.defaultHandler = defaultHandler;
     this.cache          = new ReadCache();
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Parses the specified cache.
 /// </summary>
 /// <param name="cache">The cache.</param>
 /// <returns></returns>
 public override object Parse(ReadCache cache)
 {
     if (jp.Read())
     {
         return ParseVal(false, cache);
     }
     else
     {
         return null;
     }
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Parses the specified cache.
 /// </summary>
 /// <param name="cache">The cache.</param>
 /// <returns></returns>
 public override object Parse(ReadCache cache)
 {
     if (jp.Read())
     {
         return(ParseVal(false, cache));
     }
     else
     {
         return(null);
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Parses the dictionary.
        /// </summary>
        /// <param name="ignored">if set to <c>true</c> [ignored].</param>
        /// <param name="cache">The cache.</param>
        /// <param name="handler">The handler.</param>
        /// <param name="endToken">The end token.</param>
        /// <returns></returns>
        public object ParseDictionary(bool ignored, ReadCache cache, IDictionaryReadHandler handler, JsonToken endToken)
        {
            IDictionaryReader dr = (handler != null) ? handler.DictionaryReader() : dictionaryBuilder;

            object d = dr.Init();

            while (jp.NextToken() != endToken)
            {
                object key = ParseVal(true, cache);
                if (key is Tag)
                {
                    object val;
                    jp.Read(); // advance to read value
                    string       tag = ((Tag)key).GetValue();
                    IReadHandler val_handler;
                    if (TryGetHandler(tag, out val_handler))
                    {
                        if (this.jp.TokenType == JsonToken.StartObject && val_handler is IDictionaryReadHandler)
                        {
                            // use map reader to decode value
                            val = ParseDictionary(false, cache, (IDictionaryReadHandler)val_handler);
                        }
                        else if (this.jp.TokenType == JsonToken.StartArray && val_handler is IListReadHandler)
                        {
                            // use array reader to decode value
                            val = ParseList(false, cache, (IListReadHandler)val_handler);
                        }
                        else
                        {
                            // read value and decode normally
                            val = val_handler.FromRepresentation(ParseVal(false, cache));
                        }
                    }
                    else
                    {
                        // default decode
                        val = this.Decode(tag, ParseVal(false, cache));
                    }
                    jp.Read(); // advance to read end of object or array
                    return(val);
                }
                else
                {
                    jp.Read(); // advance to read value
                    d = dr.Add(d, key, ParseVal(false, cache));
                }
            }

            return(dr.Complete(d));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Parses the value.
        /// </summary>
        /// <param name="asDictionaryKey">If set to <c>true</c> [as dictionary key].</param>
        /// <param name="cache">The cache.</param>
        /// <returns>
        /// The parsed value.
        /// </returns>
        public override object ParseVal(bool asDictionaryKey, ReadCache cache)
        {
            switch (jp.TokenType)
            {
            case JsonToken.Integer:
                return(ParseLong());

            case JsonToken.StartArray:
                return(ParseList(asDictionaryKey, cache, null));

            case JsonToken.StartObject:
                return(ParseDictionary(asDictionaryKey, cache, null));

            case JsonToken.PropertyName:
            case JsonToken.String:
                return(cache.CacheRead((string)jp.Value, asDictionaryKey, this));

            case JsonToken.Float:
            case JsonToken.Boolean:
                return(jp.Value);

            case JsonToken.Null:
                return(null);

            case JsonToken.Bytes:
            case JsonToken.Comment:
            case JsonToken.Date:
            case JsonToken.EndArray:
            case JsonToken.EndConstructor:
            case JsonToken.EndObject:
            case JsonToken.None:
            case JsonToken.Raw:
            case JsonToken.StartConstructor:
            case JsonToken.Undefined:

            default:
                return(null);
            }
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Parses the dictionary.
 /// </summary>
 /// <param name="ignored">if set to <c>true</c> [ignored].</param>
 /// <param name="cache">The cache.</param>
 /// <param name="handler">The handler.</param>
 /// <returns></returns>
 public override object ParseDictionary(bool ignored, ReadCache cache, IDictionaryReadHandler handler)
 {
     return ParseDictionary(ignored, cache, handler, JsonToken.EndObject);
 }
Ejemplo n.º 7
0
 public void TestReadCache()
 {
     ReadCache rc = new ReadCache();
     Assert.AreEqual("~:foo", rc.CacheRead("~:foo", false));
     Assert.AreEqual("~:foo", rc.CacheRead("^" + (char)WriteCache.BaseCharIdx, false));
     Assert.AreEqual("~$bar", rc.CacheRead("~$bar", false));
     Assert.AreEqual("~$bar", rc.CacheRead("^" + (char)(WriteCache.BaseCharIdx + 1), false));
     Assert.AreEqual("~#baz", rc.CacheRead("~#baz", false));
     Assert.AreEqual("~#baz", rc.CacheRead("^" + (char)(WriteCache.BaseCharIdx + 2), false));
     Assert.AreEqual("foobar", rc.CacheRead("foobar", false));
     Assert.AreEqual("foobar", rc.CacheRead("foobar", false));
     Assert.AreEqual("foobar", rc.CacheRead("foobar", true));
     Assert.AreEqual("foobar", rc.CacheRead("^" + (char)(WriteCache.BaseCharIdx + 3), true));
     Assert.AreEqual("abc", rc.CacheRead("abc", false));
     Assert.AreEqual("abc", rc.CacheRead("abc", false));
     Assert.AreEqual("abc", rc.CacheRead("abc", true));
     Assert.AreEqual("abc", rc.CacheRead("abc", true));
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Parses the list.
 /// </summary>
 /// <param name="asDictionaryKey">If set to <c>true</c> [as dictionary key].</param>
 /// <param name="cache">The cache.</param>
 /// <param name="handler">The handler.</param>
 /// <returns>
 /// The parsed list.
 /// </returns>
 public abstract object ParseList(bool asDictionaryKey, ReadCache cache, IListReadHandler handler);
Ejemplo n.º 9
0
 /// <summary>
 /// Parses the dictionary.
 /// </summary>
 /// <param name="asDictionaryKey">If set to <c>true</c> [as dictionary key].</param>
 /// <param name="cache">The cache.</param>
 /// <param name="handler">The handler.</param>
 /// <returns>
 /// The parced dictionary
 /// </returns>
 public abstract object ParseDictionary(bool asDictionaryKey, ReadCache cache, IDictionaryReadHandler handler);
Ejemplo n.º 10
0
 /// <summary>
 /// Parses the value.
 /// </summary>
 /// <param name="asDictionaryKey">If set to <c>true</c> [as dictionary key].</param>
 /// <param name="cache">The cache.</param>
 /// <returns>
 /// The parsed value.
 /// </returns>
 public abstract object ParseVal(bool asDictionaryKey, ReadCache cache);
Ejemplo n.º 11
0
        /// <summary>
        /// Parses the list.
        /// </summary>
        /// <param name="asDictionaryKey">If set to <c>true</c> [as dictionary key].</param>
        /// <param name="cache">The cache.</param>
        /// <param name="handler">The handler.</param>
        /// <returns>
        /// The parsed list.
        /// </returns>
        public override object ParseList(bool asDictionaryKey, ReadCache cache, IListReadHandler handler)
        {
            // if nextToken == JsonToken.EndArray
            if (jp.NextToken() != JsonToken.EndArray)
            {
                object firstVal = ParseVal(false, cache);
                if (firstVal != null)
                {
                    if (firstVal is string && (string)firstVal == Constants.DirectoryAsList)
                    {
                        // if the same, build a map w/ rest of array contents
                        return(ParseDictionary(false, cache, null, JsonToken.EndArray));
                    }
                    else
                    if (firstVal is Tag)
                    {
                        if (firstVal is Tag)
                        {
                            object val;
                            jp.Read();     // advance to value
                            string       tag = ((Tag)firstVal).GetValue();
                            IReadHandler val_handler;
                            if (TryGetHandler(tag, out val_handler))
                            {
                                if (this.jp.TokenType == JsonToken.StartObject && val_handler is IDictionaryReadHandler)
                                {
                                    // use map reader to decode value
                                    val = ParseDictionary(false, cache, (IDictionaryReadHandler)val_handler);
                                }
                                else
                                if (this.jp.TokenType == JsonToken.StartArray && val_handler is IListReadHandler)
                                {
                                    // use array reader to decode value
                                    val = ParseList(false, cache, (IListReadHandler)val_handler);
                                }
                                else
                                {
                                    // read value and decode normally
                                    val = val_handler.FromRepresentation(ParseVal(false, cache));
                                }
                            }
                            else
                            {
                                // default decode
                                val = this.Decode(tag, ParseVal(false, cache));
                            }
                            jp.Read();     // advance past end of object or array
                            return(val);
                        }
                    }
                }

                IListReader lr = (handler != null) ? handler.ListReader() : listBuilder;
                object      l  = lr.Init();
                l = lr.Add(l, firstVal);
                while (jp.NextToken() != JsonToken.EndArray)
                {
                    l = lr.Add(l, ParseVal(false, cache));
                }
                return(lr.Complete(l));
            }

            // array is empty
            return(listBuilder.Complete(listBuilder.Init()));
        }
Ejemplo n.º 12
0
 /// <summary>
 /// Parses the dictionary.
 /// </summary>
 /// <param name="ignored">if set to <c>true</c> [ignored].</param>
 /// <param name="cache">The cache.</param>
 /// <param name="handler">The handler.</param>
 /// <returns></returns>
 public override object ParseDictionary(bool ignored, ReadCache cache, IDictionaryReadHandler handler)
 {
     return(ParseDictionary(ignored, cache, handler, JsonToken.EndObject));
 }
Ejemplo n.º 13
0
 /// <summary>
 /// Parses the list.
 /// </summary>
 /// <param name="asDictionaryKey">If set to <c>true</c> [as dictionary key].</param>
 /// <param name="cache">The cache.</param>
 /// <param name="handler">The handler.</param>
 /// <returns>
 /// The parsed list.
 /// </returns>
 public abstract object ParseList(bool asDictionaryKey, ReadCache cache, IListReadHandler handler);
Ejemplo n.º 14
0
 /// <summary>
 /// Parses the dictionary.
 /// </summary>
 /// <param name="asDictionaryKey">If set to <c>true</c> [as dictionary key].</param>
 /// <param name="cache">The cache.</param>
 /// <param name="handler">The handler.</param>
 /// <returns>
 /// The parced dictionary
 /// </returns>
 public abstract object ParseDictionary(bool asDictionaryKey, ReadCache cache, IDictionaryReadHandler handler);
Ejemplo n.º 15
0
        /// <summary>
        /// Parses the dictionary.
        /// </summary>
        /// <param name="ignored">if set to <c>true</c> [ignored].</param>
        /// <param name="cache">The cache.</param>
        /// <param name="handler">The handler.</param>
        /// <param name="endToken">The end token.</param>
        /// <returns></returns>
        public object ParseDictionary(bool ignored, ReadCache cache, IDictionaryReadHandler handler, JsonToken endToken)
        {
            IDictionaryReader dr = (handler != null) ? handler.DictionaryReader() : dictionaryBuilder;

            object d = dr.Init();

            while (jp.NextToken() != endToken) 
            {
                object key = ParseVal(true, cache);
                if (key is Tag) 
                {
                    object val;
                    jp.Read(); // advance to read value
                    string tag = ((Tag)key).GetValue();
                    IReadHandler val_handler;
                    if (TryGetHandler(tag, out val_handler))
                    {
                        if (this.jp.TokenType == JsonToken.StartObject && val_handler is IDictionaryReadHandler) {
                            // use map reader to decode value
                            val = ParseDictionary(false, cache, (IDictionaryReadHandler)val_handler);
                        } else if (this.jp.TokenType == JsonToken.StartArray && val_handler is IListReadHandler) {
                            // use array reader to decode value
                            val = ParseList(false, cache, (IListReadHandler)val_handler);
                        } else {
                            // read value and decode normally
                            val = val_handler.FromRepresentation(ParseVal(false, cache));
                        }
                    } else {
                        // default decode
                        val = this.Decode(tag, ParseVal(false, cache));
                    }
                    jp.Read(); // advance to read end of object or array
                    return val;
                } 
                else 
                {
                    jp.Read(); // advance to read value
                    d = dr.Add(d, key, ParseVal(false, cache));
                }
            }

            return dr.Complete(d);
        }
Ejemplo n.º 16
0
 /// <summary>
 /// Parses the specified cache.
 /// </summary>
 /// <param name="cache">The cache.</param>
 /// <returns></returns>
 public abstract object Parse(ReadCache cache);
Ejemplo n.º 17
0
        /// <summary>
        /// Parses the list.
        /// </summary>
        /// <param name="asDictionaryKey">If set to <c>true</c> [as dictionary key].</param>
        /// <param name="cache">The cache.</param>
        /// <param name="handler">The handler.</param>
        /// <returns>
        /// The parsed list.
        /// </returns>
        public override object ParseList(bool asDictionaryKey, ReadCache cache, IListReadHandler handler)
        {
            // if nextToken == JsonToken.EndArray
            if (jp.NextToken() != JsonToken.EndArray) 
            {
                object firstVal = ParseVal(false, cache);
                if (firstVal != null) 
                {
                    if (firstVal is string && (string)firstVal == Constants.DirectoryAsList) 
                    {
                        // if the same, build a map w/ rest of array contents
                        return ParseDictionary(false, cache, null, JsonToken.EndArray);
                    }
                    else 
                        if (firstVal is Tag) 
                        {
                            if (firstVal is Tag) 
                            {
                                object val;
                                jp.Read(); // advance to value
                                string tag = ((Tag)firstVal).GetValue();
                                IReadHandler val_handler;
                                if (TryGetHandler(tag, out val_handler)) 
                                {
                                    if (this.jp.TokenType == JsonToken.StartObject && val_handler is IDictionaryReadHandler) 
                                    {
                                        // use map reader to decode value
                                        val = ParseDictionary(false, cache, (IDictionaryReadHandler)val_handler);
                                    } 
                                    else 
                                        if (this.jp.TokenType == JsonToken.StartArray && val_handler is IListReadHandler) 
                                        {
                                            // use array reader to decode value
                                            val = ParseList(false, cache, (IListReadHandler)val_handler);
                                        } 
                                        else 
                                        {
                                            // read value and decode normally
                                            val = val_handler.FromRepresentation(ParseVal(false, cache));
                                        }
                                } 
                                else 
                                {
                                    // default decode
                                    val = this.Decode(tag, ParseVal(false, cache));
                                }
                                jp.Read(); // advance past end of object or array
                                return val;
                            }
                        }
                }

                IListReader lr = (handler != null) ? handler.ListReader() : listBuilder;
                object l = lr.Init();
                l = lr.Add(l, firstVal);
                while (jp.NextToken() != JsonToken.EndArray) {
                    l = lr.Add(l, ParseVal(false, cache));
                }
                return lr.Complete(l);
            }

            // array is empty
            return listBuilder.Complete(listBuilder.Init());            
        }
Ejemplo n.º 18
0
 /// <summary>
 /// Parses the value.
 /// </summary>
 /// <param name="asDictionaryKey">If set to <c>true</c> [as dictionary key].</param>
 /// <param name="cache">The cache.</param>
 /// <returns>
 /// The parsed value.
 /// </returns>
 public abstract object ParseVal(bool asDictionaryKey, ReadCache cache);
Ejemplo n.º 19
0
        /// <summary>
        /// Parses the value.
        /// </summary>
        /// <param name="asDictionaryKey">If set to <c>true</c> [as dictionary key].</param>
        /// <param name="cache">The cache.</param>
        /// <returns>
        /// The parsed value.
        /// </returns>
        public override object ParseVal(bool asDictionaryKey, ReadCache cache)
        {
            switch (jp.TokenType)
            {
                case JsonToken.Integer:
                    return ParseLong();
                case JsonToken.StartArray:
                    return ParseList(asDictionaryKey, cache, null);
                case JsonToken.StartObject:
                    return ParseDictionary(asDictionaryKey, cache, null);
                case JsonToken.PropertyName:
                case JsonToken.String:
                    return cache.CacheRead((string)jp.Value, asDictionaryKey, this);
                case JsonToken.Float:
                case JsonToken.Boolean:
                    return jp.Value;
                case JsonToken.Null:
                    return null;

                case JsonToken.Bytes:
                case JsonToken.Comment:
                case JsonToken.Date:
                case JsonToken.EndArray:
                case JsonToken.EndConstructor:
                case JsonToken.EndObject:
                case JsonToken.None:
                case JsonToken.Raw:
                case JsonToken.StartConstructor:
                case JsonToken.Undefined:

                default:
                    return null;
            }
        }
Ejemplo n.º 20
0
 /// <summary>
 /// Parses the specified cache.
 /// </summary>
 /// <param name="cache">The cache.</param>
 /// <returns></returns>
 public abstract object Parse(ReadCache cache);