Skip to content
This repository was archived by the owner on Oct 23, 2024. It is now read-only.
This repository was archived by the owner on Oct 23, 2024. It is now read-only.

JSONField 注解的 unwrap 特性有 bug #1474

@xunux

Description

@xunux

当 JSONField 注解的参数 unwrapped 设为 true 时,若 该对象的字段值全为 null,序列化之后
会多一个逗号, fastjson版本为最新版 1.2.38

java 代码

import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;

public class FastjsonBugTest {
	public static void main(String[] args) {
		Map<String,Object> extraData = new HashMap<String,Object>();
		extraData.put("ext_1", null);
		extraData.put("ext_2", null);
		
		People p = new People();
		p.setId("001");
		p.setName("顾客");
		p.setExtraData(extraData);
		
		System.out.println(JSON.toJSONString(p));
	}
	
	static class People{
		private String name;
		private String id;
		@JSONField(unwrapped=true)
		private Object extraData;

		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public String getId() {
			return id;
		}
		public void setId(String id) {
			this.id = id;
		}
		public Object getExtraData() {
			return extraData;
		}
		public void setExtraData(Object extraData) {
			this.extraData = extraData;
		}
	}
}

预期结果:

{"id":"001","name":"顾客"}

实际结果:

{,"id":"001","name":"顾客"}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions