fastjson属性名设置为PascalCase、SnakeCase、KebabCase
策。。。
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 = JSONString(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。
使⽤⽅式2:基于JSONType配置PropertyNamingStrategy
@JSONType(naming=PropertyNamingStrategy.SnakeCase)
public class Cat {
private String name;
private Integer age;
private String sex;
private String testThis;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getTestThis() {
return testThis;
}
public void setTestThis(String testThis) {
}
}
public class FastJsonTest {
@Test
public void propertyNamingStrategyTest(){
Cat cat = new Cat();
cat.setName("张三");
cat.setAge(0);
cat.setSex("male");
cat.setTestThis("testIt");
Assert.assertEquals("{\"age\":0,\"name\":\"张三\",\"sex\":\"male\",\"test_this\":\"testIt\"}", JSONString(cat) ); }
}
⽅式3:Spring单例bean⽅式
@Configuration
public class FastJsonSerializeConfig {
@Bean("snakeCaseSerializeConfig")
@Scope("singleton")
public SerializeConfig snakeCaseSerializeConfig() {
SerializeConfig config = new SerializeConfig();
config.propertyNamingStrategy = PropertyNamingStrategy.SnakeCase;
return config;
}
@Bean("pascalCasSerializeConfig")
@Scope("singleton")
public SerializeConfig pascalCasSerializeConfig() {
SerializeConfig config = new SerializeConfig();
config.propertyNamingStrategy = PropertyNamingStrategy.PascalCase;
return config;
}
}
单元测试
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class FastJsonTest {
@Resource(name = "snakeCaseSerializeConfig")
private SerializeConfig snakeCaseSerializeConfig;
@Resource(name = "pascalCasSerializeConfig")
private SerializeConfig pascalCasSerializeConfig;
@Test
public void propertyNamingStrategyTest2(){
Cat cat = new Cat();
cat.setName("张三");fastjson忽略属性
cat.setAge(0);
cat.setSex("male");
cat.setTestThis("testIt");
System.out.JSONString(cat,pascalCasSerializeConfig));
System.out.JSONString(cat,snakeCaseSerializeConfig));
Assert.assertEquals("{\"age\":0,\"name\":\"张三\",\"sex\":\"male\",\"test_this\":\"testIt\"}", JSONString(cat,snakeCaseSerializeConfig) ); }
}
修改全局缺省的命名策略
.propertyNamingStrategy = PropertyNamingStrategy.PascalCase;
另外由于fastjson的策略是枚举⽅式,暂时不⽀持⾃定义新的统⼀的属性名策略,如果有其他需求可以通过
@JSONField(name = "xxx")来指定每个属性的名称。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论