Example usage for com.fasterxml.jackson.databind ObjectMapper writeValueAsString

List of usage examples for com.fasterxml.jackson.databind ObjectMapper writeValueAsString

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind ObjectMapper writeValueAsString.

Prototype

@SuppressWarnings("resource")
public String writeValueAsString(Object value) throws JsonProcessingException 

Source Link

Document

Method that can be used to serialize any Java value as a String.

Usage

From source file:api.QuizResource.java

@GET
@Path("question/{chapter}/{number}")
@Produces(MediaType.APPLICATION_JSON)//from ww w . j  a  v  a2  s .c  o  m
public String getSpecificQuestion(@PathParam("chapter") Integer chapter, @PathParam("number") Integer number) {
    //        if(!userBean.isLoggedIn()) {
    //            return null;
    //        }
    //        
    ObjectMapper mapper = new ObjectMapper();

    try {
        return mapper.writeValueAsString(quizBean.lookupQuestion(chapter, number));
    } catch (JsonProcessingException ex) {
        ex.printStackTrace();
    }

    return "";
}

From source file:api.QuizResource.java

@GET
@Path("check/{chapter}/{number}")
@Produces(MediaType.APPLICATION_JSON)//w  w w  .  ja  v  a 2s.co  m
public String checkAnswer(@PathParam("chapter") Integer chapter, @PathParam("number") Integer number,
        @QueryParam("answer") List<String> answers) {

    boolean result = quizBean.checkAnswer(chapter, number, answers);

    ObjectMapper mapper = new ObjectMapper();

    try {
        return mapper.writeValueAsString(Collections.singletonMap("isCorrect", result));
    } catch (JsonProcessingException ex) {
        ex.printStackTrace();
    }

    return jsonError("Unknown json failure :(");
}

From source file:api.QuizResource.java

/**
 * Retrieves representation of an instance of api.QuizResource
 * @return an instance of java.lang.String
 *///from w  w w. j  a  v  a2s. c  om
@GET
@Path("{chapter}")
@Produces(MediaType.APPLICATION_JSON)
public String getAllChapterQuestions(@PathParam("chapter") Integer chapter) {
    //        if(!userBean.isLoggedIn()) {
    //            return null;
    //        }
    //        
    ObjectMapper mapper = new ObjectMapper();

    try {
        return mapper.writeValueAsString(quizBean.lookupQuestionsByChapter(chapter));
    } catch (JsonProcessingException ex) {
        ex.printStackTrace();
    }

    return "";
}

From source file:api.QuizResource.java

@GET
@Path("{chapter}/{section}")
@Produces(MediaType.APPLICATION_JSON)/*from   ww  w .j a  va2s  . co  m*/
public String getAllSectionQuestions(@PathParam("chapter") Integer chapter,
        @PathParam("section") Integer section) {
    //        if(!userBean.isLoggedIn()) {
    //            return null;
    //        }
    //        
    ObjectMapper mapper = new ObjectMapper();

    try {
        return mapper.writeValueAsString(quizBean.lookupQuestionsBySection(chapter, section));
    } catch (JsonProcessingException ex) {
        ex.printStackTrace();
    }

    return "";
}

From source file:api.QuizResource.java

@GET
@Path("userstatus/{chapter}/{number}/{username}")
@Produces(MediaType.APPLICATION_JSON)/*  ww w  .  j a v  a 2  s .c o  m*/
public String getUserAnswerStatus(@PathParam("chapter") Integer chapter, @PathParam("number") Integer number,
        @PathParam("username") String username) {

    int status = quizBean.answerStatus(chapter, number, username);

    ObjectMapper mapper = new ObjectMapper();

    try {
        return mapper.writeValueAsString(
                Collections.singletonMap("answerStatus", QuizQuestion.statusToString(status)));
    } catch (JsonProcessingException ex) {
        ex.printStackTrace();
    }

    return jsonError("Unknown json failure :(");
}

From source file:org.zalando.jackson.datatype.money.MonetaryAmountSerializerTest.java

@Test
public void shouldSerialize() throws JsonProcessingException {
    final ObjectMapper unit = new ObjectMapper().findAndRegisterModules();

    final String expected = "{\"amount\":29.95,\"currency\":\"EUR\"}";
    final String actual = unit.writeValueAsString(Money.of(29.95, "EUR"));

    assertThat(actual, is(expected));/*  w  w w .  ja v a 2 s  .  c o  m*/
}

From source file:ch.icclab.cyclops.resource.impl.RateStatusResource.java

/**
 * Build the dynamic rate status response
 *
 * Pseudo Code/*w  w  w.  j a  va2 s.  c o  m*/
 * 1. Set the rating policy as dynamic and construct the response
 * 2. Return the response json string
 *
 * @return Representation
 */
private Representation buildDynamicRateResponse() {
    String jsonStr = null;
    RateStatusResponse response = new RateStatusResponse();
    ObjectMapper mapper = new ObjectMapper();
    response.setRate_policy("dynamic");
    response.setRate(null);
    try {
        jsonStr = mapper.writeValueAsString(response);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
    JsonRepresentation jsonResp = new JsonRepresentation(jsonStr);
    return jsonResp;
}

From source file:com.hortonworks.streamline.streams.catalog.TopologyComponent.java

@JsonIgnore
public String getConfigData() throws Exception {
    if (config != null) {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(config);
    }// w w w . ja  v  a2  s .c  o  m
    return "";
}

From source file:api.QuizResource.java

@GET
@Path("submit/{chapter}/{number}")
@Produces(MediaType.APPLICATION_JSON)// w  w  w.j  a  v  a  2 s  . c  o m
public String submitAnswer(@PathParam("chapter") Integer chapter, @PathParam("number") Integer number,
        @QueryParam("answer") List<String> answers) {

    if (!userBean.isLoggedIn()) {
        return jsonError("Gotta be logged in bruv.");
    }

    boolean result = quizBean.submitAnswer(chapter, number, answers, userBean);

    ObjectMapper mapper = new ObjectMapper();

    try {
        return mapper.writeValueAsString(Collections.singletonMap("isCorrect", result));
    } catch (JsonProcessingException ex) {
        ex.printStackTrace();
    }

    return jsonError("Unknown json failure :(");
}

From source file:com.googlecode.jsonschema2pojo.integration.EnumIT.java

@Test
@SuppressWarnings("unchecked")
public void jacksonCanMarshalEnums() throws NoSuchMethodException, InstantiationException,
        IllegalAccessException, InvocationTargetException, IOException {

    Object valueWithEnumProperty = parentClass.newInstance();
    Method enumSetter = parentClass.getMethod("setEnumProperty", enumClass);
    enumSetter.invoke(valueWithEnumProperty, enumClass.getEnumConstants()[2]);

    ObjectMapper objectMapper = new ObjectMapper();

    String jsonString = objectMapper.writeValueAsString(valueWithEnumProperty);
    JsonNode jsonTree = objectMapper.readTree(jsonString);

    assertThat(jsonTree.size(), is(1));//ww w. j a  va2s .co  m
    assertThat(jsonTree.has("enum_Property"), is(true));
    assertThat(jsonTree.get("enum_Property").isTextual(), is(true));
    assertThat(jsonTree.get("enum_Property").asText(), is("3rd one"));
}