Permalink
Browse files

Fixed JsonTypeDefinition to sort keyNames in Json MAP type.

  • Loading branch information...
1 parent 43f74db commit 26a66ee76a6b051d292316b6963de8196b7d319b @bbansal bbansal committed Jun 23, 2009
@@ -50,8 +50,8 @@
private Object type;
public JsonTypeDefinition(Object type) {
- this.type = type;
- validate(type);
+ this.type = validate(type);
+
}
public static JsonTypeDefinition fromJson(String typeSig) {
@@ -165,20 +165,26 @@ public void validate() {
validate(getType());
}
- private void validate(Object type) {
+ private Object validate(Object type) {
if(type == null) {
throw new IllegalArgumentException("Type or subtype cannot be null.");
} else if(type instanceof List<?>) {
List<?> l = (List<?>) type;
if(l.size() != 1)
throw new IllegalArgumentException("Lists in type definition must have length exactly one.");
- validate(l.get(0));
+ return Arrays.asList(validate(l.get(0)));
} else if(type instanceof Map<?, ?>) {
- Map<?, ?> m = (Map<?, ?>) type;
- for(Map.Entry<?, ?> entry: m.entrySet())
- validate(entry.getValue());
+ Map<String, ?> m = (Map<String, ?>) type;
+ // bbansal: sort keys here for consistent with fromJson()
+ Map<String, Object> newM = new LinkedHashMap<String, Object>(m.size());
+ List<String> keys = new ArrayList<String>((m.keySet()));
+ Collections.sort(keys);
+ for(String key: keys)
+ newM.put(key, validate(m.get(key)));
+ return newM;
} else if(type instanceof JsonTypes) {
// this is good
+ return type;
} else {
throw new IllegalArgumentException("Unknown type in json type definition: " + type
+ " of class " + type.getClass().getName());
@@ -26,6 +26,7 @@
import junit.framework.TestCase;
import voldemort.serialization.SerializationException;
+import voldemort.serialization.Serializer;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
@@ -91,6 +92,52 @@ public void testInvalidTypeDefs() {
assertInvalidTypeDef(quote("abc"));
}
+ public void testMapTypeKeyOrder() {
+ String typeSig = "{\"name\":\"string\", \"color\":\"string\", \"age\":\"int32\"}";
+ Object data = ImmutableMap.of("name", "xxx", "color", "yyy", "age", 28);
+
+ Serializer outOfOrderSerializer = new JsonTypeSerializer(new JsonTypeDefinition(ImmutableMap.of("name",
+ JsonTypes.STRING,
+ "color",
+ JsonTypes.STRING,
+ "age",
+ JsonTypes.INT32)));
+ assertEquals("Key order in Map should not matter while serializing/deserializing",
+ data,
+ outOfOrderSerializer.toObject(getSerializer(typeSig).toBytes(data)));
+
+ outOfOrderSerializer = new JsonTypeSerializer(new JsonTypeDefinition(ImmutableMap.of("color",
+ JsonTypes.STRING,
+ "age",
+ JsonTypes.INT32,
+ "name",
+ JsonTypes.STRING)));
+ assertEquals("Key order in Map should not matter while serializing/deserializing",
+ data,
+ outOfOrderSerializer.toObject(getSerializer(typeSig).toBytes(data)));
+
+ outOfOrderSerializer = new JsonTypeSerializer(new JsonTypeDefinition(ImmutableMap.of("age",
+ JsonTypes.INT32,
+ "name",
+ JsonTypes.STRING,
+ "color",
+ JsonTypes.STRING)));
+ assertEquals("Key order in Map should not matter while serializing/deserializing",
+ data,
+ outOfOrderSerializer.toObject(getSerializer(typeSig).toBytes(data)));
+
+ outOfOrderSerializer = new JsonTypeSerializer(new JsonTypeDefinition(ImmutableMap.of("color",
+ JsonTypes.STRING,
+ "name",
+ JsonTypes.STRING,
+ "age",
+ JsonTypes.INT32)));
+ assertEquals("Key order in Map should not matter while serializing/deserializing",
+ data,
+ outOfOrderSerializer.toObject(getSerializer(typeSig).toBytes(data)));
+
+ }
+
public void testToObjectIsInverseOfToBytes() {
/* test primitive types */
assertInverse(quote("int8"), (byte) 127);

0 comments on commit 26a66ee

Please sign in to comment.