-
Notifications
You must be signed in to change notification settings - Fork 495
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG] jackson的注解和fastjson的注解有些冲突,并且不同顺序表现不一致 #1985
Comments
能帮构造重现问题的testcase么? |
写了个可以重现的demo DemoBeanpackage com.example.json;
import com.alibaba.fastjson2.annotation.JSONType;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.Data;
import java.util.Map;
@Data
@JsonSerialize(using = DemoJacksonSerializer.class)
@JSONType(serializer = DemoFastJsonSerializer.class)
// 下方注释为调换顺序后,结果异常
// @JSONType(serializer = DemoFastJsonSerializer.class)
// @JsonSerialize(using = DemoJacksonSerializer.class)
public class DemoBean {
private String type;
private Map<String, Object> map;
} JacksonSerializerpackage com.example.json;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
import java.util.Map;
public class DemoJacksonSerializer extends JsonSerializer<DemoBean> {
@Override
public void serialize(DemoBean demoBean, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeStartObject();
for (Map.Entry<String, Object> entry : demoBean.getMap().entrySet()) {
gen.writeFieldName(entry.getKey());
gen.writeObject(entry.getValue());
}
gen.writeEndObject();
}
} FastJsonSerializerpackage com.example.json;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.writer.ObjectWriter;
import java.lang.reflect.Type;
import java.util.Map;
public class DemoFastJsonSerializer implements ObjectWriter<DemoBean> {
@Override
public void write(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) {
jsonWriter.startObject();
DemoBean demoBean = (DemoBean) object;
for (Map.Entry<String, Object> entry : demoBean.getMap().entrySet()) {
jsonWriter.writeName(entry.getKey());
jsonWriter.writeColon();
jsonWriter.writeAny(entry.getValue());
}
jsonWriter.endObject();
}
} 测试类package com.example.json;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.json.JsonMapper;
import org.junit.jupiter.api.Test;
import java.util.Map;
class JsonTest {
@Test
public void test() throws JsonProcessingException {
DemoBean demoBean = new DemoBean();
demoBean.setType("test");
demoBean.setMap(Map.of("a", 1));
// 期望结果:{"a":1}
System.out.println(JSON.toJSONString(demoBean));
JsonMapper mapper = new JsonMapper();
System.out.println(mapper.writeValueAsString(demoBean));
}
} 执行结果:
|
https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson/2.0.42-SNAPSHOT/ |
借着issue咨询一下,fastjson2能否兼容掉jackson的序列化反序列化注解JsonDeserialize JsonSerialize |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
问题描述
有个类,同时增加了自定义序列化的注解如下
此顺序下,fastjson和jackson均可以正常序列化
改为如下
此顺序下,jackson序列化正常,json自定义序列化失效,貌似走了默认序列化
环境信息
请填写以下信息:
The text was updated successfully, but these errors were encountered: