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

@JSONField 的 deserializeUsing 不起作用? #1662

@fongx

Description

@fongx

用下面的代码测试,发现并没有调用 @JSONField 注解上 deserializeUsing 指定的 ModelValueDeserializer 类?
版本 1.2.25, 1.2.32 以及最新的 1.2.43 都有问题

public class Main2 {

    public static void main(String[] args) {
        //ParserConfig.getGlobalInstance().putDeserializer(Model.class, new ModelValueDeserializer());
        String json = "{\"value\":123}";
        Model model = JSON.parseObject(json, Model.class);
        System.out.println(JSON.toJSONString(model));
    }

    public static class Model {
        @JSONField(serializeUsing = ModelValueSerializer.class, deserializeUsing = ModelValueDeserializer.class)
        public int value;
    }

    public static class ModelValueSerializer implements ObjectSerializer {
        @Override
        public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType,
                          int features) throws IOException {
            Integer value = (Integer) object;
            String text = value + "元";
            serializer.write(text);
        }
    }

    public static class ModelValueDeserializer implements ObjectDeserializer {

        @Override
        public Integer deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
            return parser.getLexer().intValue() * 100;
        }

        @Override
        public int getFastMatchToken() {
            return 0;
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions