Example usage for com.google.gson GsonBuilder setDateFormat

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

Introduction

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

Prototype

public GsonBuilder setDateFormat(int style) 

Source Link

Document

Configures Gson to to serialize Date objects according to the style value provided.

Usage

From source file:com.mediamath.terminalone.utils.T1JsonToObjParser.java

License:Apache License

/**
 * parses video creative upload status response to VideoCreativeUploadStatus entity.
 * //w  w  w. j  av a2 s .  co m
 * @param pResponse
 *          response string.
 * @return VideoCreativeUploadStatus object.
 */
public VideoCreativeUploadStatus parseVideoCreativeUploadStatus(String pResponse) {
    VideoCreativeUploadStatus response = null;
    GsonBuilder builder = new GsonBuilder();
    builder.setDateFormat(YYYY_MM_DD_T_HH_MM_SS);
    Gson gson = builder.create();
    response = gson.fromJson(pResponse, VideoCreativeUploadStatus.class);
    return response;
}

From source file:com.nimbits.server.gson.GsonFactory.java

License:Apache License

public static Gson getInstance(boolean excludeFieldsWithoutExposeAnnotation) {

    if (gson == null) {
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setDateFormat(Const.GSON_DATE_FORMAT)
                .registerTypeAdapter(Date.class, new DateDeserializer())
                .registerTypeAdapter(Entity.class, new EntityDeserializer())
                .registerTypeAdapter(Point.class, new PointSerializer())
                .registerTypeAdapter(Entity.class, new EntitySerializer())
                .excludeFieldsWithoutExposeAnnotation();

        for (EntityType t : EntityType.values()) {
            gsonBuilder.registerTypeAdapter(t.getClz(), new NimbitsDeserializer<Entity>(t))
                    .registerTypeAdapter(t.getClz(), new NimbitsSerializer<Entity>());
        }//from w  w  w . j a va2s. c o m
        gson = gsonBuilder.create();
    }

    if (excludedInstance == null) {
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setDateFormat(Const.GSON_DATE_FORMAT)
                .registerTypeAdapter(Date.class, new DateDeserializer())
                .registerTypeAdapter(Entity.class, new EntityDeserializer())
                .registerTypeAdapter(Point.class, new PointSerializer())
                .registerTypeAdapter(Entity.class, new EntitySerializer());

        for (EntityType t : EntityType.values()) {
            gsonBuilder.registerTypeAdapter(t.getClz(), new NimbitsDeserializer<Entity>(t))
                    .registerTypeAdapter(t.getClz(), new NimbitsSerializer<Entity>());
        }
        excludedInstance = gsonBuilder.create();
    }

    return excludeFieldsWithoutExposeAnnotation ? gson : excludedInstance;

}

From source file:com.piemanpete.go.plugin.artifactorypackage.JsonUtil.java

License:Apache License

public static String toJsonString(Object object) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    return gsonBuilder.create().toJson(object);
}

From source file:com.piemanpete.go.plugin.artifactorypackage.JsonUtil.java

License:Apache License

public static <T> T fromJsonString(String json, Class<T> type) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    return gsonBuilder.create().fromJson(json, type);
}

From source file:com.pte.liquid.relay.marshaller.json.JsonMarshaller.java

License:Apache License

public JsonMarshaller() {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.excludeFieldsWithoutExposeAnnotation();
    gsonBuilder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
    gson = gsonBuilder.create();//from w  ww.jav a 2 s  .co m

}

From source file:com.quix.aia.cn.imo.rest.AddressBookRest.java

License:Open Source License

/**
 * <p>/*www  .  ja  v  a  2s  . c  o m*/
 * This method retrieves List of Address Book for particular Agent.
 * </p>
 * 
 * @param agentId
 * 
 */
@GET
@Path("/getAgentAddressBook")
@Produces(MediaType.APPLICATION_JSON)
public Response getAgentAddressBook(@Context HttpServletRequest request, @Context ServletContext context) {
    log.log(Level.INFO, "Address Book --> getAgentAddressBook ");

    GsonBuilder builder = new GsonBuilder();
    AddressBookMaintenance addressBookMaintenance = new AddressBookMaintenance();
    Gson googleJson = null;
    MsgBeans beans = new MsgBeans();
    AuditTrailMaintenance auditTrailMaint = new AuditTrailMaintenance();
    List<AddressBook> addressBookList = new ArrayList();
    String jsonString = "";
    try {

        builder.registerTypeHierarchyAdapter(byte[].class, new JsonSerializer<byte[]>() {
            public JsonElement serialize(byte[] src, Type typeOfSrc, JsonSerializationContext context) {
                return new JsonPrimitive(Base64.encodeBase64String(src));
            }
        });

        googleJson = builder.setDateFormat("yyyy-MM-dd HH:mm:ss").create();

        log.log(Level.INFO, "Address Book --> fetching Information ... ");
        addressBookList = addressBookMaintenance.getAgentAddressBook(request, context);
        // Convert the object to a JSON string
        log.log(Level.INFO, "Address Book --> Information fetched successfully... ");
        jsonString = googleJson.toJson(addressBookList);

        return Response.status(200).entity(jsonString).build();
    } catch (Exception e) {

        beans.setCode("500");
        beans.setMassage(
                "Something wrong happens, please contact administrator. Error Message : " + e.getMessage());
        auditTrailMaint.insertAuditTrail(
                new AuditTrail("Rest", AuditTrail.MODULE_ADDRESS_BOOK, AuditTrail.FUNCTION_FAIL, "FAILED"));

        log.log(Level.SEVERE, "Address Book --> Error in fetching Record.");
        log.log(Level.SEVERE, e.getMessage());
        e.printStackTrace();
        StringWriter errors = new StringWriter();
        e.printStackTrace(new PrintWriter(errors));
        LogsMaintenance logsMain = new LogsMaintenance();
        logsMain.insertLogs("AddressBookRest", Level.SEVERE + "", errors.toString());

        return Response.status(200).entity(googleJson.toJson(beans)).build();
    } finally {
        addressBookList.clear();
        auditTrailMaint = null;
        addressBookMaintenance = null;
        jsonString = null;
        beans = null;
        builder = null;
        googleJson = null;
        System.gc();
    }
}

From source file:com.quix.aia.cn.imo.rest.AddressBookRest.java

License:Open Source License

@GET
@Path("/getContractDetail")
@Produces(MediaType.APPLICATION_JSON)//from  www . j a  va  2 s .  c  om
public Response getContractDetails(@Context HttpServletRequest request) {
    log.log(Level.INFO, "Address Book --> getContractDetail ");

    ContractDetail contractDetail = new ContractDetail();
    AddressBook addressBook;
    Object[] obj;
    GsonBuilder builder = new GsonBuilder();
    AddressBookMaintenance addressBookMaintenance = new AddressBookMaintenance();

    Gson googleJson = null;
    MsgBeans beans = new MsgBeans();
    AuditTrailMaintenance auditTrailMaint = new AuditTrailMaintenance();
    String jsonString = "";
    try {

        googleJson = builder.setDateFormat("yyyy-MM-dd HH:mm:ss").create();

        log.log(Level.INFO, "Address Book --> fetching Information ... ");
        addressBook = new AddressBook();
        String candidateAgentCode = request.getParameter("candidateAgentCode");
        candidateAgentCode = null != candidateAgentCode ? candidateAgentCode : "";

        String[] fetchFields = { "branchCode", "candidateAgentCode", "recruitmentType", "contractDate" };
        String[] conditionFieldName = { "addressCode" };
        String[] conditionFieldValue = { candidateAgentCode };
        List<Object[]> list = addressBookMaintenance.getAddressBookSelectedField(fetchFields,
                conditionFieldName, conditionFieldValue);

        if (null != list && !list.isEmpty()) {
            obj = (Object[]) list.get(0);

            contractDetail.setBranchCode((String) obj[0]);
            contractDetail.setCandidateAgentCode((String) obj[1]);
            contractDetail.setRecruitmentType((String) obj[2]);
            contractDetail.setContractDate((Date) obj[3]);
        }
        // Convert the object to a JSON string
        log.log(Level.INFO, "Address Book --> Information fetched successfully... ");

        jsonString = googleJson.toJson(contractDetail);

        return Response.status(200).entity(jsonString).build();
    } catch (Exception e) {

        beans.setCode("500");
        beans.setMassage(
                "Something wrong happens, please contact administrator. Error Message : " + e.getMessage());
        auditTrailMaint.insertAuditTrail(
                new AuditTrail("Rest", AuditTrail.MODULE_ADDRESS_BOOK, AuditTrail.FUNCTION_FAIL, "FAILED"));

        log.log(Level.SEVERE, "Address Book --> Error in fetching Record.");
        log.log(Level.SEVERE, e.getMessage());
        e.printStackTrace();
        LogsMaintenance logsMain = new LogsMaintenance();
        StringWriter errors = new StringWriter();
        e.printStackTrace(new PrintWriter(errors));
        logsMain.insertLogs("AddressBookRest", Level.SEVERE + "", errors.toString());

        return Response.status(200).entity(googleJson.toJson(beans)).build();
    } finally {
        auditTrailMaint = null;
        addressBookMaintenance = null;
        jsonString = null;
        beans = null;
        builder = null;
        googleJson = null;
        System.gc();
    }
}

From source file:com.quix.aia.cn.imo.rest.AddressBookRest.java

License:Open Source License

@GET
@Path("/getCCTestResults")
@Produces(MediaType.APPLICATION_JSON)/*from  ww w. j av a2 s  . c  o m*/
public Response getCCTestResults(@Context HttpServletRequest request) {
    log.log(Level.INFO, "Address Book --> getCCTestResults ");
    LogsMaintenance logsMain = new LogsMaintenance();
    ContractDetail contractDetail = new ContractDetail();
    AddressBook addressBook;
    GsonBuilder builder = new GsonBuilder();
    AddressBookMaintenance addressBookMaintenance = new AddressBookMaintenance();

    Gson googleJson = null;
    MsgBeans beans = new MsgBeans();
    AuditTrailMaintenance auditTrailMaint = new AuditTrailMaintenance();
    List<AddressBook> addressBookList = new ArrayList();
    String jsonString = "";
    try {

        builder.registerTypeHierarchyAdapter(byte[].class, new JsonSerializer<byte[]>() {
            public JsonElement serialize(byte[] src, Type typeOfSrc, JsonSerializationContext context) {
                return new JsonPrimitive(Base64.encodeBase64String(src));
            }
        });

        googleJson = builder.setDateFormat("yyyy-MM-dd HH:mm:ss").create();

        log.log(Level.INFO, "Address Book --> fetching Information ... ");
        addressBook = new AddressBook();
        String candidateCode = request.getParameter("candidateCode");
        candidateCode = null != candidateCode ? candidateCode : "";
        String agentId = request.getParameter("agentId");
        agentId = null != agentId ? agentId : "";

        //addressBook = addressBookMaintenance.getAddressBook(candidateCode, agentId);
        String[] fetchFields = { "ccTestResult", "ccTestResultDate" };
        String[] conditionFieldName = { "addressCode", "agentId" };
        String[] conditionFieldValue = { candidateCode, agentId };
        List<Object[]> list = addressBookMaintenance.getAddressBookSelectedField(fetchFields,
                conditionFieldName, conditionFieldValue);

        addressBookMaintenance.updateAddressBookStatus("6/9", conditionFieldName, conditionFieldValue);
        if (!list.isEmpty()) {
            Object[] obj = list.get(0);
            addressBook.setCcTestResult("" + obj[0]);
            addressBook.setCcTestResultDate((Date) obj[1]);
        }

        jsonString += "[{\"CCTestResult\":\"" + addressBook.getCcTestResult() + "\",\"Date\":\""
                + LMSUtil.convertDateToyyyymmddhhmmssDashedString(addressBook.getCcTestResultDate()) + "\"}]";
        // Convert the object to a JSON string
        log.log(Level.INFO, "Address Book --> Information fetched successfully... ");

        return Response.status(200).entity(jsonString).build();
    } catch (Exception e) {
        beans.setCode("500");
        beans.setMassage(
                "Something wrong happens, please contact administrator. Error Message : " + e.getMessage());
        auditTrailMaint.insertAuditTrail(
                new AuditTrail("Rest", AuditTrail.MODULE_ADDRESS_BOOK, AuditTrail.FUNCTION_FAIL, "FAILED"));

        log.log(Level.SEVERE, "Address Book --> Error in fetching Record.");
        log.log(Level.SEVERE, e.getMessage());
        e.printStackTrace();
        StringWriter errors = new StringWriter();
        e.printStackTrace(new PrintWriter(errors));
        logsMain.insertLogs("AddressBookRest", Level.SEVERE + "", errors.toString());

        return Response.status(200).entity(googleJson.toJson(beans)).build();
    } finally {
        addressBookList.clear();
        auditTrailMaint = null;
        addressBookMaintenance = null;
        jsonString = null;
        beans = null;
        builder = null;
        googleJson = null;
        System.gc();
    }
}

From source file:com.quix.aia.cn.imo.rest.FirstInterviewRest.java

License:Open Source License

/**
 * <p>//from  w  ww .j  a  va  2 s  .  c o m
 * This method gets Candidate First Interview Results.
 * </p>
 */
@GET
@Path("/getFirstInterview")
@Produces(MediaType.APPLICATION_JSON)
public Response getALEExamResults(@Context HttpServletRequest request, @Context ServletContext context) {

    log.log(Level.INFO, "First Interview --> get ALE Exam Results.");

    MsgBeans beans = new MsgBeans();
    Gson googleJson = null;
    GsonBuilder builder = new GsonBuilder();
    AuditTrailMaintenance auditTrailMaint = new AuditTrailMaintenance();
    CandidateFirstInterviewMaintenance candidateFirstInterviewMaint = new CandidateFirstInterviewMaintenance();
    CandidateFirstInterview candidateFirstInterview = null;
    String jsonString = "";

    try {

        String candidateCode = request.getParameter("candidateCode");
        String agentId = request.getParameter("agentId");

        log.log(Level.INFO, "First Interview --> fetching Information ... ");

        candidateFirstInterview = candidateFirstInterviewMaint.getCandidateFirstinterview(agentId,
                candidateCode);

        candidateFirstInterview.setAgentId(null);
        candidateFirstInterview.setCandidateCode(null);
        candidateFirstInterview.setFirstInterviewCode(null);
        //candidateFirstInterview.setRecruitmentPlan(null);
        //candidateFirstInterview.setRemarks(null);

        // Convert the object to a JSON string
        log.log(Level.INFO, "First Interview --> Information fetched successfully... ");

        return Response.status(200)
                .entity(builder.setDateFormat("yyyy-MM-dd HH:mm:ss").create().toJson(candidateFirstInterview))
                .build();
    } catch (Exception e) {
        beans.setCode("500");
        beans.setMassage(
                "Something wrong happens, please contact administrator. Error Message : " + e.getMessage());

        log.log(Level.SEVERE, "Training --> Error in fetching Record.");
        log.log(Level.SEVERE, e.getMessage());
        e.printStackTrace();
        StringWriter errors = new StringWriter();
        e.printStackTrace(new PrintWriter(errors));
        LogsMaintenance logsMain = new LogsMaintenance();
        logsMain.insertLogs("FirstInterviewRest", Level.SEVERE + "", errors.toString());

        auditTrailMaint.insertAuditTrail(
                new AuditTrail("Rest", AuditTrail.MODULE_TRAINING, AuditTrail.FUNCTION_FAIL, "FAILED"));

        return Response.status(500).entity(new Gson().toJson(beans)).build();
    } finally {
        auditTrailMaint = null;
        jsonString = null;
        beans = null;
        System.gc();
    }
}

From source file:com.quix.aia.cn.imo.rest.InterviewRest.java

License:Open Source License

@GET
@Path("/getcandidatDetails")
@Produces({ MediaType.APPLICATION_JSON })
public Response getcandidatDetails(@Context HttpServletRequest request, @Context ServletContext context) {
    log.log(Level.INFO, "InterviewRest --> getcandidatDetails");
    MsgBeans beans = new MsgBeans();
    AuditTrailMaintenance auditTrailMaint = new AuditTrailMaintenance();
    String candidateCode = request.getParameter("candidateCode");
    candidateCode = candidateCode == null || candidateCode.equals("") ? "0" : candidateCode;
    String agentId = request.getParameter("agentId");
    agentId = agentId == null || agentId.equals("") ? "0" : agentId;
    String interviewCode = request.getParameter("interviewCode");
    interviewCode = interviewCode == null || interviewCode.equals("") ? "0" : interviewCode;

    GsonBuilder builder = new GsonBuilder();

    try {//from w  w  w. j av a2  s .c  o m
        InterviewAttendanceMaintenance objMaintenance = new InterviewAttendanceMaintenance();
        InterviewCandidate interviewCandidate = objMaintenance.getCandidateDetailsRest(request,
                Integer.parseInt(interviewCode), Integer.parseInt(candidateCode), agentId);

        //String responseJsonString = "[{\"ccTestResult\":\""+interviewCandidate.getCcTestResult()+"\"}]";
        auditTrailMaint.insertAuditTrail(
                new AuditTrail("Rest", AuditTrail.MODULE_INTERVIEW, AuditTrail.FUNCTION_REST, "SUCCESS"));
        return Response.status(200)
                .entity(builder.setDateFormat("yyyy-MM-dd HH:mm:ss").create().toJson(interviewCandidate))
                .build();
    } catch (Exception e) {
        log.log(Level.INFO, "InterviewRest --> deleteInterviewRegistration --> Exception..... ");
        log.log(Level.SEVERE, e.getMessage());
        StringWriter errors = new StringWriter();
        e.printStackTrace(new PrintWriter(errors));
        LogsMaintenance logsMain = new LogsMaintenance();
        logsMain.insertLogs("InterviewRest", Level.SEVERE + "", errors.toString());

        auditTrailMaint.insertAuditTrail(
                new AuditTrail("Rest", AuditTrail.MODULE_INTERVIEW, AuditTrail.FUNCTION_FAIL, "FAILED"));
        beans.setCode("500");
        beans.setMassage("Database Error");
        return Response.status(500).entity(new Gson().toJson(beans)).build();
    }
}