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.arvato.thoroughly.util.CommonUtils.java

License:Open Source License

/**
 * @param whetherExposeAnnotation/*from   ww  w . ja v  a2 s.c o  m*/
 * @param dateFormat
 * @return Gson instance
 */
public static Gson getGsonByBuilder(boolean whetherExposeAnnotation, String dateFormat) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    if (whetherExposeAnnotation) {
        //Export only entity with @Expose annotation properties
        gsonBuilder.excludeFieldsWithoutExposeAnnotation();
    }

    gsonBuilder.enableComplexMapKeySerialization();

    if (StringUtils.isEmpty(dateFormat)) {
        dateFormat = "yyyy-MM-dd HH:mm:ss";
    }
    gsonBuilder.setDateFormat(dateFormat);//.serializeNulls()
    gsonBuilder.setVersion(1.0);

    return gsonBuilder.create();
}

From source file:com.ashok.telugumovies.MainActivity.java

private void requestJsonObject() {
    RequestQueue queue = Volley.newRequestQueue(this);
    String url = "http://thecodeeasy.com/movie/channelpearjson.json";
    StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
        @Override//from w w  w  .  j  av  a  2s.  c o  m
        public void onResponse(String response) {
            Log.d(TAG, "Response " + response);
            GsonBuilder builder = new GsonBuilder();
            Gson mGson = builder.create();
            List<ItemObject> posts = new ArrayList<ItemObject>();
            posts = Arrays.asList(mGson.fromJson(response, ItemObject[].class));
            adapter = new RecyclerViewAdapter(MainActivity.this, posts);
            recyclerView.setAdapter(adapter);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d(TAG, "Error " + error.getMessage());
        }
    });
    queue.add(stringRequest);
}

From source file:com.asynhkm.productchecker.Checker.CheckerTask.java

private DataProductVersion return_result(String resultString) throws Exception {
    final GsonBuilder gsonb = new GsonBuilder();
    DataProductVersion mDataProductVersion = new DataProductVersion();
    ReturnResult rt;/* w w  w .  j  a  va  2  s . co m*/
    try {
        System.out.println("RESPONSE: " + resultString);
        if (isSuccess(resultString)) {
            final JSONObject Jr = new JSONObject(resultString);
            JSONObject data = Jr.getJSONObject("license_detail");
            mDataProductVersion = gsonb.create().fromJson(data.toString(), DataProductVersion.class);
        } else {
            ReturnResult result = gsonb.create().fromJson(resultString, ReturnResult.class);
            mDataProductVersion.setRR(result);
        }
    } catch (JsonParseException e) {
        rt = new ReturnResult(e.getMessage());
        mDataProductVersion.setRR(rt);
    } catch (JSONException e) {
        rt = new ReturnResult(e.getMessage());
        mDataProductVersion.setRR(rt);
    } catch (Exception e) {
        rt = new ReturnResult(e.getMessage());
        mDataProductVersion.setRR(rt);
    }
    return mDataProductVersion;
}

From source file:com.asynhkm.productchecker.Checker.CheckerTask.java

protected String consolidate() {
    final GsonBuilder gsonb = new GsonBuilder();
    String request_body = "";

    Gson gson = gsonb.create();
    switch (status_req) {
    case registration:
        requestRegister mrequestRegister = new requestRegister();
        mrequestRegister.domain = mac_id;
        mrequestRegister.product_key = productKey;
        request_body = gson.toJson(mrequestRegister);
        break;// w ww. ja  v  a2s. c  o  m
    case check:
        requestCheck mrequestCheck = new requestCheck();
        mrequestCheck.domain = mac_id;
        mrequestCheck.key = licenseKey;
        request_body = gson.toJson(mrequestCheck);
        break;
    }

    return request_body;
}

From source file:com.atlauncher.workers.InstanceInstaller.java

License:Open Source License

public InstanceInstaller(String instanceName, Pack pack, PackVersion version, boolean isReinstall,
        boolean isServer) {
    this.instanceName = instanceName;
    this.pack = pack;
    this.version = version;
    this.isReinstall = isReinstall;
    this.isServer = isServer;
    if (isServer) {
        serverLibraries = new ArrayList<File>();
    }//from   w  w w .j  a  v a  2s. c o m
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapterFactory(new EnumTypeAdapterFactory());
    builder.registerTypeAdapter(Date.class, new DateTypeAdapter());
    builder.registerTypeAdapter(File.class, new FileTypeAdapter());
    builder.setPrettyPrinting();
    this.gson = builder.create();
}

From source file:com.auction.request.handler.AuthHandler.java

@ClientRequest(action = ACTION.SAVE_ACCOUNT_SETTING_FA)
public ClientResponse saveAccountSettingFA(ISession session, IPacket packet) throws Exception {
    GeneralResponse response = new GeneralResponse();
    try {/*from  w  w w . jav  a 2s . c o  m*/
        Gson gson = new Gson();
        AccountSettingFA accountSettingFA = gson.fromJson(packet.getPacketBody(), AccountSettingFA.class);
        FeaturedAdManager featuredAdManager = new FeaturedAdManager();
        if (accountSettingFA.getId() > 0) {
            featuredAdManager.updateFeaturedAdAccountSetting(accountSettingFA);
        } else {
            int userId = (int) session.getUserId();
            if (userId > 0) {
                User user = new User();
                user.setId(userId);
                accountSettingFA.setUser(user);
                accountSettingFA = featuredAdManager.addFeaturedAdAccountSetting(accountSettingFA);
            } else {
                response.setSuccess(false);
                return response;
            }
        }
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY);
        gson = gsonBuilder.create();
        String accountSettingFAString = gson.toJson(accountSettingFA);
        AccountSettingFA response1 = new Gson().fromJson(accountSettingFAString, AccountSettingFA.class);
        response1.setSuccess(true);
        return response1;
        //response.setSuccess(true);
        //return response;
    } catch (Exception ex) {
        response.setSuccess(false);
        return response;
    }
}

From source file:com.auction.request.handler.AuthHandler.java

@ClientRequest(action = ACTION.ADD_MESSAGE_TEXT)
public ClientResponse addMessageText(ISession session, IPacket packet) throws Exception {
    GeneralResponse response = new GeneralResponse();
    Gson gson = new Gson();
    Message message = gson.fromJson(packet.getPacketBody(), Message.class);
    int userId = (int) session.getUserId();
    User user = new User();
    if (userId > 0) {
        if (message.getMessageTextList() != null && !message.getMessageTextList().isEmpty()) {
            user.setId(userId);//from  www .  jav  a 2  s. c o m
            message.getMessageTextList().get(0).setUser(user);
            MessageManager messageManager = new MessageManager();
            messageManager.addMessageText(message);

            GsonBuilder gsonBuilder = new GsonBuilder();
            gsonBuilder.registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY);
            gson = gsonBuilder.create();
            String messageInfoString = gson.toJson(messageManager.getMessageInfo(message.getId()));
            Message response1 = gson.fromJson(messageInfoString, Message.class);
            response1.setMessage("Message Text is added successfully");
            response1.setSuccess(true);
            return response1;
        } else {
            response.setMessage("Please add message text.");
            response.setSuccess(false);
            return response;
        }
    } else {
        response.setMessage("Invalid user. Please login.");
        response.setSuccess(false);
        return response;
    }
}

From source file:com.auction.request.handler.RequestHandler.java

@ClientRequest(action = ACTION.FETCH_MEMBER_ROLES)
public ClientResponse getMemberRoleList(ISession session, IPacket packet) {
    UserManager userManager = new UserManager();
    List<Role> roles = userManager.getMemberRoles();
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY);
    Gson gson = gsonBuilder.create();
    String rolesString = gson.toJson(roles);
    RoleList response = gson.fromJson("{\"roles\":" + rolesString + "}", RoleList.class);
    response.setSuccess(true);/* w w  w. j  a  va  2s.c  o  m*/
    return response;
}

From source file:com.auction.request.handler.RequestHandler.java

@ClientRequest(action = ACTION.FETCH_USER_LIST)
public ClientResponse getUserList(ISession session, IPacket packet) {
    UserManager userManager = new UserManager();
    List<User> users = userManager.getUsers(0, 10);
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY);
    Gson gson = gsonBuilder.create();
    String userString = gson.toJson(users);
    UserList response = gson.fromJson("{\"users\":" + userString + "}", UserList.class);
    response.setSuccess(true);/*from ww w.  j  a  v a 2 s.  c o m*/
    return response;
}

From source file:com.auction.request.handler.RequestHandler.java

@ClientRequest(action = ACTION.FETCH_PRODUCT_TYPE_LIST)
public ClientResponse getProductTypeList(ISession session, IPacket packet) {
    ProductManager productManager = new ProductManager();
    //ProductTypeList response = new Gson().fromJson("{\"productTypes\":[{\"id\":\"1\",\"title\":\"Property\"}, {\"id\":\"2\",\"title\":\"Room\"}]}", ProductTypeList.class );
    //ProductTypeList response = new ProductTypeList();
    //response.setProductTypes(productManager.getProductTypes());        
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY);
    Gson gson = gsonBuilder.create();
    String productString = gson.toJson(productManager.getProductTypes());
    ProductTypeList response = gson.fromJson("{\"productTypes\":" + productString + "}", ProductTypeList.class);

    response.setSuccess(true);//from w w  w  .ja v a2 s .  c  om
    return response;
}