Search in sources :

Example 1 with PrettyPrinter

use of com.fasterxml.jackson.core.PrettyPrinter in project jackson-core by FasterXML.

the class TestJDKSerializability method testPrettyPrinter.

public void testPrettyPrinter() throws Exception {
    PrettyPrinter p = new DefaultPrettyPrinter();
    byte[] stuff = jdkSerialize(p);
    PrettyPrinter back = jdkDeserialize(stuff);
    // what should we test?
    assertNotNull(back);
}
Also used : DefaultPrettyPrinter(com.fasterxml.jackson.core.util.DefaultPrettyPrinter) DefaultPrettyPrinter(com.fasterxml.jackson.core.util.DefaultPrettyPrinter) PrettyPrinter(com.fasterxml.jackson.core.PrettyPrinter)

Example 2 with PrettyPrinter

use of com.fasterxml.jackson.core.PrettyPrinter in project asterixdb by apache.

the class ResultExtractor method extract.

public static InputStream extract(InputStream resultStream) throws Exception {
    ObjectMapper om = new ObjectMapper();
    String resultStr = IOUtils.toString(resultStream, Charset.defaultCharset());
    PrettyPrinter singleLine = new SingleLinePrettyPrinter();
    ObjectNode result = om.readValue(resultStr, ObjectNode.class);
    LOGGER.fine("+++++++\n" + result + "\n+++++++\n");
    String type = "";
    String status = "";
    String results = "";
    String field = "";
    for (Iterator<String> sIter = result.fieldNames(); sIter.hasNext(); ) {
        field = sIter.next();
        switch(field) {
            case "requestID":
                break;
            case "clientContextID":
                break;
            case "signature":
                break;
            case "status":
                status = om.writeValueAsString(result.get(field));
                break;
            case "type":
                type = om.writeValueAsString(result.get(field));
                break;
            case "metrics":
                LOGGER.fine(om.writeValueAsString(result.get(field)));
                break;
            case "errors":
                JsonNode errors = result.get(field).get(0).get("msg");
                throw new AsterixException(errors.asText());
            case "results":
                if (result.get(field).size() <= 1) {
                    if (result.get(field).size() == 0) {
                        results = "";
                    } else if (result.get(field).isArray()) {
                        if (result.get(field).get(0).isTextual()) {
                            results = result.get(field).get(0).asText();
                        } else {
                            ObjectMapper omm = new ObjectMapper();
                            omm.setDefaultPrettyPrinter(singleLine);
                            omm.enable(SerializationFeature.INDENT_OUTPUT);
                            results = omm.writer(singleLine).writeValueAsString(result.get(field));
                        }
                    } else {
                        results = om.writeValueAsString(result.get(field));
                    }
                } else {
                    StringBuilder sb = new StringBuilder();
                    JsonNode[] fields = Iterators.toArray(result.get(field).elements(), JsonNode.class);
                    if (fields.length > 1) {
                        for (JsonNode f : fields) {
                            if (f.isObject()) {
                                sb.append(om.writeValueAsString(f));
                            } else {
                                sb.append(f.asText());
                            }
                        }
                    }
                    results = sb.toString();
                }
                break;
            default:
                throw new AsterixException("Unanticipated field \"" + field + "\"");
        }
    }
    return IOUtils.toInputStream(results);
}
Also used : PrettyPrinter(com.fasterxml.jackson.core.PrettyPrinter) AsterixException(org.apache.asterix.common.exceptions.AsterixException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

PrettyPrinter (com.fasterxml.jackson.core.PrettyPrinter)2 DefaultPrettyPrinter (com.fasterxml.jackson.core.util.DefaultPrettyPrinter)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 AsterixException (org.apache.asterix.common.exceptions.AsterixException)1