Example #1
0
        internal new static JObject Parse(JsonStream jsonStream)
        {
            jsonStream.MoveToNextContent();
            if (!'{'.Equals(jsonStream.CurrentChar))
            {
                throw ExceptionHelpers.MakeJsonErrorException(jsonStream);
            }
            List <KeyValuePair <string, JToken> > properties = new List <KeyValuePair <string, JToken> >();

            jsonStream.Move();
            jsonStream.MoveToNextContent();

            if (jsonStream.IsStartOfString())
            {
                while (true)
                {
                    var prop = GetNextProperty(jsonStream);
                    properties.Add(prop);

                    jsonStream.MoveToNextContent();
                    if (','.Equals(jsonStream.CurrentChar))
                    {
                        jsonStream.Move();
                        jsonStream.MoveToNextContent();
                        if (jsonStream.IsStartOfString())
                        {
                            continue;
                        }
                        else if ('}'.Equals(jsonStream.CurrentChar))
                        {
                            break;
                        }
                        else
                        {
                            throw ExceptionHelpers.MakeJsonErrorException(jsonStream);
                        }
                    }
                    else if ('}'.Equals(jsonStream.CurrentChar))
                    {
                        break;
                    }
                    else
                    {
                        throw ExceptionHelpers.MakeJsonErrorException(jsonStream);
                    }
                }
            }
            else if (!'}'.Equals(jsonStream.CurrentChar))
            {
                throw ExceptionHelpers.MakeJsonErrorException(jsonStream);
            }

            jsonStream.Move();
            var answer = new JObject();

            answer.m_items = new Dictionary <string, JToken>(properties);
            return(answer);
        }
Example #2
0
        public static JToken ParseNew(string json)
        {
            var jsonStream = new JsonStream(json);
            var answer     = Parse(jsonStream);

            if (jsonStream.HasNextContent())
            {
                throw ExceptionHelpers.MakeJsonErrorException(jsonStream);
            }
            return(answer);
        }
Example #3
0
        private static KeyValuePair <String, JToken> GetNextProperty(JsonStream jsonStream)
        {
            var propName = jsonStream.MoveBehindStringAndGet();

            jsonStream.MoveToNextContent();
            if (!':'.Equals(jsonStream.CurrentChar))
            {
                throw ExceptionHelpers.MakeJsonErrorException(jsonStream);
            }
            jsonStream.Move();
            var propValue = JToken.Parse(jsonStream);

            return(new KeyValuePair <string, JToken>(propName, propValue));
        }
        internal new static JArray Parse(JsonStream jsonStream)
        {
            jsonStream.MoveToNextContent();
            if (!'['.Equals(jsonStream.CurrentChar))
            {
                throw ExceptionHelpers.MakeJsonErrorException(jsonStream);
            }

            jsonStream.Move();
            jsonStream.MoveToNextContent();

            List <JToken> values = new List <JToken>();

            if (!']'.Equals(jsonStream.CurrentChar))
            {
                while (true)
                {
                    var value = JToken.Parse(jsonStream);
                    values.Add(value);
                    jsonStream.MoveToNextContent();
                    if (','.Equals(jsonStream.CurrentChar))
                    {
                        jsonStream.Move();
                        jsonStream.MoveToNextContent();
                        if (']'.Equals(jsonStream.CurrentChar))
                        {
                            break;
                        }
                    }
                    else if (']'.Equals(jsonStream.CurrentChar))
                    {
                        break;
                    }
                }
            }

            jsonStream.Move();
            var answer = new JArray(values);

            return(answer);
        }
Example #5
0
        protected static JToken Parse(JsonStream jsonStream)
        {
            JToken answer;
            var    contentChar = jsonStream.MoveToNextContent();

            switch (contentChar)
            {
            case '{':
                answer = JObject.Parse(jsonStream);
                break;

            case '[':
                answer = JArray.Parse(jsonStream);
                break;

            default:
                answer = JValue.Parse(jsonStream);
                break;
            }

            return(answer);
        }
 public static Exception MakeJsonErrorException(JsonStream jsonStream)
 {
     return(new Exception($"Can not parse json string. Json error at index: {jsonStream.CurrentIndex}"));
 }
Example #7
0
        internal new static JValue Parse(JsonStream jsonStream)
        {
            JValue answer;

            switch (jsonStream.CurrentChar)
            {
            case 'n':
                if (jsonStream.HasNullFollow())
                {
                    answer = new JValue(null);
                    jsonStream.MoveBehindNull();
                }
                else
                {
                    throw ExceptionHelpers.MakeJsonErrorException(jsonStream);
                }
                break;

            case 't':
                if (jsonStream.HasTrueFollow())
                {
                    answer = (JValue)true;
                    jsonStream.MoveBehindTrue();
                }
                else
                {
                    throw ExceptionHelpers.MakeJsonErrorException(jsonStream);
                }
                break;

            case 'f':
                if (jsonStream.HasFalseFollow())
                {
                    answer = (JValue)false;
                    jsonStream.MoveBehindFalse();
                }
                else
                {
                    throw ExceptionHelpers.MakeJsonErrorException(jsonStream);
                }
                break;

            default:
                if (jsonStream.IsStartOfString())
                {
                    var content = jsonStream.MoveBehindStringAndGet();
                    answer = (JValue)content;
                }
                else if (jsonStream.IsStartOfNumber())
                {
                    var number = jsonStream.MoveBehindNumberAndGet();
                    answer = (JValue)double.Parse(number);
                }
                else
                {
                    throw ExceptionHelpers.MakeJsonErrorException(jsonStream);
                }
                break;
            }
            return(answer);
        }