fastjson缺省使用CamelCase,在1.2.15版本之后,fastjson支持配置PropertyNamingStrategy,支持如下四种:
name demo
CamelCase persionId
PascalCase PersonId
SnakeCase person_id
KebabCase person-id
使用方式1
SerializeConfig config = new SerializeConfig(); // 生产环境中,config要做singleton处理,要不然会存在性能问题
config.propertyNamingStrategy = PropertyNamingStrategy.SnakeCase;
Model model = new Model();
model.personId = 1001;
String text = JSON.toJSONString(model, config);
Assert.assertEquals("{\"person_id\":1001}", text);
ParserConfig parserConfig = new ParserConfig(); // 生产环境中,parserConfig要做singleton处理,要不然会存在性能问题
parserConfig.propertyNamingStrategy = PropertyNamingStrategy.SnakeCase;
Model model2 = JSON.parseObject(text, Model.class, parserConfig);
Assert.assertEquals(model.personId, model2.personId);
(1)可以写线程安全的单例工具类
(2)可以通过springxml方式或者java config方式构造单例的SerializeConfig的bean,在需要的地方注入使用即。建议命名为对应风格名称+SerializeConfig,如snakeCaseSerializeConfig。