Skip to content

[BUG] JSONB反序列化异常,字段值为0时,经过JSONB序列化、反序列化后为字段值为-1 #3842

@luxiao0000

Description

@luxiao0000

问题描述

JSONB反序列化异常,字段值为0时,经过JSONB序列化、反序列化后为字段值为-1

如果去除UserData类errorCode字段的默认值,无此问题

2.0.55是最后正常的版本
2.0.56~2.0.59异常

环境信息

  • OS信息: intel macos
  • JDK信息:Temurin-21.0.2+13
  • 版本信息:Fastjson2 2.0.56

重现步骤

public class TestFastJson2Serialization {

public static void main(String[] args) throws Exception {
    System.out.println(test(-99));
    System.out.println(test(0));
    System.out.println(test(99));
}

private static int test(int errorCode) {
    UserData u1 = new UserData();
    u1.setErrorCode(errorCode);

    byte[] bytes = JSONB.toBytes(
            u1,
            JSONWriter.Feature.WriteClassName,
            JSONWriter.Feature.FieldBased,
            JSONWriter.Feature.ReferenceDetection,
            JSONWriter.Feature.WriteNulls,
            JSONWriter.Feature.NotWriteDefaultValue,
            JSONWriter.Feature.NotWriteHashMapArrayListClassName,
            JSONWriter.Feature.WriteNameAsSymbol);

    UserData u2 = JSONB.parseObject(
            bytes,
            UserData.class,
            JSONReader.Feature.UseDefaultConstructorAsPossible,
            JSONReader.Feature.IgnoreAutoTypeNotMatch,
            JSONReader.Feature.UseNativeObject,
            JSONReader.Feature.FieldBased
    );
    return u2.getErrorCode();
}


public static class UserData implements Serializable {

    private static final long serialVersionUID = 8521716552408936209L;

    private int errorCode = -1;
    // private int errorCode;

    public int getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(int errorCode) {
        this.errorCode = errorCode;
    }
}

}

期待的正确结果

-99
0
99

相关日志输出

-99
-1
99

附加信息

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixed

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions