Skip to content

Instantly share code, notes, and snippets.

@kimchy
Created March 3, 2011 18:32
Show Gist options
  • Save kimchy/853232 to your computer and use it in GitHub Desktop.
Save kimchy/853232 to your computer and use it in GitHub Desktop.
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.smile.SmileFactory;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class Test {
public static void main(String[] args) throws Exception {
SmileFactory factory = new SmileFactory();
HashMap props1 = buildMap("", 65);
HashMap props2 = buildMap("", 1);
writeMapAndParse(factory, props1);
writeMapAndParse(factory, props2);
writeMapAndParse(factory, props1);
writeMapAndParse(factory, props2);
}
private static void writeMapAndParse(SmileFactory factory, Map map) throws Exception {
ByteArrayOutputStream os = new ByteArrayOutputStream();
// generate
JsonGenerator generator = factory.createJsonGenerator(os);
writeMap(generator, map);
generator.close();
// parse
JsonParser parser = factory.createJsonParser(os.toByteArray());
while (parser.nextToken() != null) {
}
}
private static HashMap buildMap(String prefix, int size) {
HashMap props = new HashMap<String, String>();
for (int it = 0; it < size; it++) {
String key = prefix + "prop_" + it;
props.put(key, "a");
}
return props;
}
// A sample utility to write a map
public static void writeMap(JsonGenerator gen, Map<String, Object> map) throws IOException {
gen.writeStartObject();
for (Map.Entry<String, Object> entry : map.entrySet()) {
gen.writeFieldName(entry.getKey());
Object value = entry.getValue();
if (value == null) {
gen.writeNull();
} else {
writeValue(gen, value);
}
}
gen.writeEndObject();
}
private static void writeIterable(JsonGenerator gen, Iterable iterable) throws IOException {
gen.writeStartArray();
for (Object value : iterable) {
writeValue(gen, value);
}
gen.writeEndArray();
}
private static void writeObjectArray(JsonGenerator gen, Object[] array) throws IOException {
gen.writeStartArray();
for (Object value : array) {
writeValue(gen, value);
}
gen.writeEndArray();
}
private static void writeValue(JsonGenerator gen, Object value) throws IOException {
Class type = value.getClass();
if (type == String.class) {
gen.writeString((String) value);
} else if (type == Integer.class) {
gen.writeNumber(((Integer) value).intValue());
} else if (type == Long.class) {
gen.writeNumber(((Long) value).longValue());
} else if (type == Float.class) {
gen.writeNumber(((Float) value).floatValue());
} else if (type == Double.class) {
gen.writeNumber(((Double) value).doubleValue());
} else if (type == Short.class) {
gen.writeNumber(((Short) value).shortValue());
} else if (type == Boolean.class) {
gen.writeBoolean(((Boolean) value).booleanValue());
} else if (value instanceof Map) {
writeMap(gen, (Map) value);
} else if (value instanceof Iterable) {
writeIterable(gen, (Iterable) value);
} else if (value instanceof Object[]) {
writeObjectArray(gen, (Object[]) value);
} else if (type == byte[].class) {
gen.writeBinary((byte[]) value);
} else {
gen.writeString(value.toString());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment