Ejemplo n.º 1
0
        private static string GetNextPropertyName(string json, int start, out int endIndex)
        {
            for (int i = start; i < json.Length; i++)
            {
                var c = json[i];
                if (' '.Equals(c))
                {
                    continue;
                }
                else if ('"'.Equals(c) || '\''.Equals(c))
                {
                    var index = json.FindNextCharIndexFromIndex(c, i + 1);
                    var name  = json.Substring(i + 1, index - i - 1);
                    endIndex = index;
                    return(name);
                }
                else
                {
                    throw ExceptionHelpers.MakeJsonErrorException(json, i);
                }
            }

            throw ExceptionHelpers.MakeJsonErrorException(json, start);
        }
Ejemplo n.º 2
0
        internal static JToken Parse(string json, int start, out int endIndex)
        {
            JToken answer = null;

            endIndex = start;
            for (int i = start; i < json.Length; i++)
            {
                if (' '.Equals(json[i]))
                {
                    continue;
                }
                var c = json[i];
                if ('{'.Equals(c))
                {
                    answer = JObject.Parse(json, i, out endIndex);
                    break;
                }
                else if ('['.Equals(c))
                {
                    answer = JArray.Parse(json, i, out endIndex);
                    break;
                }
                else if ('"'.Equals(c) || '\''.Equals(c))
                {
                    var nextIndex = json.FindNextCharIndexFromIndex(c, i + 1);
                    if (nextIndex == -1)
                    {
                        throw ExceptionHelpers.MakeJsonErrorException(json, i);
                    }
                    var content = json.Substring(i + 1, nextIndex - i - 1);
                    answer   = (JToken)content;
                    endIndex = nextIndex;
                    break;
                }
                else if ('.'.Equals(c) || (c >= '0' && c <= '9'))
                {
                    var number = json.FindNumberFollowByIndex(i);
                    endIndex = i + number.Length - 1;
                    answer   = (JToken)double.Parse(number);
                    break;
                }
                else if (json.HasStringFollowByIndex("null", i))
                {
                    answer   = new JValue(null);
                    endIndex = i + 3;
                    break;
                }
                else if (json.HasStringFollowByIndex("true", i))
                {
                    answer   = (JToken)true;
                    endIndex = i + 3;
                    break;
                }
                else if (json.HasStringFollowByIndex("false", i))
                {
                    answer   = (JToken)false;
                    endIndex = i + 4;
                    break;
                }
                else
                {
                    throw ExceptionHelpers.MakeJsonErrorException(json, i);
                }
            }
            return(answer);
        }
Ejemplo n.º 3
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);
        }