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

Commit

Permalink
backport bug fixed #1371
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Aug 6, 2017
1 parent 90e821b commit fec4087
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
63 changes: 63 additions & 0 deletions src/test/java/com/alibaba/json/bvt/issue_1300/Issue1371.java
Original file line number Diff line number Diff line change
@@ -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<Rooms, Rooms> enumMap = new TreeMap<Rooms, Rooms>();

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));
// }
}

0 comments on commit fec4087

Please sign in to comment.