Example usage for com.google.gson GsonBuilder create

List of usage examples for com.google.gson GsonBuilder create

Introduction

In this page you can find the example usage for com.google.gson GsonBuilder create.

Prototype

public Gson create() 

Source Link

Document

Creates a Gson instance based on the current configuration.

Usage

From source file:com.javacreed.examples.gson.part3.Example1.java

License:Apache License

public static void main(final String[] args) throws IOException {
    // Configure GSON
    final GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setPrettyPrinting();// www  . j ava2s  . c  o  m
    final Gson gson = gsonBuilder.create();

    final Author joshuaBloch = new Author();
    joshuaBloch.setId(1);
    joshuaBloch.setName("Joshua Bloch");

    final Author nealGafter = new Author();
    nealGafter.setId(2);
    nealGafter.setName("Neal Gafter");

    final Book javaPuzzlers = new Book();
    javaPuzzlers.setTitle("Java Puzzlers: Traps, Pitfalls, and Corner Cases");
    javaPuzzlers.setIsbn("032133678X");
    javaPuzzlers.setAuthors(new Author[] { joshuaBloch, nealGafter });

    final Book effectiveJava = new Book();
    effectiveJava.setTitle("Effective Java (2nd Edition)");
    effectiveJava.setIsbn("0321356683");
    effectiveJava.setAuthors(new Author[] { joshuaBloch });

    final Book[] books = new Book[] { javaPuzzlers, effectiveJava };

    final String json = gson.toJson(books);
    System.out.println(json);
}

From source file:com.javacreed.examples.gson.part3.Example2.java

License:Apache License

public static void main(final String[] args) throws IOException {
    // Configure GSON
    final GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Book.class, new BookSerialiser());
    gsonBuilder.setPrettyPrinting();/*ww  w  . jav a2 s .  co  m*/
    final Gson gson = gsonBuilder.create();

    final Author joshuaBloch = new Author();
    joshuaBloch.setId(1);
    joshuaBloch.setName("Joshua Bloch");

    final Author nealGafter = new Author();
    nealGafter.setId(2);
    nealGafter.setName("Neal Gafter");

    final Book javaPuzzlers = new Book();
    javaPuzzlers.setTitle("Java Puzzlers: Traps, Pitfalls, and Corner Cases");
    javaPuzzlers.setIsbn("032133678X");
    javaPuzzlers.setAuthors(new Author[] { joshuaBloch, nealGafter });

    final Book effectiveJava = new Book();
    effectiveJava.setTitle("Effective Java (2nd Edition)");
    effectiveJava.setIsbn("0321356683");
    effectiveJava.setAuthors(new Author[] { joshuaBloch });

    final Book[] books = new Book[] { javaPuzzlers, effectiveJava };

    final String json = gson.toJson(books);
    System.out.println(json);
}

From source file:com.javacreed.examples.gson.part3.Main.java

License:Apache License

public static void main(final String[] args) throws IOException {
    // Configure GSON
    final DataTypeAdapterFactory.Builder dtafBuilder = new DataTypeAdapterFactory.Builder();
    dtafBuilder.add(A.class, new ATypeAdapter());

    final GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapterFactory(dtafBuilder.build());
    gsonBuilder.setPrettyPrinting();// www  .j av a2  s . c om

    final Gson gson = gsonBuilder.create();

    final B b = new B();

    final String json = gson.toJson(b);
    System.out.println(json);
}

From source file:com.javacreed.examples.gson.part4.Main.java

License:Apache License

public static void main(final String[] args) throws IOException {

    final Class<?> type = new TypeToken<List<A>>() {
    }.getRawType();//from   w ww  . j a  v  a2s. c  o  m

    // Configure GSON
    final DataTypeAdapterFactory.Builder dtafBuilder = new DataTypeAdapterFactory.Builder();
    dtafBuilder.add(A.class, new ATypeAdapter());
    dtafBuilder.add(type, new ListATypeAdapter());

    final GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapterFactory(dtafBuilder.build());
    gsonBuilder.setPrettyPrinting();

    final Gson gson = gsonBuilder.create();

    final List<B> list = Arrays.asList(new B());

    final String json = gson.toJson(list, type);
    System.out.println(json);
}

From source file:com.jd.survey.util.HibernateProxySerializer.java

License:Open Source License

@Override
public JsonElement serialize(HibernateProxy proxyObj, Type arg1, JsonSerializationContext arg2) {
    try {/* w  w  w .jav a2s  .com*/
        GsonBuilder gsonBuilder = new GsonBuilder();
        //below ensures deep deproxied serialization
        gsonBuilder.registerTypeHierarchyAdapter(HibernateProxy.class, new HibernateProxySerializer());
        Object deProxied = proxyObj.getHibernateLazyInitializer().getImplementation();
        return gsonBuilder.create().toJsonTree(deProxied);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.jobchat.action.MobileAction.java

private void updateUser(JSONObject data) {
    GsonBuilder builder = new GsonBuilder();
    // ?????????????Expose
    // ???????????//
    // builder.excludeFieldsWithoutExposeAnnotation();
    Gson gson = builder.create();
    String s = data.toString();//from   ww w. java 2 s .c  o  m
    JUser u = gson.fromJson(s, new TypeToken<JUser>() {
    }.getType());
    JUser u2 = null;
    boolean b = false;
    if (u.getUserPassword() != null) {
        u2 = userService.findUserByPhone(u.getUserPhone());
        Md5 md5 = new Md5(u.getUserPassword());
        md5.processString();
        String strEncrypto = md5.getStringDigest();
        u2.setUserPassword(strEncrypto);
        b = userService.updateUser(u2);
    } else {
        u2 = userService.getUserById(u.getUserId());
        u.setUserPassword(u2.getUserPassword());
        b = userService.updateUser(u);
    }
    Map<String, Object> map = new HashMap<String, Object>();
    int status = 1;
    if (b) {
        status = 0;
    } else {
        status = 1;
    }
    map.put("ret", status);
    map.put("msg", "");
    Map<String, Object> map2 = new HashMap<String, Object>();
    map.put("data", map2);
    try {
        ResultUtils.toJson(ServletActionContext.getResponse(), map);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.jobchat.action.MobileAction.java

private void addUser(JSONObject data) {
    GsonBuilder builder = new GsonBuilder();
    Gson gson = builder.create();
    String s = data.toString();/*w  w w .j a  v a 2 s  .  c o m*/
    JUser u = gson.fromJson(s, new TypeToken<JUser>() {
    }.getType());

    String ptime = TextUtility.formatDate(new Date());
    Md5 md5 = new Md5(u.getUserPassword());
    md5.processString();
    String strEncrypto = md5.getStringDigest();
    u.setUserPassword(strEncrypto);
    u.setUserTime(ptime);
    u.setUserStatus(0);
    int outid = 0;
    JUser m = null;
    do {
        outid = (int) (Math.random() * 10000000);
        m = userService.findUserByOutid(outid);
    } while (m == null);
    u.setUserOutid(outid);
    if (u.getUserNikename() == null) {
        u.setUserNikename(u.getUserPhone());
    }
    u.setUserType((short) 0);
    boolean b = userService.addUser(u);
    JUserinfo uinfo = new JUserinfo();
    uinfo.setJUser(u);
    b = userinfoService.addUserinfo(uinfo);
    Map<String, Object> map = new HashMap<String, Object>();
    int status = 1;
    if (b) {
        status = 0;
        map.put("msg", "");
        Map<String, Object> map2 = new HashMap<String, Object>();
        map.put("data", map2);
    } else {
        status = 1;
        map.put("msg", "");
    }
    map.put("ret", status);
    try {
        ResultUtils.toJson(ServletActionContext.getResponse(), map);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.jobchat.action.MobileAction.java

private void addMessages(JSONObject data) throws JSONException {
    //SToken token = tokenService.getToken(data.getString("acesstoken").toString());
    GsonBuilder builder = new GsonBuilder();
    Gson gson = builder.create();
    String s = data.toString();/*from w w  w.  j  a  v  a 2 s .c  om*/
    JMessages u = gson.fromJson(s, new TypeToken<JMessages>() {
    }.getType());

    //u.setSUser(token.getSUser());
    MmochatFilterService fiter = new MmochatFilterService();
    fiter.init();
    @SuppressWarnings("static-access")
    String content = fiter.getFilterString(u.getMessagesInfo());
    u.setMessagesInfo(content);
    String ptime = TextUtility.formatDate(new Date());
    u.setMessagesTime(ptime);
    boolean b = messagesService.addJMessages(u);
    Map<String, Object> map = new HashMap<String, Object>();
    int status = 1;
    if (b) {
        status = 0;
        map.put("msg", "");
    } else {
        status = 1;
        map.put("msg", "");
    }
    map.put("ret", status);
    try {
        ResultUtils.toJson(ServletActionContext.getResponse(), map);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.jobchat.action.MobileAction.java

private void updateMessages(JSONObject data) throws JSONException {
    int mid = -1;
    JMessages m = null;// ww  w  .  j a v a 2s .com
    if (data.has("mid")) {
        mid = data.getInt("mid");
        m = messagesService.getJMessagesById(mid);
        m.setMessagesStatus(1);
    } else {
        GsonBuilder builder = new GsonBuilder();
        Gson gson = builder.create();
        String s = data.toString();
        m = gson.fromJson(s, new TypeToken<JMessages>() {
        }.getType());
        if (m.getMessagesId() > 0) {
            int agreenum = m.getMessagesAgreenum();
            m = messagesService.getJMessagesById(m.getMessagesId());
            m.setMessagesAgreenum(agreenum);
        }
    }
    boolean b = messagesService.updateJMessages(m);

    Map<String, Object> map = new HashMap<String, Object>();
    int status = 1;
    if (b) {
        status = 0;
    } else {
        status = 1;
    }
    map.put("ret", status);
    map.put("msg", "");
    try {
        ResultUtils.toJson(ServletActionContext.getResponse(), map);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.jobchat.action.MobileAction.java

private void addComments(JSONObject data) throws JSONException {
    GsonBuilder builder = new GsonBuilder();
    Gson gson = builder.create();
    String s = data.toString();/*from   w  ww.  j av a2  s.  c o  m*/
    JComments u = gson.fromJson(s, new TypeToken<JComments>() {
    }.getType());
    MmochatFilterService fiter = new MmochatFilterService();
    fiter.init();
    @SuppressWarnings("static-access")
    String com = fiter.getFilterString(u.getCommentsInfo());
    String ptime = TextUtility.formatDate(new Date());
    u.setCommentsInfo(com);
    u.setCommentsTime(ptime);
    u.setCommentsInfostatus(0);
    boolean b = commentsService.addSComments(u);
    Map<String, Object> map = new HashMap<String, Object>();
    int status = 1;
    if (b) {
        status = 0;
        map.put("msg", "");
    } else {
        status = 1;
        map.put("msg", "");
    }
    map.put("ret", status);
    try {
        ResultUtils.toJson(ServletActionContext.getResponse(), map);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}