Skip to content
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

Closed
yimik opened this issue Nov 2, 2023 · 5 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@yimik
Copy link

yimik commented Nov 2, 2023

问题描述

有个类,同时增加了自定义序列化的注解如下

@JsonSerialize(using = MyJacksonSerializer.class)
@JSONType(serializer = MyFastjsonSerializer.class)

此顺序下,fastjson和jackson均可以正常序列化

改为如下

@JSONType(serializer = MyFastjsonSerializer.class)
@JsonSerialize(using = MyJacksonSerializer.class)

此顺序下,jackson序列化正常,json自定义序列化失效,貌似走了默认序列化

环境信息

请填写以下信息:

  • OS信息: Win11
  • JDK信息: OpenJDK 17
  • 版本信息:Fastjson2 2.0.41
@yimik yimik added the bug Something isn't working label Nov 2, 2023
@wenshao
Copy link
Member

wenshao commented Nov 2, 2023

能帮构造重现问题的testcase么?

@yimik
Copy link
Author

yimik commented Nov 3, 2023

能帮构造重现问题的testcase么?

写了个可以重现的demo

DemoBean

package 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;
}

JacksonSerializer

package 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();
    }
}

FastJsonSerializer

package 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));
    }
}

执行结果:

上述代码执行结尾如下
{"a":1}
{"a":1}

将DemoBean中注释放开(即调换注解顺序)执行结果如下:
{"map":{"a":1},"type":"test"}
{"a":1}

@wenshao wenshao added this to the 2.0.42 milestone Nov 5, 2023
@wenshao wenshao added the fixed label Nov 5, 2023
@wenshao
Copy link
Member

wenshao commented Nov 5, 2023

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson/2.0.42-SNAPSHOT/
问题已修复,请帮忙用2.0.42-SNAPSHOT版本验证,正式版本预计今晚发布。

@wenshao
Copy link
Member

wenshao commented Nov 5, 2023

https://github.com/alibaba/fastjson2/releases/tag/2.0.42
问题已修复,请用新版本

@wenshao wenshao closed this as completed Nov 5, 2023
@wwulfric
Copy link

借着issue咨询一下,fastjson2能否兼容掉jackson的序列化反序列化注解JsonDeserialize JsonSerialize

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed
Projects
None yet
Development

No branches or pull requests

3 participants