提问者:小点点

如何反序列化JSON字符串以对象放松根值大小写敏感性?


在将我的项目从Jersey移动到Spring MVC时面临问题。如何在Jackson中放松根值的不区分大小写?

我想支持大写和小写。

我们有下面的Jackson配置,它适用于属性和枚举,但不适用于根值

spring.jackson.mapper.accept-case-insensitive-properties=true
spring.jackson.mapper.accept-case-insensitive-enums=true

就我而言,我无法访问 Car 类,因此无法使用任何杰克逊注释(如 @JsonRootValue)将 Car 类的名称更新为“car”

这是一个带有测试的示例类,以重现我所面临的问题。

在jackson库中有放宽根名的配置不是很好吗?

public class CarTest {

    public class Car {
        String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
    
    @Test
    public void testCarRootValueCaseSensitivity() throws IOException {
        Car car = new Car();
        car.setName("audi");

        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
        objectMapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
        String carAsString = objectMapper.writeValueAsString(car);
        System.out.println(carAsString);

        // Works fine with out any exception as the root value Car has captital 'C'
        objectMapper.readValue("{\"Car\":{\"name\":\"audi\"}}", Car.class);

        // Throws exception when lower case 'c' is provided than uppercase 'C'
        objectMapper.readValue("{\"car\":{\"name\":\"audi\"}}", Car.class);
    }
}

共1个答案

匿名用户

当您查看抛出的异常时:

Exception in thread "main" com.fasterxml.jackson.databind.exc.MismatchedInputException: Root name 'car' does not match expected ('Car') for type [simple type, class com.example.Car]
 at [Source: (String)"{"car":{"name":"audi"}}"; line: 1, column: 2] (through reference chain: com.example.Car["car"])
    at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63)
    at com.fasterxml.jackson.databind.DeserializationContext.reportPropertyInputMismatch(DeserializationContext.java:1477)
    at com.fasterxml.jackson.databind.DeserializationContext.reportPropertyInputMismatch(DeserializationContext.java:1493)
    at com.fasterxml.jackson.databind.ObjectMapper._unwrapAndDeserialize(ObjectMapper.java:4286)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4200)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3205)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3173)

您可以注意到_unwrapAndDeserialize方法名称。

在这个方法中你可以找到这段代码:

String actualName = p.getCurrentName();
if (!expSimpleName.equals(actualName)) {
   ctxt.reportPropertyInputMismatch(rootType, actualName,
       "Root name '%s' does not match expected ('%s') for type %s",
       actualName, expSimpleName, rootType);
}

由于始终使用< code>equals方法,因此无法配置此行为。

如果您真的想用不区分大小写的模式解析它,可以重写<code>_unwrapAndDeserialize</code>方法,并用<code>equals IgnoreCase</code<替换<code>等于</code〕。示例类:

class CaseInsensitiveObjectMapper extends ObjectMapper {
    protected Object _unwrapAndDeserialize(JsonParser p, DeserializationContext ctxt, DeserializationConfig config, JavaType rootType, JsonDeserializer<Object> deser) throws IOException {
        PropertyName expRootName = config.findRootName(rootType);
        // 12-Jun-2015, tatu: Should try to support namespaces etc but...
        String expSimpleName = expRootName.getSimpleName();
        if (p.getCurrentToken() != JsonToken.START_OBJECT) {
            ctxt.reportWrongTokenException(rootType, JsonToken.START_OBJECT,
                    "Current token not START_OBJECT (needed to unwrap root name '%s'), but %s",
                    expSimpleName, p.getCurrentToken());
        }
        if (p.nextToken() != JsonToken.FIELD_NAME) {
            ctxt.reportWrongTokenException(rootType, JsonToken.FIELD_NAME,
                    "Current token not FIELD_NAME (to contain expected root name '%s'), but %s",
                    expSimpleName, p.getCurrentToken());
        }
        String actualName = p.getCurrentName();
        if (!expSimpleName.equalsIgnoreCase(actualName)) {
            ctxt.reportPropertyInputMismatch(rootType, actualName,
                    "Root name '%s' does not match expected ('%s') for type %s",
                    actualName, expSimpleName, rootType);
        }
        // ok, then move to value itself....
        p.nextToken();
        Object result = deser.deserialize(p, ctxt);
        // and last, verify that we now get matching END_OBJECT
        if (p.nextToken() != JsonToken.END_OBJECT) {
            ctxt.reportWrongTokenException(rootType, JsonToken.END_OBJECT,
                    "Current token not END_OBJECT (to match wrapper object with root name '%s'), but %s",
                    expSimpleName, p.getCurrentToken());
        }
        if (config.isEnabled(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)) {
            _verifyNoTrailingTokens(p, ctxt, rootType);
        }
        return result;
    }
}

我从< code>ObjectMapper类(版本< code>2.10.1)复制了整个方法,如果您要升级< code>Jackson版本,您需要检查该方法的实现情况,并在需要时替换它。

最后,您可以在测试中使用这种新类型:

ObjectMapper objectMapper = new CaseInsensitiveObjectMapper();

另见:

  • JSON区分大小写吗?