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

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

Introduction

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

Prototype

public ObjectWriter writerWithDefaultPrettyPrinter() 

Source Link

Document

Factory method for constructing ObjectWriter that will serialize objects using the default pretty printer for indentation

Usage

From source file:org.talend.core.repository.utils.ProjectDataJsonProvider.java

private static void saveProjectSettings(Project project) throws PersistenceException {
    ProjectSettings projectSetting = new ProjectSettings();
    projectSetting//from ww  w.j  av  a2s  .  c om
            .setImplicitContextSettingJson(getImplicitContextSettingJson(project.getImplicitContextSettings()));
    projectSetting.setStatAndLogsSettingJson(getStatAndLogsSettingJson(project.getStatAndLogsSettings()));
    projectSetting.setTechnicalStatus(getTechnicalStatusJson(project.getTechnicalStatus()));
    projectSetting.setDocumentationStatus(getDocumentationJson(project.getDocumentationStatus()));
    File file = getSavingConfigurationFile(project.getTechnicalLabel(), FileConstants.PROJECTSETTING_FILE_NAME);
    try {
        if (!file.exists()) {
            file.createNewFile();
        }
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.writerWithDefaultPrettyPrinter().writeValue(file, projectSetting);
    } catch (Exception e) {
        throw new PersistenceException(e);
    }
}

From source file:org.talend.core.repository.utils.ProjectDataJsonProvider.java

public static void saveConfigComponent(String projectLabel, List<ComponentsJsonModel> componentJsons)
        throws PersistenceException {
    Collections.sort(componentJsons, new Comparator<ComponentsJsonModel>() {

        @Override//from ww  w  .  jav a 2  s . c  o  m
        public int compare(ComponentsJsonModel configTypeNode1, ComponentsJsonModel configTypeNode2) {
            return configTypeNode1.getId().compareTo(configTypeNode2.getId());
        }

    });

    File file = getSavingConfigurationFile(projectLabel, FileConstants.COMPONENT_FILE_NAME);
    try {
        if (!file.exists()) {
            file.createNewFile();
        }
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.writerWithDefaultPrettyPrinter().writeValue(file, componentJsons);
        ResourceUtils.getProject(projectLabel).getFolder(FileConstants.SETTINGS_FOLDER_NAME).refreshLocal(1,
                null);
    } catch (Exception e) {
        throw new PersistenceException(e);
    }
}

From source file:jenkins.metrics.api.MetricsRootAction.java

/**
 * Utility wrapper to construct the writer to use for a {@link HttpServletResponse} based on the
 * {@link HttpServletRequest}.//from w w  w.  j av  a2 s . c o m
 *
 * @param mapper  the {@link ObjectMapper} to use.
 * @param request the {@link HttpServletRequest} to respond to.
 * @return the {@link ObjectWriter} to use for the response.
 */
private static ObjectWriter getWriter(ObjectMapper mapper, HttpServletRequest request) {
    final boolean prettyPrint = Boolean.parseBoolean(request.getParameter("pretty"));
    if (prettyPrint) {
        return mapper.writerWithDefaultPrettyPrinter();
    }
    return mapper.writer();
}

From source file:net.riezebos.thoth.renderers.RendererBase.java

protected void executeJson(Map<String, Object> variables, OutputStream outputStream) throws ServletException {
    try {//from   w  ww .j a v a2 s .c om
        boolean prettyPrintJson = getConfiguration().isPrettyPrintJson();
        ObjectMapper mapper = new ObjectMapper();
        ObjectWriter writer = prettyPrintJson ? mapper.writerWithDefaultPrettyPrinter() : mapper.writer();
        writer.writeValue(outputStream, variables);
    } catch (Exception e) {
        throw new ServletException(e);
    }
}

From source file:com.groupon.odo.controllers.BackupController.java

/**
 * Get all backup data//from ww  w .  j  av  a2s.  c o m
 *
 * @param model
 * @return
 * @throws Exception
 */
@SuppressWarnings("deprecation")
@RequestMapping(value = "/api/backup", method = RequestMethod.GET)
public @ResponseBody String getBackup(Model model, HttpServletResponse response) throws Exception {
    response.addHeader("Content-Disposition", "attachment; filename=backup.json");
    response.setContentType("application/json");

    Backup backup = BackupService.getInstance().getBackupData();
    ObjectMapper objectMapper = new ObjectMapper();
    ObjectWriter writer = objectMapper.writerWithDefaultPrettyPrinter();

    return writer.withView(ViewFilters.Default.class).writeValueAsString(backup);
}

From source file:edu.ucsd.crbs.cws.App.java

License:asdf

public static void addNewWorkspaceFile(OptionSet optionSet, boolean uploadFile, final String theArg)
        throws Exception {
    String postURL = null;/* w ww  .  j  av  a 2 s.c o m*/
    if (optionSet.has(URL_ARG)) {
        postURL = (String) optionSet.valueOf(URL_ARG);
        failIfOptionSetMissingLoginOrToken(optionSet, "--" + theArg + " and --" + URL_ARG + " flag");
    }

    File file = (File) optionSet.valueOf(theArg);
    WorkspaceFile wsp = new WorkspaceFile();
    wsp.setName(file.getName());
    wsp.setSize(file.length());
    wsp.setDir(file.isDirectory());
    if (optionSet.has(OWNER_ARG)) {
        wsp.setOwner((String) optionSet.valueOf(OWNER_ARG));
    }
    if (optionSet.has(PATH_ARG)) {
        wsp.setPath((String) optionSet.valueOf(PATH_ARG));
    }
    if (optionSet.has(JOB_ID_ARG)) {
        wsp.setSourceJobId((Long) optionSet.valueOf(JOB_ID_ARG));
    }
    if (optionSet.has(MD5_ARG)) {
        wsp.setMd5((String) optionSet.valueOf(MD5_ARG));
    }
    if (optionSet.has(DESCRIPTION_ARG)) {
        wsp.setDescription((String) optionSet.valueOf(DESCRIPTION_ARG));
    }
    if (optionSet.has(TYPE_ARG)) {
        wsp.setType((String) optionSet.valueOf(TYPE_ARG));
    }

    if (optionSet.has(NAME_ARG)) {
        wsp.setName((String) optionSet.valueOf(NAME_ARG));
    }

    if (optionSet.has(SIZE_ARG)) {
        wsp.setSize((Long) optionSet.valueOf(SIZE_ARG));
    }

    if (optionSet.has(WORKSPACE_FILE_FAILED_ARG)) {
        wsp.setFailed((Boolean) optionSet.valueOf(WORKSPACE_FILE_FAILED_ARG));
    }

    ObjectMapper om = new ObjectMapper();
    ObjectWriter ow = om.writerWithDefaultPrettyPrinter();
    if (postURL == null) {
        System.out.println("\n--- JSON Representation of WorkspaceFile ---");
        System.out.println(ow.writeValueAsString(wsp));
        System.out.flush();
        System.out.println("---------------------------------------");
        System.exit(0);
    }
    User u = getUserFromOptionSet(optionSet);
    WorkspaceFileRestDAOImpl workspaceFileDAO = new WorkspaceFileRestDAOImpl();
    workspaceFileDAO.setRestURL(postURL);
    workspaceFileDAO.setUser(u);

    WorkspaceFile workspaceFileRes = workspaceFileDAO.insert(wsp, uploadFile);
    System.out.println("WorkspaceFile id: " + workspaceFileRes.getId());

    if (uploadFile == false) {
        return;
    }

    if (workspaceFileRes.getUploadURL() == null) {
        throw new Exception("No upload url found for workflow!!!" + ow.writeValueAsString(workspaceFileRes));
    }
    uploadWorkspaceFile(workspaceFileRes, file);
}

From source file:com.sparsity.ocl.ast.printer.OclAstJsonPrinter.java

@Override
public void visit(Operation operation, A context) {
    ObjectMapper mapper = new ObjectMapper();
    try {//w w w  .j  av  a 2 s.  c  om
        text = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(operation);
    } catch (Exception e) {

    }
}

From source file:com.groupon.odo.controllers.BackupController.java

/**
 * API call to backup active overreides and active server group for a client
 * Can also backup entire odo configuration
 *
 * @param model/*from   www  .j a v a  2 s  . c o m*/
 * @param response
 * @param profileID Id of profile to backup
 * @param clientUUID Client Id to backup
 * @param odoExport Flag to also include whole odo backup in client backpu
 * @return
 * @throws Exception
 */

@SuppressWarnings("deprecation")
@RequestMapping(value = "/api/backup/profile/{profileID}/{clientUUID}", method = RequestMethod.GET)
public @ResponseBody String getSingleProfileConfiguration(Model model, HttpServletResponse response,
        @PathVariable int profileID, @PathVariable String clientUUID,
        @RequestParam(value = "odoExport", defaultValue = "false") boolean odoExport) throws Exception {
    response.setContentType("application/json");

    ObjectMapper objectMapper = new ObjectMapper();
    ObjectWriter writer = objectMapper.writerWithDefaultPrettyPrinter();

    if (odoExport) {
        response.addHeader("Content-Disposition", "attachment; filename=Config_and_Profile_Backup.json");
        ConfigAndProfileBackup configAndProfileBackup = BackupService.getInstance()
                .getConfigAndProfileData(profileID, clientUUID);
        return writer.withView(ViewFilters.Default.class).writeValueAsString(configAndProfileBackup);
    } else {
        response.addHeader("Content-Disposition", "attachment; filename=Enabled_Endpoints.json");
        SingleProfileBackup singleProfileBackup = BackupService.getInstance().getProfileBackupData(profileID,
                clientUUID);
        return writer.withView(ViewFilters.Default.class).writeValueAsString(singleProfileBackup);
    }
}

From source file:alluxio.rest.TestCase.java

/**
 * Runs the test case.//www  .  j a  va2  s  .com
 */
public void run() throws Exception {
    String expected = "";
    if (mExpectedResult != null) {
        ObjectMapper mapper = new ObjectMapper();
        if (mOptions.isPrettyPrint()) {
            expected = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(mExpectedResult);
        } else {
            expected = mapper.writeValueAsString(mExpectedResult);
        }
    }
    String result = call();
    Assert.assertEquals(mEndpoint, expected, result);
}

From source file:io.syndesis.maven.ExtractConnectorDescriptorsMojo.java

private void saveCamelMetaData(ArrayNode root) throws MojoExecutionException, IOException {
    File targetFile = new File(target);
    if (!targetFile.getParentFile().exists() && !targetFile.getParentFile().mkdirs()) {
        throw new MojoExecutionException("Cannot create directory " + targetFile.getParentFile());
    }// w ww. j ava 2s  .co m
    ObjectMapper mapper = new ObjectMapper();
    mapper.writerWithDefaultPrettyPrinter().writeValue(new File(target), root);
}