Skip to content

Commit

Permalink
refactor SerializeConfig & add improved EnumTest
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Apr 19, 2016
1 parent 56bb2bb commit 451843b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
22 changes: 16 additions & 6 deletions src/main/java/com/alibaba/fastjson/serializer/SerializeConfig.java
Expand Up @@ -70,7 +70,7 @@
* *
* @author wenshao[szujobs@hotmail.com] * @author wenshao[szujobs@hotmail.com]
*/ */
public class SerializeConfig extends IdentityHashMap<Type, ObjectSerializer> { public class SerializeConfig {
public final static SerializeConfig globalInstance = new SerializeConfig(); public final static SerializeConfig globalInstance = new SerializeConfig();


private static boolean awtError = false; private static boolean awtError = false;
Expand All @@ -84,6 +84,8 @@ public class SerializeConfig extends IdentityHashMap<Type, ObjectSerializer> {


private String typeKey = JSON.DEFAULT_TYPE_KEY; private String typeKey = JSON.DEFAULT_TYPE_KEY;


private final IdentityHashMap<Type, ObjectSerializer> serializers;

public String getTypeKey() { public String getTypeKey() {
return typeKey; return typeKey;
} }
Expand Down Expand Up @@ -167,7 +169,7 @@ public SerializeConfig() {
} }


public SerializeConfig(int tableSize) { public SerializeConfig(int tableSize) {
super(tableSize); serializers = new IdentityHashMap<Type, ObjectSerializer>(1024);


try { try {
if (asm) { if (asm) {
Expand Down Expand Up @@ -292,7 +294,7 @@ public void addFilter(Class<?> clazz, SerializeFilter filter) {
} }


public ObjectSerializer getObjectWriter(Class<?> clazz) { public ObjectSerializer getObjectWriter(Class<?> clazz) {
ObjectSerializer writer = get(clazz); ObjectSerializer writer = serializers.get(clazz);


if (writer == null) { if (writer == null) {
try { try {
Expand All @@ -311,7 +313,7 @@ public ObjectSerializer getObjectWriter(Class<?> clazz) {
// skip // skip
} }


writer = get(clazz); writer = serializers.get(clazz);
} }


if (writer == null) { if (writer == null) {
Expand All @@ -333,7 +335,7 @@ public ObjectSerializer getObjectWriter(Class<?> clazz) {
// skip // skip
} }


writer = get(clazz); writer = serializers.get(clazz);
} }
} }


Expand Down Expand Up @@ -405,8 +407,16 @@ public ObjectSerializer getObjectWriter(Class<?> clazz) {
put(clazz, createJavaBeanSerializer(clazz)); put(clazz, createJavaBeanSerializer(clazz));
} }


writer = get(clazz); writer = serializers.get(clazz);
} }
return writer; return writer;
} }

public final ObjectSerializer get(Type key) {
return this.serializers.get(key);
}

public boolean put(Type key, ObjectSerializer value) {
return this.serializers.put(key, value);
}
} }
Expand Up @@ -21,6 +21,7 @@ public void test_enum() throws Exception {
Assert.assertEquals("\"Big\"", JSON.toJSONString(Type.Big, SerializerFeature.WriteEnumUsingToString)); // "Big" Assert.assertEquals("\"Big\"", JSON.toJSONString(Type.Big, SerializerFeature.WriteEnumUsingToString)); // "Big"
Assert.assertEquals("\"Medium\"", JSON.toJSONString(Type.Medium, SerializerFeature.WriteEnumUsingToString)); // "Medium" Assert.assertEquals("\"Medium\"", JSON.toJSONString(Type.Medium, SerializerFeature.WriteEnumUsingToString)); // "Medium"
Assert.assertEquals("\"Small\"", JSON.toJSONString(Type.Small, SerializerFeature.WriteEnumUsingToString)); // "Small" Assert.assertEquals("\"Small\"", JSON.toJSONString(Type.Small, SerializerFeature.WriteEnumUsingToString)); // "Small"
Assert.assertEquals("'Small'", JSON.toJSONString(Type.Small, SerializerFeature.UseSingleQuotes)); // "Small"
} }


} }
Expand Up @@ -28,7 +28,11 @@ public void test_0() throws Exception {
Assert.assertFalse(0 == size(map)); Assert.assertFalse(0 == size(map));
} }


public static int size(IdentityHashMap map) throws Exception { public static int size(SerializeConfig config) throws Exception {
Field serializersField = SerializeConfig.class.getDeclaredField("serializers");
serializersField.setAccessible(true);
Object map = serializersField.get(config);

Field bucketsField = IdentityHashMap.class.getDeclaredField("buckets"); Field bucketsField = IdentityHashMap.class.getDeclaredField("buckets");
bucketsField.setAccessible(true); bucketsField.setAccessible(true);
Object[] buckets = (Object[]) bucketsField.get(map); Object[] buckets = (Object[]) bucketsField.get(map);
Expand Down

0 comments on commit 451843b

Please sign in to comment.