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:ch.rasc.extclassgenerator.ModelAnnotationProcessor.java

private static String generateSubclassCode(Class<?> clazz, OutputConfig outputConfig) {
    Model modelAnnotation = clazz.getAnnotation(Model.class);

    String name;//from ww  w.  ja v  a  2 s .  c  o m
    if (modelAnnotation != null && StringUtils.hasText(modelAnnotation.value())) {
        name = modelAnnotation.value();
    } else {
        name = clazz.getName();
    }

    Map<String, Object> modelObject = new LinkedHashMap<String, Object>();
    modelObject.put("extend", name + "Base");

    StringBuilder sb = new StringBuilder(100);
    sb.append("Ext.define(\"").append(name).append("\",");
    if (outputConfig.isDebug()) {
        sb.append("\n");
    }

    String configObjectString;
    try {

        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, false);

        if (!outputConfig.isSurroundApiWithQuotes()) {
            if (outputConfig.getOutputFormat() == OutputFormat.EXTJS5) {
                mapper.addMixInAnnotations(ProxyObject.class, ProxyObjectWithoutApiQuotesExtJs5Mixin.class);
            } else {
                mapper.addMixInAnnotations(ProxyObject.class, ProxyObjectWithoutApiQuotesMixin.class);
            }
            mapper.addMixInAnnotations(ApiObject.class, ApiObjectMixin.class);
        } else {
            if (outputConfig.getOutputFormat() != OutputFormat.EXTJS5) {
                mapper.addMixInAnnotations(ProxyObject.class, ProxyObjectWithApiQuotesMixin.class);
            }
        }

        if (outputConfig.isDebug()) {
            configObjectString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(modelObject);
        } else {
            configObjectString = mapper.writeValueAsString(modelObject);
        }

    } catch (JsonGenerationException e) {
        throw new RuntimeException(e);
    } catch (JsonMappingException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    sb.append(configObjectString);
    sb.append(");");

    if (outputConfig.isUseSingleQuotes()) {
        return sb.toString().replace('"', '\'');
    }

    return sb.toString();

}

From source file:org.venice.piazza.serviceregistry.controller.messaging.handlers.RegistryServiceHandlerTest.java

@Before
public void setup() {
    uuidFactory = mock(UUIDFactory.class);
    when(uuidFactory.getUUID()).thenReturn("NoDoz");
    template = mock(RestTemplate.class);
    try {/*from w ww. j  a  v  a 2s  .com*/
        whenNew(RestTemplate.class).withNoArguments().thenReturn(template);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    rm = new ResourceMetadata();
    rm.name = "toUpper Params";
    rm.description = "Service to convert string to uppercase";
    service = new Service();
    service.setMethod("POST");
    service.setResourceMetadata(rm);
    service.setServiceId("8");
    DataType dataType1 = new URLParameterDataType();

    //DEBUGGING
    ObjectMapper mapper = new ObjectMapper();
    try {
        String jsonReq = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(service);
        System.out.println(jsonReq);
    } catch (JsonProcessingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

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

License:asdf

public static void getWorkflowAsJson(OptionSet optionSet) throws Exception {
    WorkflowRestDAOImpl workflowDAO = new WorkflowRestDAOImpl();
    workflowDAO.setRestURL((String) optionSet.valueOf(URL_ARG));
    User u = getUserFromOptionSet(optionSet);
    workflowDAO.setUser(u);//ww w. j  a  va 2  s  .  c  o  m

    Long wfid = (Long) optionSet.valueOf(GET_WORKFLOW_ARG);
    Workflow w = workflowDAO.getWorkflowById(wfid.toString(), u);
    if (w != null) {
        ObjectMapper om = new ObjectMapper();
        ObjectWriter ow = om.writerWithDefaultPrettyPrinter();
        System.out.println(ow.writeValueAsString(w));
    } else {
        System.err.println("Unable to retreive WorkspaceFile with id: " + wfid.toString());
        System.exit(1);
    }
}

From source file:org.bremersee.comparator.test.ComparatorItemTests.java

@Test
public void testJsonComparatorItem() throws Exception {

    System.out.println("Testing JSON write-read operations ...");

    ComparatorItem item = new ComparatorItem("i0", true);
    item.next("i1", false).next("i2", true);

    ObjectMapper om = new ObjectMapper();

    String jsonStr = om.writerWithDefaultPrettyPrinter().writeValueAsString(item);

    System.out.println(jsonStr);/* ww w . j av  a2s  .c  o m*/

    ComparatorItem readItem = om.readValue(jsonStr, ComparatorItem.class);

    System.out.println(item.toList(true));
    System.out.println(readItem.toList(true));

    TestCase.assertEquals(item.toList(true), readItem.toList(true));

    System.out.println("OK\n");
}

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

License:asdf

public static void getJobAsJson(OptionSet optionSet) throws Exception {
    JobRestDAOImpl jobDAO = new JobRestDAOImpl();
    jobDAO.setRestURL((String) optionSet.valueOf(URL_ARG));
    User u = getUserFromOptionSet(optionSet);
    jobDAO.setUser(u);//from w w  w  .  j av  a 2 s . com

    Job j = jobDAO.getJobById(((Long) optionSet.valueOf(GET_JOB_ARG)).toString());
    if (j != null) {
        ObjectMapper om = new ObjectMapper();
        ObjectWriter ow = om.writerWithDefaultPrettyPrinter();
        System.out.println(ow.writeValueAsString(j));
    } else {
        System.err.println(
                "Unable to retreive job with id: " + ((Long) optionSet.valueOf(GET_JOB_ARG)).toString());
        System.exit(1);
    }
    return;

}

From source file:com.github.yongchristophertang.engine.web.response.PrintResultHandler.java

/**
 * Reformat Json string for better look in printing
 *
 * @param rawJson raw json string// w  w w.  java  2 s .c om
 * @return formatted json string
 */
private String getPrettyJsonPrint(String rawJson) {
    ObjectMapper mapper = new ObjectMapper();
    try {
        Object json = mapper.readValue(rawJson, Object.class);
        return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json);
    } catch (Exception e) {
        return rawJson;
    }
}

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

License:asdf

public static void getWorkspaceFileAsJson(OptionSet optionSet) throws Exception {
    WorkspaceFileRestDAOImpl workspaceFileDAO = new WorkspaceFileRestDAOImpl();
    workspaceFileDAO.setRestURL((String) optionSet.valueOf(URL_ARG));
    User u = getUserFromOptionSet(optionSet);
    workspaceFileDAO.setUser(u);//  www.  j ava  2s.c  o m
    Long workspaceFileId = (Long) optionSet.valueOf(GET_WORKSPACE_FILE_ARG);

    if (workspaceFileId == -1) {
        List<WorkspaceFile> wsfList = workspaceFileDAO.getWorkspaceFiles(null, null, null, null, null);
        ObjectMapper om = new ObjectMapper();
        ObjectWriter ow = om.writerWithDefaultPrettyPrinter();
        if (wsfList != null) {
            for (WorkspaceFile wsf : wsfList) {
                System.out.println(ow.writeValueAsString(wsf));
            }
        }
        return;
    }
    WorkspaceFile wsf = workspaceFileDAO.getWorkspaceFileById(workspaceFileId.toString(), u);
    if (wsf != null) {
        ObjectMapper om = new ObjectMapper();
        ObjectWriter ow = om.writerWithDefaultPrettyPrinter();
        System.out.println(ow.writeValueAsString(wsf));
    } else {
        System.err.println("Unable to retreive WorkspaceFile with id: "
                + ((Long) optionSet.valueOf(GET_WORKSPACE_FILE_ARG)).toString());
        System.exit(1);
    }
    return;

}

From source file:com.auditbucket.engine.service.EngineConfig.java

private void doHealth() {
    ObjectMapper om = new ObjectMapper();
    try {//from   w  ww  . ja va  2  s  .c o  m
        ObjectWriter or = om.writerWithDefaultPrettyPrinter();
        logger.info("\r\n" + or.writeValueAsString(getHealth()));

    } catch (JsonProcessingException e) {

        logger.error("doHealth", e);
    }
}

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

License:asdf

/**
 * Prints out examples of {@link Workflow}, {@link WorkspaceFile}, {@link Job}, {@link WorkspaceFile}
 * objects in pretty JSON format//  w ww .j a  va  2  s  . co m
 * @throws Exception 
 */
public static void renderExampleWorkflowsAndTasksAsJson() throws Exception {

    ObjectMapper om = new ObjectMapper();
    ObjectWriter ow = om.writerWithDefaultPrettyPrinter();

    System.out.println("Json for Empty Workflow");
    System.out.println("-----------------------");
    System.out.println(ow.writeValueAsString(getWorkflowWithNoParameters()));
    System.out.flush();
    System.out.println("-----------------------\n\n");

    System.out.println("Json for Workflow with Parameters");
    System.out.println("-----------------------");

    System.out.println(ow.writeValueAsString(getWorkflowWithParameters()));
    System.out.flush();
    System.out.println("-----------------------\n\n");

    System.out.println("Json for Job with 2 parameters and workflow");
    System.out.println("-----------------------");
    System.out.println(ow.writeValueAsString(getJobWithParametersAndWorkflow()));
    System.out.flush();
    System.out.println("-----------------------\n\n");

    System.out.println("Json for WorkspaceFile");
    System.out.println("-----------------------");
    System.out.println(ow.writeValueAsString(getWorkspaceFile()));
    System.out.flush();

    System.out.println("Json for User");
    System.out.println("-----------------------");
    System.out.println(ow.writeValueAsString(getUser()));
    System.out.flush();

    System.out.println("Json for Event");
    System.out.println("-----------------------");
    System.out.println(ow.writeValueAsString(getEvent()));
    System.out.flush();

}

From source file:org.bremersee.sms.test.ModelTests.java

@Test
public void testJsonSmsSendRequestDto() throws Exception {

    System.out.println("Testing JSON SmsSendRequestDto ...");

    SmsSendRequestDto request = new SmsSendRequestDto("bremersee", "0123456789", "Hello",
            new Date(System.currentTimeMillis() + 30000L));

    ObjectMapper om = new ObjectMapper();

    String jsonStr = om.writerWithDefaultPrettyPrinter().writeValueAsString(request);

    System.out.println(jsonStr);/*from  w w w .j a  va  2  s .  c o  m*/

    SmsSendRequestDto readRequest = om.readValue(jsonStr, SmsSendRequestDto.class);

    System.out.println(readRequest);

    String newJsonStr = om.writerWithDefaultPrettyPrinter().writeValueAsString(readRequest);
    System.out.println(newJsonStr);

    TestCase.assertEquals(jsonStr, newJsonStr);

    System.out.println("OK\n");
}