由于特殊原因我设置了全局PropertyNamingStrategy.SnakeCase,但有些类需要单独设置驼峰序列化PropertyNamingStrategy.CamelCase,于是在类上加了@jsontype(naming = PropertyNamingStrategy.CamelCase),但不起作用。com.alibaba fastjson 1.2.75
bbuxkriu1#
复现代码如下:
import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.PropertyNamingStrategy;import com.alibaba.fastjson.annotation.JSONType;import com.alibaba.fastjson.serializer.SerializeConfig;import lombok.AllArgsConstructor;import lombok.Data;import org.junit.Test;public class Issue3652 { @Test public void test_SerializeConfig_different_Class_Annotation() { Object[] models = new Object[]{ new Model1("hello,world"), new Model2("hello,world"), new Model3("hello,world"), new Model4("hello,world"), }; for (int i = 0; i < 4; i++) { System.out.printf("---------- begin circle %d%n", i); for (int j = 0; j < 4; j++) { SerializeConfig config = new SerializeConfig(); config.propertyNamingStrategy = PropertyNamingStrategy.values()[j]; System.out.println(JSON.toJSONString(models[i], config)); } System.out.printf("---------- end circle %d%n", i); } } @JSONType(naming = PropertyNamingStrategy.CamelCase) @Data @AllArgsConstructor public class Model1 { private String goodBoy; } @JSONType(naming = PropertyNamingStrategy.PascalCase) @Data @AllArgsConstructor public class Model2 { private String goodBoy; } @JSONType(naming = PropertyNamingStrategy.SnakeCase) @Data @AllArgsConstructor public class Model3 { private String goodBoy; } @JSONType(naming = PropertyNamingStrategy.KebabCase) @Data @AllArgsConstructor public class Model4 { private String goodBoy; }}
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.PropertyNamingStrategy;
import com.alibaba.fastjson.annotation.JSONType;
import com.alibaba.fastjson.serializer.SerializeConfig;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.junit.Test;
public class Issue3652 {
@Test
public void test_SerializeConfig_different_Class_Annotation() {
Object[] models = new Object[]{
new Model1("hello,world"),
new Model2("hello,world"),
new Model3("hello,world"),
new Model4("hello,world"),
};
for (int i = 0; i < 4; i++) {
System.out.printf("---------- begin circle %d%n", i);
for (int j = 0; j < 4; j++) {
SerializeConfig config = new SerializeConfig();
config.propertyNamingStrategy = PropertyNamingStrategy.values()[j];
System.out.println(JSON.toJSONString(models[i], config));
}
System.out.printf("---------- end circle %d%n", i);
@JSONType(naming = PropertyNamingStrategy.CamelCase)
@Data
@AllArgsConstructor
public class Model1 {
private String goodBoy;
@JSONType(naming = PropertyNamingStrategy.PascalCase)
public class Model2 {
@JSONType(naming = PropertyNamingStrategy.SnakeCase)
public class Model3 {
@JSONType(naming = PropertyNamingStrategy.KebabCase)
public class Model4 {
输出为
---------- begin circle 0{"goodBoy":"hello,world"}{"GoodBoy":"hello,world"}{"good_boy":"hello,world"}{"good-boy":"hello,world"}---------- end circle 0---------- begin circle 1{"GoodBoy":"hello,world"}{"GoodBoy":"hello,world"}{"GoodBoy":"hello,world"}{"GoodBoy":"hello,world"}---------- end circle 1---------- begin circle 2{"good_boy":"hello,world"}{"good_boy":"hello,world"}{"good_boy":"hello,world"}{"good_boy":"hello,world"}---------- end circle 2---------- begin circle 3{"good-boy":"hello,world"}{"good-boy":"hello,world"}{"good-boy":"hello,world"}{"good-boy":"hello,world"}---------- end circle 3
---------- begin circle 0
{"goodBoy":"hello,world"}
{"GoodBoy":"hello,world"}
{"good_boy":"hello,world"}
{"good-boy":"hello,world"}
---------- end circle 0
---------- begin circle 1
---------- end circle 1
---------- begin circle 2
---------- end circle 2
---------- begin circle 3
---------- end circle 3
当类注解为 PropertyNamingStrategy.CamelCase 时,注解是无效的,会按照config输出.原因就是上图红框框出来的那个判断.
PropertyNamingStrategy.CamelCase
如图,JSONType的默认naming为 PropertyNamingStrategy.CamelCase , 所以才需要框内的判断去避免"没有声明naming却使用了CamelCase"的情况.
1条答案
按热度按时间bbuxkriu1#
复现代码如下:
输出为
当类注解为
PropertyNamingStrategy.CamelCase
时,注解是无效的,会按照config输出.原因就是上图红框框出来的那个判断.
如图,JSONType的默认naming为
PropertyNamingStrategy.CamelCase
, 所以才需要框内的判断去避免"没有声明naming却使用了CamelCase"的情况.