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.lambdamatic.internal.elasticsearch.clientdsl.Client.java

private static String formatJsonDocument(final String requestBody)
        throws IOException, JsonParseException, JsonMappingException, JsonProcessingException {
    final ObjectMapper mapper = new ObjectMapper();
    final Object json = mapper.readValue(requestBody, Object.class);
    final String indented = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json);
    return indented;
}

From source file:org.lambdamatic.internal.elasticsearch.clientdsl.Client.java

private static String formatJsonDocument(final InputStream requestBodyStream)
        throws IOException, JsonParseException, JsonMappingException, JsonProcessingException {
    final ObjectMapper mapper = new ObjectMapper();
    final Object json = mapper.readValue(requestBodyStream, Object.class);
    final String indented = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json);
    return indented;
}

From source file:com.redhat.lightblue.util.JsonUtils.java

/**
 * Validates input node against given schema. Returns NULL if no errors
 * reported, else returns string representing violations.
 *
 * @param schema the json schema (see #loadSchema)
 * @param node the json node to validate
 * @return null if there are no errors, else string with all errors and
 * warnings//from  w  ww. j a va 2 s.  co  m
 * @throws ProcessingException
 */
public static String jsonSchemaValidation(JsonSchema schema, JsonNode node)
        throws ProcessingException, JsonProcessingException {
    ProcessingReport report = schema.validate(node);
    Iterator<ProcessingMessage> i = report.iterator();
    StringBuilder buff = new StringBuilder();
    while (!report.isSuccess() && i != null && i.hasNext()) {
        ProcessingMessage pm = i.next();

        // attempting to pretty print the json
        ObjectMapper mapper = new ObjectMapper();
        String prettyPrintJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(pm.asJson());

        buff.append(prettyPrintJson).append("\n\n");
    }

    return report.isSuccess() ? null : buff.toString();
}

From source file:org.fastj.fit.tool.JSONHelper.java

public static String jsonString(Object o) {
    ObjectMapper mapper = new ObjectMapper();
    try {/*from  w  ww. ja va 2s .  co  m*/
        return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(o);
    } catch (Throwable e) {
        LogUtil.error("GetJSON fail: e={}", e.getMessage());
    }
    return null;
}

From source file:it.polimi.diceH2020.plugin.control.FileManager.java

/**
 * Generates the JSON file used to compose the result
 *//* w  ww  .ja  v  a 2  s .co m*/
public static void generateOutputJson() {
    Map<Integer, String> map = new HashMap<Integer, String>();

    for (ClassDesc cd : Configuration.getCurrent().getClasses()) {
        map.put(cd.getId(), cd.getDdsmPath());
    }

    JSONObject json = new JSONObject(map);
    ObjectMapper mapper = new ObjectMapper();

    try {
        mapper.writerWithDefaultPrettyPrinter().writeValue(
                new File(Preferences.getSavingDir() + Configuration.getCurrent().getID() + "OUT.json"), json);
    } catch (JsonProcessingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.phoenixnap.oss.ramlapisync.naming.SchemaHelper.java

/**
 * Uses Jackson object mappers to convert an ajaxcommandparameter annotated type into its JSONSchema representation.
 * If Javadoc is supplied, this will be injected as comments
 * //from   w  w w .  j  a v  a2 s  .c  o  m
 * @param clazz The Class to convert
 * @param responseDescription The javadoc description supplied if available
 * @param javaDocStore The Entire java doc store available
 * @return A string containing the Json Schema
 */
public static String convertClassToJsonSchema(ApiParameterMetadata clazz, String responseDescription,
        JavaDocStore javaDocStore) {
    if (clazz == null || clazz.equals(Void.class)) {
        return "{}";
    }
    try {
        ObjectMapper m = new ObjectMapper();
        JsonSchema jsonSchema = extractSchemaInternal(clazz.getType(), clazz.getGenericType(),
                responseDescription, javaDocStore, m);

        return m.writerWithDefaultPrettyPrinter().writeValueAsString(jsonSchema);
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
}

From source file:com.phoenixnap.oss.ramlapisync.naming.SchemaHelper.java

/**
 * Uses Jackson object mappers to convert a Pojo into its JSONSchema representation. If Javadoc is supplied, this
 * will be injected as comments//from  ww  w.ja  v  a  2  s . c o m
 * 
 * @param clazz The Class to be inspected
 * @param responseDescription The description to be embedded in the response
 * @param javaDocStore Associated JavaDoc for this class that can be embedded in the schema
 * @return Json Schema representing the class in string format
 */
public static String convertClassToJsonSchema(Type clazz, String responseDescription,
        JavaDocStore javaDocStore) {
    if (clazz == null || clazz.equals(Void.class)) {
        return "{}";
    }
    try {
        ObjectMapper m = new ObjectMapper();
        JsonSchema jsonSchema = extractSchemaInternal(clazz, TypeHelper.inferGenericType(clazz),
                responseDescription, javaDocStore, m);

        return m.writerWithDefaultPrettyPrinter().writeValueAsString(jsonSchema);
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
}

From source file:it.polimi.diceH2020.plugin.control.FileManager.java

/**
 * Builds the JSON representation of the current Configuration and
 * eventually dumps it on a file.//from  w w w .  j  av  a 2  s.  c  o  m
 */
public static void generateInputJson() {
    Configuration conf = Configuration.getCurrent(); // TODO: REMOVE
    InstanceDataMultiProvider data = InstanceDataMultiProviderGenerator.build();

    data.setId(conf.getID());

    setMapJobProfile(data, conf);
    setMapClassParameters(data, conf);

    if (conf.getIsPrivate()) {

        data.setMapPublicCloudParameters(null);
        setPrivateParameters(data);

    } else {
        // Set MapVMConfigurations
        data.setMapVMConfigurations(null);
        data.setPrivateCloudParameters(null);

        if (conf.getHasLtc()) {
            setEtaR(data, conf);
        } else {
            data.setMapPublicCloudParameters(null);
        }
    }

    setMachineLearningProfile(data, conf);

    if (!Configuration.getCurrent().canSend()) {
        return;
    }

    // Generate Json
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new Jdk8Module());

    try {
        mapper.writerWithDefaultPrettyPrinter()
                .writeValue(new File(Preferences.getSavingDir() + conf.getID() + ".json"), data);
    } catch (JsonProcessingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:org.forgerock.openidm.tools.scriptedbundler.ScriptedBundler.java

private static int bundleConnector(String filename, Boolean verbose) {
    File file = new File(filename);
    if (!file.exists() || !file.canRead()) {
        System.err.println("Unable to read from " + filename + ". Please check that the "
                + "file exists and is readable.");
        return FAILURE;
    }/*  www .  j av  a 2 s.co m*/

    ObjectMapper mapper = new ObjectMapper();
    CustomConfiguration config = null;
    try {
        config = mapper.readValue(file, CustomConfiguration.class);
    } catch (Exception e) {
        System.err.println("Unable to parse the configuration file");
        e.printStackTrace(System.err);
        return FAILURE;
    }

    System.out.println("Generating connector sources for " + config.getDisplayName());

    try {
        if (verbose) {
            System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(config));
        }

        SourceGenerator.generateSources(config);
    } catch (IOException e) {
        System.err.println(e.getMessage());
        e.printStackTrace(System.err);
        return FAILURE;
    }

    return SUCCESS;
}

From source file:edu.usu.sdl.openstorefront.common.util.StringProcessor.java

/**
 * Remove all json fields not in the list to keep.
 *
 * @param json/*  ww w . j  a  v  a 2  s. com*/
 * @param fieldsToKeep
 * @return
 */
public static String stripeFieldJSON(String json, Set<String> fieldsToKeep) {
    ObjectMapper mapper = defaultObjectMapper();

    try {
        JsonNode rootNode = mapper.readTree(json);
        processNode(rootNode, fieldsToKeep);

        Object jsonString = mapper.readValue(rootNode.toString(), Object.class);
        return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonString);
    } catch (IOException ex) {
        throw new OpenStorefrontRuntimeException(ex);
    }
}