-
Notifications
You must be signed in to change notification settings - Fork 111
JacksonSampleSimplePojoMapper
Paul Brown edited this page Oct 19, 2011
·
1 revision
- page was renamed from SimplePojoMapper <<TableOfContents(4)>>
If you just need to map POJOs (Plain Old Java Objects) back and forth from JSON -- maybe because you want to pass data between Java and some other programming language, or maybe because you want to persist POJOs in a JSON format -- just below is a simple PojoMapper class that does this.
// Create a POJO MyPojo pojo = new MyPojo(); // Populate the POJO // Convert the POJO to a JSON string String pojoAsString = PojoMapper.toJson(pojo, true); // Convert the JSON string back into a POJO pojo = (MyPojo) PojoMapper.fromJson(pojoAsString, MyPojo.class); // Convert the POJO to a JSON string that gets stored in a file FileWriter fw = new FileWriter("pojo.txt"); PojoMapper.toJson(pojo3, fw, true); // Convert the JSON string in the file back into a POJO FileReader fr = new FileReader("pojo.txt"); pojo = (MyPojo) PojoMapper.fromJson(fr, MyPojo.class);
import org.codehaus.jackson.JsonFactory; import org.codehaus.jackson.JsonGenerationException; import org.codehaus.jackson.JsonGenerator; import org.codehaus.jackson.JsonParseException; import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper; public class PojoMapper { private static ObjectMapper m = new ObjectMapper(); private static JsonFactory jf = new JsonFactory(); public static <T> Object fromJson(String jsonAsString, Class<T> pojoClass) throws JsonMappingException, JsonParseException, IOException { return m.readValue(jsonAsString, pojoClass); } public static <T> Object fromJson(FileReader fr, Class<T> pojoClass) throws JsonParseException, IOException { return m.readValue(fr, pojoClass); } public static String toJson(Object pojo, boolean prettyPrint) throws JsonMappingException, JsonGenerationException, IOException { StringWriter sw = new StringWriter(); JsonGenerator jg = jf.createJsonGenerator(sw); if (prettyPrint) { jg.useDefaultPrettyPrinter(); } m.writeValue(jg, pojo); return sw.toString(); } public static void toJson(Object pojo, FileWriter fw, boolean prettyPrint) throws JsonMappingException, JsonGenerationException, IOException { JsonGenerator jg = jf.createJsonGenerator(fw); if (prettyPrint) { jg.useDefaultPrettyPrinter(); } m.writeValue(jg, pojo); } }
import java.io.FileReader; import java.io.FileWriter; import java.util.Date; import java.util.HashMap; import java.util.Map; public class MyPojo { private boolean on; private String name; private Map<String,Date> map; public void setOn(boolean on) { this.on = on; } public boolean isOn() { return this.on; } public void setName(String name) { this.name = name; } public String getName() { return this.name; } public void setMap(Map<String,Date> map) { this.map = map; } public Map<String,Date> getMap() { return this.map; } public String toString() { StringBuilder sb = new StringBuilder(); sb.append("on=" + on); sb.append(" name=" + name); sb.append(" map={"); for (Map.Entry<String,Date> entry : this.map.entrySet()) { sb.append("" + entry.getKey() + "=" + entry.getValue()); } sb.append("}"); return sb.toString(); } public static void main(String[] args) { // Create a POJO MyPojo pojo = new MyPojo(); // Populate it with some data pojo.setName("Lucky"); pojo.setOn(true); Map<String,Date> map = new HashMap<String,Date>(); map.put("now", new Date()); pojo.setMap(map); // Map it to JSON and then back again try { String pojoAsString = PojoMapper.toJson(pojo, true); System.out.println("POJO as string:\n" + pojoAsString + "\n"); MyPojo pojo2 = (MyPojo) PojoMapper.fromJson(pojoAsString, MyPojo.class); System.out.println("POJO toString():\n" + pojo2 + "\n"); } catch (Exception e) { e.printStackTrace(); } // Create another POJO MyPojo pojo3 = new MyPojo(); pojo3.setName("Baldwin"); pojo3.setOn(false); Map<String,Date> map2 = new HashMap<String,Date>(); map2.put("now", new Date()); pojo3.setMap(map2); // Map it to JSON, store it on disk and then read it back try { FileWriter fw = new FileWriter("pojo.txt"); PojoMapper.toJson(pojo3, fw, true); FileReader fr = new FileReader("pojo.txt"); MyPojo pojo4 = (MyPojo) PojoMapper.fromJson(fr, MyPojo.class); System.out.println("POJO read from file:\n" + pojo4); } catch (Exception e) { e.printStackTrace(); } } }
POJO as string: { "name" : "Lucky", "on" : true, "map" : { "now" : 1270349496303 } } POJO toString(): on=true name=Lucky map={now=Sat Apr 03 19:51:36 PDT 2010} POJO read from file: on=false name=Baldwin map={now=Sat Apr 03 19:51:36 PDT 2010}
Back to JacksonFeatures11
CategoryJackson