skip to Main Content

Why fastjson and Jackson both judge the string ""[]"" a valid JSON?

@Test
public void validJSON() {
    String jsonString1 = ""[]"";
    System.out.println("fastjson: " + JSON.isValid(jsonString1));

    ObjectMapper objectMapper = new ObjectMapper();
    boolean flag = false;
    try {
        JsonNode jsonNode1 = objectMapper.readTree(jsonString1);
        flag = true;
    } catch (Exception e) {
        flag = false;
    }
    System.out.println("jackjson: " + flag);
}

result:

fastjson: true
jackjson: true

I want to know why ""[]"" is a valid JSON string.

2

Answers


  1. Because the example boils down to – "literary whatever you want here". The specification says that (almost) anything enclosed in double quotes is a valid string, the rest should be escaped to be part of the string. Check this as well.

    And a string is a valid json.

    Login or Signup to reply.
  2. ObjectMapper is usually used for serialize/deserialize Java objects, and a "[]" string is equal to an Empty Array in Java so de-serializing "[]" is a valid string for ObjectMapper.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search