示例#1
0
        public static object parseSpan(ReadOnlySpan <char> json)
        {
            if (json.Length == 0)
            {
                return(null);
            }
            var firstChar = json[0];

            if (firstChar >= '0' && firstChar <= '9')
            {
                try {
                    var longValue = MemoryProvider.Instance.ParseInt64(json);
                    return(longValue >= int.MinValue && longValue <= int.MaxValue
                        ? (int)longValue
                        : longValue);
                } catch {}

                if (json.TryParseDouble(out var doubleValue))
                {
                    return(doubleValue);
                }
            }

            if (firstChar == '{' || firstChar == '[')
            {
                json.ParseJsToken(out var token);
                return(token.Evaluate(JS.CreateScope()));
            }

            return(json.ToString());
        }
示例#2
0
        public static object parseSpan(ReadOnlySpan <char> json)
        {
            if (json.Length == 0)
            {
                return(null);
            }
            var firstChar = json[0];

            bool isEscapedJsonString(ReadOnlySpan <char> js) =>
            js.StartsWith(@"{\") || js.StartsWith(@"[{\");

            if (firstChar >= '0' && firstChar <= '9')
            {
                try {
                    var longValue = MemoryProvider.Instance.ParseInt64(json);
                    return(longValue >= int.MinValue && longValue <= int.MaxValue
                        ? (int)longValue
                        : longValue);
                } catch {}

                if (json.TryParseDouble(out var doubleValue))
                {
                    return(doubleValue);
                }
            }
            else if (firstChar == '{' || firstChar == '[' &&
                     !isEscapedJsonString(json.TrimStart()))
            {
                json.ParseJsToken(out var token);
                return(token.Evaluate(JS.CreateScope()));
            }
            else if (json.Length == 4)
            {
                if (firstChar == 't' && json[1] == 'r' && json[2] == 'u' && json[3] == 'e')
                {
                    return(true);
                }
                if (firstChar == 'n' && json[1] == 'u' && json[2] == 'l' && json[3] == 'l')
                {
                    return(null);
                }
            }
            else if (json.Length == 5 && firstChar == 'f' && json[1] == 'a' && json[2] == 'l' && json[3] == 's' && json[4] == 'e')
            {
                return(false);
            }

            var unescapedString = JsonTypeSerializer.Unescape(json);

            return(unescapedString.ToString());
        }
示例#3
0
 public static object parse(string json)
 {
     json.AsSpan().ParseJsToken(out var token);
     return(token.Evaluate(JS.CreateScope()));
 }