GsonDemo.java

Download GsonDemo.java

 1: // In this demo, we use Google's FOSS JSON library "Gson" to
 2: // easily convert a complex Map to JSON and back again.
 3: //
 4: // This code originally downloaded from
 5: //  <https://github.com/google/gson/blob/master/extras/src/main/
 6: //     java/com/google/gson/extras/examples/rawcollections/
 7: //     RawCollectionsExample.java>
 8: //
 9: // Modified 3/2017 by Wayne Pollock, Tampa Florida USA
10: 
11: // Download gson.jar (and optionally the API docs, also in a jar) from
12: //  <https://search.maven.org/#search%7Cga%7C1%7Cg%3A%22com.google.code.gson%22>
13: // Make sure that jar file is on your CLASSPATH.
14: 
15: import java.lang.reflect.Type;
16: import java.util.*;
17: import com.google.gson.*;
18: import com.google.gson.reflect.TypeToken;
19: 
20: public class GsonDemo {
21:   static class Event {  // Trivial class to show off Gson.
22:     private int id;
23:     private String detail;
24:     private Event(int id, String detail) {
25:       this.id = id;
26:       this.detail = detail;
27:     }
28:     @Override
29:     public String toString() {
30:       return String.format( "(id=%d, detail=%s)", id, detail );
31:     }
32:   }
33: 
34:   public static void main(String[] args) {
35:     // Create a gson object:
36:     //Gson gson = new Gson();  // Creates "compact" JSON text, no nulls.
37:     Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls().create();
38: 
39:     Map<String, Set<Event>> map = new HashMap<>();
40: 
41:     Set<Event> set = new HashSet<>();
42:     set.add( new Event(1, "Birthday") );
43:     set.add( new Event(2, "Work Anniversary") );
44:     map.put( "Events1", set );
45: 
46:     map.put( "Events2", null );  // Just because we can!
47: 
48:     set = new HashSet<>();  // Make another set
49:     set.add( new Event(3, "Vacation") );
50:     set.add( new Event(4, "Holiday") );
51:     set.add( new Event(5, "In-service Day") );
52:     map.put( "Events3", set );
53: 
54:     // Convert Java collection to JSON:
55:     String json = gson.toJson( map );
56:     System.out.println( "\nUsing Gson.toJson():\n" + json );
57: 
58:     // Convert JSON to Java collection:
59: 
60:     // First, create a Java Type object to represent the class of the object,
61:     // in this case a Map<String, Set<Event>>.  This is required since JSON
62:     // doesn't include any class information; there's no automatic way
63:     // to convert JSON back to complex objects without telling the library
64:     // the exact type to use.  (Going from objects to JSON is no problem.)
65:     // This is done by creating an anonymous inner class that extends
66:     // Gson's "TypeToken" class, then using the getType method:
67:     Type stringToSetOfEventMap =
68:         new TypeToken<Map<String, Set<Event>>>(){}.getType();
69: 
70:     Map<String,Set<Event>> map2 = gson.fromJson(json, stringToSetOfEventMap);
71:     System.out.println( "\n\nUsing Gson.fromJson():\n" + map2.toString() );
72:   }
73: }