Search in sources :

Example 1 with InstanceCreator

use of com.google.gson.InstanceCreator in project core by s4.

the class ObjectBuilder method main.

public static void main(String[] argv) throws Exception {
    ObjectBuilder b = new ObjectBuilder();
    String s = "{a:5, b:100}";
    Object out = b.fromJson(s, TEST.class);
    System.out.println(out.toString());
    TEST t = new TEST(1, 2);
    System.out.println(b.toJson(t));
    String[] query = { "name", "count", "freq" };
    String[] target = { "ACDW", "11" };
    io.s4.message.Request.ClientRInfo rinfo = new io.s4.message.Request.ClientRInfo();
    rinfo.setRequesterUUID(UUID.randomUUID());
    Request req = new io.s4.message.SinglePERequest(Arrays.asList(target), Arrays.asList(query), rinfo);
    System.out.println(req.toString());
    InstanceCreator<io.s4.message.Request.RInfo> infoCreator = new InstanceCreator<io.s4.message.Request.RInfo>() {

        public io.s4.message.Request.RInfo createInstance(Type type) {
            return new io.s4.message.Request.ClientRInfo();
        }
    };
    Gson gson = (new GsonBuilder()).registerTypeAdapter(io.s4.message.Request.RInfo.class, infoCreator).registerTypeAdapter(Object.class, new ObjectTypeAdapter()).create();
    System.out.println("gson: " + gson.toJson(req));
    System.out.println("gson reversed: " + gson.fromJson(gson.toJson(req), SinglePERequest.class));
    System.out.println(b.toJson(req));
    System.out.println(b.toJson(Arrays.asList(query)));
    System.out.println("----------------------------------------------");
    ArrayList<SSTest> list = new ArrayList<SSTest>();
    SSTest ss1 = new SSTest();
    ss1.str = "list-element-1";
    SSTest ss2 = new SSTest();
    ss2.str = "list-element-2";
    list.add(ss1);
    list.add(ss2);
    Map<String, Object> listmap = new HashMap<String, Object>();
    listmap.put("ll", list);
    MapTest mt = new MapTest();
    mt.map = listmap;
    Object listmapobj = listmap;
    System.out.println("list: " + gson.toJson(list));
    System.out.println("listmap: " + gson.toJson(listmap));
    System.out.println("listmapobj: " + gson.toJson(listmapobj));
    System.out.println("mapobject: " + gson.toJson(mt));
}
Also used : SinglePERequest(io.s4.message.SinglePERequest) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) GsonBuilder(com.google.gson.GsonBuilder) SinglePERequest(io.s4.message.SinglePERequest) Request(io.s4.message.Request) Type(java.lang.reflect.Type) InstanceCreator(com.google.gson.InstanceCreator) JsonObject(com.google.gson.JsonObject)

Aggregations

Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 InstanceCreator (com.google.gson.InstanceCreator)1 JsonObject (com.google.gson.JsonObject)1 Request (io.s4.message.Request)1 SinglePERequest (io.s4.message.SinglePERequest)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1