public static List <object> ParseArray(Slice data)
        {
            if (data.Count == 0)
            {
                return(null);
            }
            char[] chars  = Encoding.UTF8.GetChars(data.Array, data.Offset, data.Count);
            var    parser = new TinyJsonParser(chars, 0, chars.Length);
            var    token  = parser.ReadToken();

            if (token == Token.Eof)
            {
                return(null);
            }
            var array = (List <object>)parser.m_current;

            if (token != Token.ArrayBegin)
            {
                throw new FormatException("Invalid JSON document: array expected");
            }
            token = parser.ReadToken();
            if (token != Token.Eof)
            {
                throw new FormatException("Invalid JSON document: extra data after array");
            }
            return(array);
        }
        internal static Dictionary <string, object> ParseObject([NotNull] char[] chars, int offset, int count)
        {
            Contract.Requires(chars != null && offset >= 0 && count >= 0);

            var parser = new TinyJsonParser(chars, offset, count);
            var token  = parser.ReadToken();

            if (token == Token.Eof)
            {
                return(null);
            }

            // ensure we got an object
            if (token != Token.MapBegin)
            {
                throw new InvalidOperationException(String.Format("JSON object expected, but got a {0}", token));
            }
            var map = (Dictionary <string, object>)parser.m_current;

            // ensure that there is nothing after the object
            token = parser.ReadToken();
            if (token != Token.Eof)
            {
                throw new InvalidOperationException("Extra data at the end of the JSON object");
            }
            return(map);
        }
示例#3
0
        public static List <object?>?ParseArray(ReadOnlyMemory <char> chars)
        {
            if (chars.Length == 0)
            {
                return(null);
            }

            var parser = new TinyJsonParser(chars);
            var token  = parser.ReadToken();

            if (token == Token.Eof)
            {
                return(null);
            }

            var array = (List <object?>?)parser.m_current;

            if (token != Token.ArrayBegin)
            {
                throw new FormatException("Invalid JSON document: array expected");
            }
            token = parser.ReadToken();
            if (token != Token.Eof)
            {
                throw new FormatException("Invalid JSON document: extra data after array");
            }
            return(array);
        }
示例#4
0
        public static Dictionary <string, object?>?ParseObject(ReadOnlyMemory <char> chars)
        {
            if (chars.Length == 0)
            {
                return(null);
            }

            var parser = new TinyJsonParser(chars);
            var token  = parser.ReadToken();

            if (token == Token.Eof)
            {
                return(null);
            }

            // ensure we got an object
            if (token != Token.MapBegin)
            {
                throw new InvalidOperationException($"JSON object expected, but got a {token}");
            }
            var map = (Dictionary <string, object?>?)parser.m_current;

            // ensure that there is nothing after the object
            token = parser.ReadToken();
            if (token != Token.Eof)
            {
                throw new InvalidOperationException("Extra data at the end of the JSON object");
            }
            return(map);
        }
		public static List<object> ParseArray(Slice data)
		{
			if (data.Count == 0) return null;
			char[] chars = Encoding.UTF8.GetChars(data.Array, data.Offset, data.Count);
			var parser = new TinyJsonParser(chars, 0, chars.Length);
			var token = parser.ReadToken();
			if (token == Token.Eof) return null;
			var array = (List<object>)parser.m_current;
			if (token != Token.ArrayBegin) throw new FormatException("Invalid JSON document: array expected");
			token = parser.ReadToken();
			if (token != Token.Eof) throw new FormatException("Invalid JSON document: extra data after array");
			return array;
		}
		internal static Dictionary<string, object> ParseObject([NotNull] char[] chars, int offset, int count)
		{
			Contract.Requires(chars != null && offset >= 0 && count >= 0);

			var parser = new TinyJsonParser(chars, offset, count);
			var token = parser.ReadToken();
			if (token == Token.Eof) return null;

			// ensure we got an object
			if (token != Token.MapBegin) throw new InvalidOperationException(String.Format("JSON object expected, but got a {0}", token));
			var map = (Dictionary<string, object>)parser.m_current;

			// ensure that there is nothing after the object
			token = parser.ReadToken();
			if (token != Token.Eof) throw new InvalidOperationException("Extra data at the end of the JSON object");
			return map;
		}