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] WriteNonStringValueAsString with Boolean #2560

Closed
Cooper-Zhong opened this issue May 11, 2024 · 3 comments
Closed

[BUG] WriteNonStringValueAsString with Boolean #2560

Cooper-Zhong opened this issue May 11, 2024 · 3 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@Cooper-Zhong
Copy link
Contributor

问题描述

WriteNonStringValueAsString没有处理Boolean类型

环境信息

  • OS信息: [MacOS 12.7.4 M1 Pro 16 GB]
  • JDK信息: [Openjdk 17.0.6]
  • 版本信息:[Fastjson2-2.0.49]

重现步骤

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class MutatedIssue1874_271 {
    @Test
    public void mutatedTest() {
        List<TestJson> list = new ArrayList<>();
        list.add(new TestJson(true, Boolean.TRUE));
        list.add(new TestJson(false, Boolean.FALSE));
        String json = JSON.toJSONString(list, JSONWriter.Feature.WriteNonStringValueAsString);
        System.out.println(json);
        List<TestJson> list2 = JSON.parseArray(json, TestJson.class);
        assertEquals(list, list2);
        assertEquals("[{\"b\":\"true\",\"b2\":\"true\"},{\"b\":\"false\",\"b2\":\"false\"}]", json);
    }

    @Data
    @AllArgsConstructor
    public static class TestJson {
        private boolean b;
        private Boolean b2;
    }

    @Test
    public void mutatedTest1() {
        List<TestJson1> list = new ArrayList<>();
        list.add(new TestJson1(1, 1));
        list.add(new TestJson1(2, 2));
        String json = JSON.toJSONString(list, JSONWriter.Feature.WriteNonStringValueAsString);
        System.out.println(json);
        List<TestJson1> list2 = JSON.parseArray(json, TestJson1.class);
        assertEquals(list, list2);
        assertEquals("[{\"a\":\"1\",\"a2\":\"1\"},{\"a\":\"2\",\"a2\":\"2\"}]", json);
    }

    @Data
    @AllArgsConstructor
    public static class TestJson1 {
        private int a;
        private Integer a2;
    }
}

期待的正确结果

[{"b":"true","b2":"true"},{"b":"false","b2":"false"}]

相关日志输出

Expected :[{"b":"true","b2":"true"},{"b":"false","b2":"false"}]
Actual :[{"b":"true","b2":true},{"b":"false","b2":false}]

@Cooper-Zhong Cooper-Zhong added the bug Something isn't working label May 11, 2024
@wenshao wenshao added this to the 2.0.50 milestone May 11, 2024
@wenshao
Copy link
Member

wenshao commented May 11, 2024

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.50-SNAPSHOT/
问题已修复,请帮忙用2.0.50-SNAPSHOT版本验证下

@wenshao wenshao added the fixed label May 11, 2024
@Cooper-Zhong
Copy link
Contributor Author

验证已修复,辛苦wenshao

@wenshao
Copy link
Member

wenshao commented May 12, 2024

https://github.com/alibaba/fastjson2/releases/tag/2.0.50
2.0.50已发布,请用新版本

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

2 participants