Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

FieldTypeResolver

wenshao edited this page Apr 13, 2016 · 9 revisions

FieldTypeResolver是1.2.9 & 1.1.49.android中引入的新功能,用于解析嵌套对象时,自动识别子对象的类型信息。FieldTypeResolver只会作用于value类型为json object的字段。

例如:

public static class Item {
    public int value;
}

FieldTypeResolver fieldResolver = new FieldTypeResolver() {
    public Type resolve(Object object, String fieldName) {
        if (fieldName.startsWith("item_")) {
            return Item.class;
        }
        
        return null;
    }
};

String text = "{\"item_0\":{},\"item_1\":{},\"item_2\":1001}";
JSONObject jsonObject = JSON.parseObject(text, JSONObject.class, fieldResolver);
Assert.assertTrue(jsonObject.get("item_0") instanceof Item);
Assert.assertTrue(jsonObject.get("item_1") instanceof Item);
Assert.assertTrue(jsonObject.get("item_2") instanceof Integer);

例子中可以看出,item_2的value没有被FieldTypeResolver影响,类型还是Integer,因为value是Integer,而不是Object。

Clone this wiki locally