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]JSONWriter.Feature 问题 #878

Closed
ffitio opened this issue Oct 28, 2022 · 1 comment
Closed

[BUG]JSONWriter.Feature 问题 #878

ffitio opened this issue Oct 28, 2022 · 1 comment
Labels
bug Something isn't working fixed
Milestone

Comments

@ffitio
Copy link

ffitio commented Oct 28, 2022

问题描述

简要描述您碰到的问题。
前提:fastjson2 的默认字符串输出特性是 字符串如果是 null 则不会输出该字段
问题:

  • 不设置任何 JSONWriter 特性时,表现正确
  • 设置了 JSONWriter.Feature.WriteNullListAsEmpty 特性时,貌似会覆盖缺省的字符串输出行为,可是明明设置的特性是空数组输出为 [],为什么把 字符串为 null 时不输出该字段 这个默认的特性给整没了 ?
  • 设置 JSONWriter.Feature.BrowserCompatible 特性时,表现又是正确的

环境信息

请填写以下信息:

  • OS信息: MacOS M1 Max / Monterey 12.
  • JDK信息: Openjdk 11
  • 版本信息:Fastjson2 2.0.16

重现步骤

如何操作可以重现该问题:

package com.example.demo;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;

import java.io.Serializable;

public class DemoApplication {

    public static void main(String[] args) {
        Response response = new Response();
        response.setSuccess(true);

        // {"success":true} 默认没有问题
        System.out.println(JSON.toJSONString(response));

        // {"errCode":null,"errMessage":null,"success":true} 这里有问题
        System.out.println(JSON.toJSONString(response, JSONWriter.Feature.WriteNullListAsEmpty));

        // {"success":true} 这个也没问题
        System.out.println(JSON.toJSONString(response, JSONWriter.Feature.BrowserCompatible));
    }

    public static class Response implements Serializable {
        private static final long serialVersionUID = 1L;

        /**
         * successful
         */
        private boolean success;

        /**
         * error code
         */
        private String errCode;

        /**
         * error message
         */
        private String errMessage;

        public boolean isSuccess() {
            return success;
        }

        public void setSuccess(boolean success) {
            this.success = success;
        }

        public String getErrCode() {
            return errCode;
        }

        public void setErrCode(String errCode) {
            this.errCode = errCode;
        }

        public String getErrMessage() {
            return errMessage;
        }

        public void setErrMessage(String errMessage) {
            this.errMessage = errMessage;
        }
    }

}

期待的正确结果

对您期望发生的结果进行清晰简洁的描述。
设置 JSONWriter.Feature.WriteNullListAsEmpty 不影响缺省的字符串输出行为

相关日志输出

请复制并粘贴任何相关的日志输出。

附加信息

如果你还有其他需要提供的信息,可以在这里填写(可以提供截图、视频等)。

@ffitio ffitio added the bug Something isn't working label Oct 28, 2022
@wenshao wenshao added this to the 2.0.17 milestone Oct 29, 2022
@wenshao wenshao added the fixed label Oct 29, 2022
@wenshao
Copy link
Member

wenshao commented Oct 30, 2022

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

@wenshao wenshao closed this as completed Oct 30, 2022
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