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

Commit

Permalink
bug fixed for issue #1474
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Oct 1, 2017
1 parent 6264ced commit d35a1a1
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,29 @@ protected void write(JSONSerializer serializer, //
}
}

commaFlag = true;
boolean fieldUnwrappedNull = false;
if (fieldInfo.unwrapped
&& propertyValue instanceof Map) {
Map map = ((Map) propertyValue);
if (map.size() == 0) {
fieldUnwrappedNull = true;
} else if (!serializer.isEnabled(SerializerFeature.WriteMapNullValue)){
boolean hasNotNull = false;
for (Object value : map.values()) {
if (value != null) {
hasNotNull = true;
break;
}
}
if (!hasNotNull) {
fieldUnwrappedNull = true;
}
}
}

if (!fieldUnwrappedNull) {
commaFlag = true;
}
}

this.writeAfter(serializer, object, commaFlag ? ',' : '\0');
Expand Down
51 changes: 51 additions & 0 deletions src/test/java/com/alibaba/json/bvt/issue_1400/Issue1474.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.alibaba.json.bvt.issue_1400;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.annotation.JSONType;
import junit.framework.TestCase;

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

public class Issue1474 extends TestCase {
public void test_for_issue() throws Exception {
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);

assertEquals("{\"id\":\"001\",\"name\":\"顾客\"}", JSON.toJSONString(p));
}

@JSONType(asm = false)
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;
}
}
}

0 comments on commit d35a1a1

Please sign in to comment.