From fec40876549a8e6feea8c80bce2083327c2c8dee Mon Sep 17 00:00:00 2001 From: wenshao Date: Sun, 6 Aug 2017 18:09:28 +0800 Subject: [PATCH] backport bug fixed #1371 --- .../fastjson/serializer/MapSerializer.java | 5 +- .../json/bvt/issue_1300/Issue1371.java | 63 +++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 src/test/java/com/alibaba/json/bvt/issue_1300/Issue1371.java diff --git a/src/main/java/com/alibaba/fastjson/serializer/MapSerializer.java b/src/main/java/com/alibaba/fastjson/serializer/MapSerializer.java index 326b2b858d..5c1d475044 100755 --- a/src/main/java/com/alibaba/fastjson/serializer/MapSerializer.java +++ b/src/main/java/com/alibaba/fastjson/serializer/MapSerializer.java @@ -117,8 +117,9 @@ public void write(JSONSerializer serializer, Object object, Object fieldName, Ty out.write(','); } - if ((out.features & SerializerFeature.BrowserCompatible.mask) != 0 - || (out.features & SerializerFeature.WriteNonStringKeyAsString.mask) != 0) { + if (((out.features & SerializerFeature.BrowserCompatible.mask) != 0 + || (out.features & SerializerFeature.WriteNonStringKeyAsString.mask) != 0 + ) && !(entryKey instanceof Enum)) { String strEntryKey = JSON.toJSONString(entryKey); serializer.write(strEntryKey); } else { diff --git a/src/test/java/com/alibaba/json/bvt/issue_1300/Issue1371.java b/src/test/java/com/alibaba/json/bvt/issue_1300/Issue1371.java new file mode 100644 index 0000000000..9eb2f0f90d --- /dev/null +++ b/src/test/java/com/alibaba/json/bvt/issue_1300/Issue1371.java @@ -0,0 +1,63 @@ +package com.alibaba.json.bvt.issue_1300; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.serializer.SerializerFeature; +import junit.framework.TestCase; +import org.junit.Assert; + +import java.util.Map; +import java.util.TreeMap; + +/** + * Created by wenshao on 05/08/2017. + */ +public class Issue1371 extends TestCase { + private enum Rooms{ + A, B, C, D ,E ; + } + + public void testFastjsonEnum(){ + + Map enumMap = new TreeMap(); + + enumMap.put(Rooms.C, Rooms.D); + enumMap.put(Rooms.E, Rooms.A); + + Assert.assertEquals(JSON.toJSONString(enumMap, SerializerFeature.WriteNonStringKeyAsString), + "{\"C\":\"D\",\"E\":\"A\"}"); + + } + + + + +// public void testParsed(){ +// +// String oldStyleJson = "{1:'abc', 2:'cde'}"; +// +// Gson gson = new Gson(); +// +// Map fromJson = gson.fromJson(oldStyleJson, Map.class); +// +// Assert.assertNull(fromJson.get(1)); +// +// Assert.assertEquals(fromJson.get("1"), "abc" ); +// +// Map parsed = JSON.parseObject(oldStyleJson, Map.class, Feature.IgnoreAutoType, Feature.DisableFieldSmartMatch); +// +// +// Assert.assertNull(parsed.get(1)); +// +// Assert.assertEquals(parsed.get("1"), "abc" ); +// +// } +// +// public void testParsed_jackson() throws Exception { +// +// String oldStyleJson = "{1:\"abc\", 2:\"cde\"}"; +// +// ObjectMapper objectMapper = new ObjectMapper(); +// Map fromJson = objectMapper.readValue(oldStyleJson, Map.class); +// Assert.assertNull(fromJson.get(1)); +// } +}