如何使用jsonschema2pojo-core将annotationStyle设置为Gson?

问题描述 投票:0回答:2

我正在使用 jsonschema2pojo-core 将 JSON 解析为 POJO。问题是我想将annotationStyle 设置为GSON。 (默认为杰克逊)。

有什么想法吗?

非常感谢。

json gson jsonschema2pojo
2个回答
0
投票

实现 GenerationConfig.java 或扩展 DefaultGenerationConfig.java

    ...
    @Override
    public AnnotationStyle getAnnotationStyle() {
        return AnnotationStyle.GSON;
    }
    ...

0
投票

如果对某人有帮助,请全面实施:

    public void convertJsonToJavaClass(String inputJsonStr, File outputJavaClassDirectory,
                                   String packageName, String javaClassName)
        throws IOException {
    JCodeModel jcodeModel = new JCodeModel();

    GenerationConfig config = new DefaultGenerationConfig() {
        @Override
        public boolean isGenerateBuilders() {
            return true;
        }

        @Override
        public SourceType getSourceType() {
            return SourceType.JSON;
        }
        @Override
        public AnnotationStyle getAnnotationStyle() {
            return AnnotationStyle.GSON;
        }
    };

    SchemaMapper mapper = new SchemaMapper(new RuleFactory(config, new Jackson2Annotator(config),
            new SchemaStore()), new SchemaGenerator());
    mapper.generate(jcodeModel, javaClassName, packageName, inputJsonStr);

    jcodeModel.build(outputJavaClassDirectory);

}
© www.soinside.com 2019 - 2024. All rights reserved.