Skip to content

Commit

Permalink
bug fixed for dup type key
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Mar 15, 2016
1 parent 5be6c1a commit da6b9fc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -71,7 +72,7 @@ public void write(JSONSerializer serializer, Object object, Object fieldName, Ty

if (out.isEnabled(SerializerFeature.WriteClassName)) {
Class<?> mapClass = map.getClass();
boolean containsKey = (mapClass == JSONObject.class || mapClass == HashMap.class)
boolean containsKey = (mapClass == JSONObject.class || mapClass == HashMap.class || mapClass == LinkedHashMap.class)
&& map.containsKey(JSON.DEFAULT_TYPE_KEY);
if (!containsKey) {
out.writeFieldName(JSON.DEFAULT_TYPE_KEY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,23 @@ public void test_dupType2() throws Exception {
Assert.assertEquals("{\"@type\":\"java.util.LinkedHashMap\",\"1001\":{\"@type\":\"com.alibaba.json.bvt.writeClassName.WriteDuplicateType$DianDianCart\",\"id\":1001}}", text1);

}


public void test_dupType3() throws Exception {
DianDianCart cart = new DianDianCart();
cart.setId(1001);

LinkedHashMap<String, LinkedHashMap<String, Object>> cartMap = new LinkedHashMap<String, LinkedHashMap<String, Object>>();

LinkedHashMap<String, Object> obj = new LinkedHashMap<String, Object>();
obj.put(JSON.DEFAULT_TYPE_KEY, "com.alibaba.json.bvt.writeClassName.WriteDuplicateType$DianDianCart");
obj.put("id", 1001);
cartMap.put("1001", obj);

String text1 = JSON.toJSONString(cartMap, SerializerFeature.WriteClassName);
Assert.assertEquals("{\"@type\":\"java.util.LinkedHashMap\",\"1001\":{\"@type\":\"com.alibaba.json.bvt.writeClassName.WriteDuplicateType$DianDianCart\",\"id\":1001}}", text1);

}

public static class DianDianCart {

Expand Down

0 comments on commit da6b9fc

Please sign in to comment.