杰克逊的JSON(2.5.0)验证无法正常工作

gtg*_*ola 3 java json jackson

简单的测试案例:

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public static void main(String[] args) throws Exception {
    String json = "1,2";
    ObjectMapper parser = new ObjectMapper();
    JsonNode rootNode = parser.readTree(json);

}
Run Code Online (Sandbox Code Playgroud)

引发异常:

Exception in thread "main" com.fasterxml.jackson.core.JsonParseException: 
Unexpected character (',' (code 44)): Expected space separating root-level values
at [Source: 1,2; line: 1, column: 3]
Run Code Online (Sandbox Code Playgroud)

一切都很好,但如果我更改String json为以下任何一个,则:

String json = "null,false";
String json = "[1,2,3,null,\"hello\"],false";
String json = "true,3";
String json = "true,{\"test\":3}";
Run Code Online (Sandbox Code Playgroud)

没有抛出异常。

为什么会有差异?

vto*_*tor 5

默认情况下,Jackson使用ReaderBasedJsonParser,其中在读取/初始化要解析的JSON时会进行显式检查

switch (i) {
        case '"':
            _tokenIncomplete = true;
            t = JsonToken.VALUE_STRING;
            break;
        case '[':
            if (!inObject) {
                _parsingContext = _parsingContext.createChildArrayContext(_tokenInputRow, _tokenInputCol);
            }
            t = JsonToken.START_ARRAY;
            break;
        case '{':
            if (!inObject) {
                _parsingContext = _parsingContext.createChildObjectContext(_tokenInputRow, _tokenInputCol);
            }
            t = JsonToken.START_OBJECT;
            break;
        case ']':
        case '}':
            // Error: neither is valid at this point; valid closers have
            // been handled earlier
            _reportUnexpectedChar(i, "expected a value");
        case 't':
            _matchTrue();
            t = JsonToken.VALUE_TRUE;
            break;
        case 'f':
            _matchFalse();
            t = JsonToken.VALUE_FALSE;
            break;
        case 'n':
            _matchNull();
            t = JsonToken.VALUE_NULL;
            break;

        case '-':
            /* Should we have separate handling for plus? Although
             * it is not allowed per se, it may be erroneously used,
             * and could be indicate by a more specific error message.
             */
            t = _parseNegNumber();
            break;
        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
            t = _parsePosNumber(i);
            break;
Run Code Online (Sandbox Code Playgroud)

从上面的代码中,您可以看到它检查了您提到的字符-null,true,{,[,false等。”如果匹配,它将返回有效的json令牌。那就是为什么,如果你改变

String json = "true,3";
Run Code Online (Sandbox Code Playgroud)

String json = "3,true";
Run Code Online (Sandbox Code Playgroud)

它将再次引发众所周知的异常。

请注意,还检查了以字母“ n”开头的给定字符串,以验证它是否与null匹配(对以“ t”,“ f”开头的字符串也进行了同样的检查)。

然后有趣的是_parsePosNumber(i)方法,该方法基本上要求_reportMissingRootWS检查根-并且显然失败。

因此,基本上,如果json有效,则永远不会达到“失败者”的情况,或者给定的字符串以那些“ true”,“ false”,“ null”,“ [”,“ {”,“]”,“}”中的on开头”也将被视为有效的json。

我假设此功能在Jackson 非标准功能列表中,因此上述格式被视为有效JSON。