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.auction.request.handler.RequestHandler.java

@ClientRequest(action = ACTION.FETCH_CLOSING_PRODUCT_LIST)
public ClientResponse getClosingProductList(ISession session, IPacket packet) {
    ProductManager productManager = new ProductManager();
    List<Product> products = productManager.getClosingProducts(6);
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY);
    Gson gson = gsonBuilder.create();
    String productString = gson.toJson(products);
    ProductList response = gson.fromJson("{\"products\":" + productString + "}", ProductList.class);
    response.setSuccess(true);//from w w  w .j a va 2 s.c  o m
    return response;
}

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

@ClientRequest(action = ACTION.FETCH_SAVED_PRODUCT_LIST)
public ClientResponse getSavedProductList(ISession session, IPacket packet) {
    int userId = (int) session.getUserId();
    ProductManager productManager = new ProductManager();
    List<Product> products = productManager.getSavedProducts(userId, 0, 100);
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY);
    Gson gson = gsonBuilder.create();
    String productString = gson.toJson(products);
    ProductList response = gson.fromJson("{\"products\":" + productString + "}", ProductList.class);
    response.setSuccess(true);//from  www  . jav  a  2s  . c om
    return response;
}

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

@ClientRequest(action = ACTION.FETCH_PRODUCT_LIST)
public ClientResponse getProductList(ISession session, IPacket packet) {
    SearchParams searchParams = null;//from  ww  w . j a v  a 2 s  . c o  m
    try {
        Gson gson1 = new Gson();
        searchParams = gson1.fromJson(packet.getPacketBody(), SearchParams.class);
    } catch (Exception ex) {
        logger.error(ex.toString());
    }

    ProductManager pm = new ProductManager();
    List<Product> products = pm.getProducts(searchParams, 0, 12);
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY);
    Gson gson = gsonBuilder.create();
    String productString = gson.toJson(products);
    System.out.println(productString);
    ProductList response = gson.fromJson("{\"products\":" + productString + "}", ProductList.class);
    //        ProductList response = new Gson().fromJson("{\"products\":[{\"id\":\"1\",\"title\":\"Fun at the Bowling Alley1\", \"img\":\"a.jpg\", \"price\":\"100\", \"price_type\":\"pw\", \"size\":\"single\", \"images\":[{\"id\":\"1\", \"url\":\"a.jpg\"}, {\"id\":\"2\", \"url\":\"b.jpg\"}], \"available\":\"2017-04-18\", \"description\":\"Double room in E16 available from 17/04/2017, short walk away from Prince Regent Lane DLR1.\"}, {\"id\":\"2\",\"title\":\"Fun at the Bowling Alley2\", \"img\":\"a.jpg\", \"price\":\"200\", \"price_type\":\"pw\", \"size\":\"single\", \"images\":[{\"id\":\"1\", \"url\":\"a.jpg\"}, {\"id\":\"2\", \"url\":\"b.jpg\"}], \"available\":\"2017-04-18\", \"description\":\"Double room in E16 available from 17/04/2017, short walk away from Prince Regent Lane DLR2.\"}, {\"id\":\"3\",\"title\":\"Fun at the Bowling Alley3\", \"img\":\"a.jpg\", \"price\":\"300\", \"price_type\":\"pw\", \"size\":\"single\", \"images\":[{\"id\":\"1\", \"url\":\"a.jpg\"}, {\"id\":\"2\", \"url\":\"b.jpg\"}], \"available\":\"2017-04-18\", \"description\":\"Double room in E16 available from 17/04/2017, short walk away from Prince Regent Lane DLR3.\"}, {\"id\":\"4\",\"title\":\"Fun at the Bowling Alley4\", \"img\":\"a.jpg\", \"price\":\"400\", \"price_type\":\"pw\", \"size\":\"single\", \"images\":[{\"id\":\"1\", \"url\":\"a.jpg\"}, {\"id\":\"2\", \"url\":\"b.jpg\"}], \"available\":\"2017-04-18\", \"description\":\"Double room in E16 available from 17/04/2017, short walk away from Prince Regent Lane DLR4.\"}, {\"id\":\"5\",\"title\":\"Fun at the Bowling Alley5\", \"img\":\"a.jpg\", \"price\":\"500\", \"price_type\":\"pw\", \"size\":\"single\", \"images\":[{\"id\":\"1\", \"url\":\"a.jpg\"}, {\"id\":\"2\", \"url\":\"b.jpg\"}], \"available\":\"2017-04-18\", \"description\":\"Double room in E16 available from 17/04/2017, short walk away from Prince Regent Lane DLR5.\"}, {\"id\":\"6\",\"title\":\"Fun at the Bowling Alley6\", \"img\":\"a.jpg\", \"price\":\"600\", \"price_type\":\"pw\", \"size\":\"single\", \"images\":[{\"id\":\"1\", \"url\":\"a.jpg\"}, {\"id\":\"2\", \"url\":\"b.jpg\"}], \"available\":\"2017-04-18\", \"description\":\"Double room in E16 available from 17/04/2017, short walk away from Prince Regent Lane DLR6.\"} ]}", ProductList.class );
    response.setSuccess(true);
    return response;
}

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

@ClientRequest(action = ACTION.FETCH_BID_LIST)
public ClientResponse getBidList(ISession session, IPacket packet) {
    //BidList response = new Gson().fromJson("{\"bids\":[{\"bidId\":\"1\",\"time\":\"21 Apr 2017 9:38:35AM\",\"amount\":\"1000\", \"currency\":{\"id\":\"1\",\"title\":\"\",\"amount\":\"4.00\",\"currencyUnit\":{\"id\":\"1\",\"title\":\"\"}}, \"user\":{\"firstName\":\"Nazmul\", \"lastName\":\"Hasan\"}}, {\"bidId\":\"2\",\"time\":\"20 Apr 2017 9:38:35AM\",\"amount\":\"2000\", \"currency\":{\"id\":\"1\",\"title\":\"\",\"amount\":\"4.00\",\"currencyUnit\":{\"id\":\"1\",\"title\":\"\"}}, \"user\":{\"firstName\":\"Alamgir\", \"lastName\":\"Kabir\"}}]}", ProductBidList.class );
    //ProductBidList response = new Gson().fromJson("{\"productBidList\":[{\"id\":1,\"bidId\":\"1\",\"bidTime\":\"2017-04-25 9:38:35AM\",\"bidAmount\":\"1000\", \"bidAmountUnit\":{\"id\":\"1\",\"title\":\"\",\"currencyUnit\":{\"id\":\"1\",\"title\":\"\"}}, \"user\":{\"userId\":1,\"firstName\":\"Nazmul\", \"lastName\":\"Hasan\"}, \"product\":{\"productId\":\"p1\"}}, {\"id\":2,\"bidId\":\"2\",\"bidTime\":\"2017-04-27 9:38:35AM\",\"bidAmount\":\"2000\", \"bidAmountUnit\":{\"id\":\"1\",\"title\":\"\",\"currencyUnit\":{\"id\":\"1\",\"title\":\"\"}}, \"user\":{\"userId\":2,\"firstName\":\"Alamgir\", \"lastName\":\"Kabir\"}, \"product\":{\"productId\":\"p1\"}}, {\"id\":1,\"bidId\":\"3\",\"bidTime\":\"2017-04-28 9:38:35AM\",\"bidAmount\":\"3000\", \"bidAmountUnit\":{\"id\":\"1\",\"title\":\"\",\"currencyUnit\":{\"id\":\"1\",\"title\":\"\"}}, \"user\":{\"userId\":3,\"firstName\":\"Shem\", \"lastName\":\"Haye\"}, \"product\":{\"productId\":\"p1\"}}]}", ProductBidList.class );
    Gson gson1 = new Gson();
    Product product = gson1.fromJson(packet.getPacketBody(), Product.class);
    ProductManager productManager = new ProductManager();
    List<ProductBid> productBidList = productManager.getProductBidList(product.getId());
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY);
    Gson gson = gsonBuilder.create();
    String productBidListString = gson.toJson(productBidList);
    ProductBidList response = gson.fromJson("{\"productBidList\":" + productBidListString + "}",
            ProductBidList.class);

    response.setSuccess(true);/*from  w ww .  ja v a  2  s . com*/
    return response;
}

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

@ClientRequest(action = ACTION.FETCH_PRODUCT_INFO)
public ClientResponse getProductInfo(ISession session, IPacket packet) {
    Gson gson1 = new Gson();
    Product product = gson1.fromJson(packet.getPacketBody(), Product.class);
    ProductManager productManager = new ProductManager();

    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY);
    Gson gson = gsonBuilder.create();
    String productString = gson.toJson(productManager.getProductInfo(product.getId()));
    Product response = gson.fromJson(productString, Product.class);

    //Product response = new Gson().fromJson("{\"id\":\"1\",\"productId\":\"p1\",\"user\":{\"id\":\"1\"},\"title\":\"Title of product1.\", \"description\":\"Description of product1.\",\"firstName\":\"Nazmul\",\"lastName\":\"Hasan\",\"phone\":\"01711123456\",\"img\":\"a.jpg\", \"images\":[{\"id\":\"1\", \"url\":\"a.jpg\"}, {\"id\":\"2\", \"url\":\"b.jpg\"}], \"location\":{\"locationId\":\"1\", \"locationType\":\"area\", \"searchString\":\"London\", \"postCode\":\"AB2 8YR\"},\"productType\":{\"id\":\"1\"},\"productSize\":{\"id\":\"1\"},\"productCategory\":{\"id\":\"1\"}, \"amenities\":[{\"id\":\"1\"}, {\"id\":\"2\"}],\"smoking\":{\"id\":\"1\"},\"gender\":{\"id\":\"1\"},\"occupation\":{\"id\":\"1\"},\"pet\":{\"id\":\"1\"}, \"durations\":[{\"id\":\"1\"}, {\"id\":\"2\"}],\"basePrice\":\"100\", \"basePriceUnit\":{\"id\":\"1\",\"title\":\"\",\"currencyUnit\":{\"id\":\"1\",\"title\":\"\"}},\"securityDeposit\":\"200\", \"securityDepositUnit\":{\"id\":\"1\",\"title\":\"\",\"currencyUnit\":{\"id\":\"1\",\"title\":\"\"}}, \"startDate\":\"2017-05-10\", \"endDate\":\"2017-05-16\",\"minStay\":{\"id\":\"1\"},\"maxStay\":{\"id\":\"1\"}, \"isFeaturedAd\":\"true\", \"isDefaultBid\":\"false\",\"adBid\":\"0.3\", \"adBidUnit\":{\"id\":\"1\",\"title\":\"p\",\"currencyUnit\":{\"id\":\"1\",\"title\":\"\"}}}", Product.class );
    if (response != null) {
        response.setSuccess(true);/*  w  ww . j a v a  2 s  . c  o m*/
    } else {
        response = new Product();
        response.setSuccess(false);
    }
    return response;
}

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

@ClientRequest(action = ACTION.FETCH_USER_INFO)
public ClientResponse getUserInfo(ISession session, IPacket packet) {
    //check if user info is provided with id, if not then use user id from the session
    User userInfo = null;/*from   w w  w .  j  av a2  s .  com*/
    int userId = 0;
    try {
        Gson gson = new Gson();
        userInfo = gson.fromJson(packet.getPacketBody(), User.class);
    } catch (Exception ex) {
        logger.error(ex.toString());
    }
    if (userInfo == null || userInfo.getId() == 0) {
        userId = (int) session.getUserId();
    } else {
        userId = userInfo.getId();
    }
    UserManager userManager = new UserManager();

    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY);
    Gson gson = gsonBuilder.create();
    String userProfileString = gson.toJson(userManager.getUserProfileById(userId));
    User response = gson.fromJson(userProfileString, User.class);

    //User response = userManager.getUserProfileById(userId);
    //User response = new Gson().fromJson("{\"userId\":\"1\", \"firstName\":\"Nazmul\", \"lastName\":\"Hasan\", \"email\":\"bdlions@gmail.com\", \"cellNo\":\"8801678112509\", \"img\":\"user.jpg\", \"document\":\"document.jpg\", \"isVerified\":\"true\"}", User.class );
    response.setSuccess(true);
    return response;
}

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

@ClientRequest(action = ACTION.FETCH_ACCOUNT_SETTING_FA)
public ClientResponse getAccountSettingFAInfo(ISession session, IPacket packet) {
    try {/* ww w . j  av a 2 s .co m*/
        int userId = (int) session.getUserId();
        FeaturedAdManager featuredAdManager = new FeaturedAdManager();
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY);
        Gson gson = gsonBuilder.create();
        AccountSettingFA accountSettingFA = featuredAdManager.getFeaturedAdAccountSetting(userId);
        String accountSettingFAString = gson.toJson(accountSettingFA);
        AccountSettingFA response = new Gson().fromJson(accountSettingFAString, AccountSettingFA.class);
        response.setSuccess(true);
        return response;
    } catch (Exception ex) {
        GeneralResponse response = new GeneralResponse();
        response.setSuccess(false);
        return response;
    }
}

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

@ClientRequest(action = ACTION.FETCH_MESSAGE_INBOX_LIST)
public ClientResponse getMessageInbooxList(ISession session, IPacket packet)
        throws InvalidRequestException, Throwable {
    logger.debug("processing request - fetch message inbox list");
    int userId = (int) session.getUserId();
    try {/*from w  w  w.j a  va  2  s. com*/
        MessageManager messageManager = new MessageManager();
        List<Message> messageList = messageManager.getInboxMessageList(userId);
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY);
        Gson gson = gsonBuilder.create();
        String messageListString = gson.toJson(messageList);
        MessageList response = gson.fromJson("{\"messageList\":" + messageListString + "}", MessageList.class);
        response.setSuccess(true);
        logger.debug("fetch message inbox list completed.");
        return response;
    } catch (Exception ex) {
        logger.debug(ex.toString());
        throw new InvalidRequestException();
    }
}

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

@ClientRequest(action = ACTION.FETCH_MESSAGE_SENT_LIST)
public ClientResponse getMessageSentList(ISession session, IPacket packet) {
    int userId = (int) session.getUserId();
    MessageManager messageManager = new MessageManager();
    List<Message> messageList = messageManager.getSentMessageList(userId);
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY);
    Gson gson = gsonBuilder.create();
    String messageListString = gson.toJson(messageList);
    MessageList response = gson.fromJson("{\"messageList\":" + messageListString + "}", MessageList.class);
    response.setSuccess(true);//from w  w  w  . j  a va2 s .c  om
    return response;
}

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

@ClientRequest(action = ACTION.FETCH_MESSAGE_INFO)
public ClientResponse getMessageInfo(ISession session, IPacket packet) {
    Gson gson1 = new Gson();
    Message message = gson1.fromJson(packet.getPacketBody(), Message.class);
    MessageManager messageManager = new MessageManager();

    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY);
    Gson gson = gsonBuilder.create();
    String messageInfoString = gson.toJson(messageManager.getMessageInfo(message.getId()));
    Message response = gson.fromJson(messageInfoString, Message.class);

    if (response != null) {
        response.setSuccess(true);//w  w  w .  ja va  2  s. c o m
    } else {
        response = new Message();
        response.setSuccess(false);
    }
    return response;
}