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:fi.vm.sade.osoitepalvelu.kooste.mvc.AppSettingsController.java

@ApiOperation("Palauttaa sovelluksen polkuviittauksia sisltvt, aikaisessa kyttliittymn alustuksen "
        + "vaiheessa tarpeelliset asetukset kyttliittymlle JavaScriptin, joka tuottaa asetukset"
        + " window.CONFIG -olioon.")
@RequestMapping(value = "/settings.js", method = RequestMethod.GET, produces = "text/javascript")
@ResponseBody/*from ww  w . j  av a 2  s  . c  om*/
public String settingsJs() throws IOException {
    AppSettingsDto settings = appSettingsService.getUiSettings();
    ObjectMapper mapper = objectMapperProvider.getContext(ObjectMapper.class);
    return "window.CONFIG  =  " + mapper.writeValueAsString(settings) + ";";
}

From source file:com.abuabdul.mytravelpal.controller.MyTravelPalLandingController.java

@RequestMapping(value = "/secure/travel/loadTravelMode.go", produces = "application/json")
@ResponseBody/* w  w w .  j a va  2 s. co m*/
public String myTravelPalTravelMode(HttpServletResponse response) throws JsonProcessingException {
    log.debug("Entering myTravelPalTravelMode() in " + this.getClass().getName());
    Map<String, String> map = Maps.newHashMap();
    map.put("", "Select");
    map.putAll(travelModesMap);
    response.setStatus(HttpServletResponse.SC_OK);
    ObjectMapper mapper = new ObjectMapper();
    return mapper.writeValueAsString(map);
}

From source file:com.abuabdul.mytravelpal.controller.MyTravelPalLandingController.java

@RequestMapping(value = "/secure/travel/loadTravelType.go", produces = "application/json")
@ResponseBody/*  w ww .j  av  a2s . co m*/
public String myTravelPalTravelType(HttpServletResponse response) throws JsonProcessingException {
    log.debug("Entering myTravelPalTravelType() in " + this.getClass().getName());
    Map<String, String> map = Maps.newHashMap();
    map.put("", "Select");
    map.putAll(travelTypesMap);
    response.setStatus(HttpServletResponse.SC_OK);
    ObjectMapper mapper = new ObjectMapper();
    return mapper.writeValueAsString(map);
}

From source file:net.mindengine.galen.api.PageDump.java

public void exportAsHtml(String title, File file) throws IOException {
    makeSureFileExists(file);//  w  ww.j ava 2  s  .  co m
    ObjectMapper objectMapper = new ObjectMapper();
    String jsonText = objectMapper.writeValueAsString(this);

    String template = IOUtils.toString(getClass().getResourceAsStream("/pagedump/page.html"));

    String htmlText = template.replace("${title}", title);
    htmlText = htmlText.replace("${json}", jsonText);

    FileUtils.writeStringToFile(file, htmlText);
}

From source file:de.dev.eth0.elasticsearch.aem.indexing.ElasticSearchIndexContentBuilder.java

private ReplicationContent createReplicationContent(ReplicationContentFactory factory, IndexEntry content)
        throws ReplicationException {
    Path tempFile;/*from www . j  a v a  2  s  . c  om*/

    try {
        tempFile = Files.createTempFile("elastic_index", ".tmp");
    } catch (IOException e) {
        throw new ReplicationException("Could not create temporary file", e);
    }

    try (BufferedWriter writer = Files.newBufferedWriter(tempFile, Charset.forName("UTF-8"))) {
        ObjectMapper mapper = new ObjectMapper();
        writer.write(mapper.writeValueAsString(content));
        writer.flush();

        return factory.create("text/plain", tempFile.toFile(), true);
    } catch (IOException e) {
        throw new ReplicationException("Could not write to temporary file", e);
    }
}

From source file:com.dub.skoolie.web.controller.system.schedule.events.SystemSchoolEventController.java

@RequestMapping(value = "/system/schedule/events/school/{id}", method = RequestMethod.GET)
public @ResponseBody String getSchoolEvents(@PathVariable("id") Long school) throws JsonProcessingException {
    List<SchoolEventBean> schoolEventBeans = uiSchoolEventServiceImpl.getSchoolEventsBySchool(school);
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    //JsonObjectBuilder object = Json.createObjectBuilder().add("id", "1").add("title", "Test event").add("allDay", "").add("end", "2016-06-06 14:00:00").add("start","2016-06-06 12:00:00");
    //JsonObjectBuilder object2 = Json.createObjectBuilder().add("id", "2").add("title", "Test event 2").add("allDay", "").add("end", "2016-06-26 14:00:00").add("start","2016-06-26 12:00:00");
    ObjectMapper mapper = new ObjectMapper();
    //mapper.setDateFormat(df);
    //JsonArray array = Json.createArrayBuilder().add(object).add(object2).build();
    return mapper.writeValueAsString(schoolEventBeans);
}

From source file:de.unirostock.sems.cbarchive.web.dataholder.WorkspaceHistory.java

@JsonIgnore
public String toCookieJson() throws JsonProcessingException {

    ObjectMapper mapper = new ObjectMapper();
    String json = mapper.writeValueAsString(recentWorkspaces);

    json = Base64.encodeBase64URLSafeString(json.getBytes());
    return json;//  w ww .j ava2 s.c  om
}

From source file:com.samlikescode.stackoverflow.questions.q29630371.PersonTest.java

@Test
    public void serializePerson2Test() throws JsonProcessingException {
        // set up test data including parent properties
        Person person = new Person("Database property 1", "Database property 2");
        person.setIndex("1");
        person.setFirstName("Joe");
        person.setLastName("Bob");

        // register the mix in
        //        ObjectMapper om = new ObjectMapper()
        //                .addMixIn(Person.class, PersonMixIn.class);
        ObjectMapper om = new ObjectMapper();

        // translate object to JSON string using Jackson
        String json = om.writeValueAsString(person);

        //        assertFalse(json.contains("dbPropertyA"));
        //        assertFalse(json.contains("dbPropertyB"));
        //        assertFalse(json.contains("index"));
        System.out.println(json);
    }/*  w w  w . j a va  2  s  .c  o  m*/

From source file:de.elanev.studip.android.app.backend.datamodel.Settings.java

public String toJson() {
    ObjectMapper mapper = new ObjectMapper();
    String json = "";
    try {//from w  ww  .  j a v  a  2s  .  c  o  m
        json = mapper.writeValueAsString(this);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
    return json;
}

From source file:org.apache.ambari.view.registry.web.service.ApplicationService.java

private String getJsonString(ApplicationConfig config) throws JsonProcessingException {
    ObjectMapper mapper = new ObjectMapper();
    return mapper.writeValueAsString(config);
}