Search in sources :

Example 71 with ObjectWriter

use of com.fasterxml.jackson.databind.ObjectWriter in project airlift by airlift.

the class JsonMapper method writeTo.

@Override
public void writeTo(Object value, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream outputStream) throws IOException {
    // Prevent broken browser from attempting to render the json as html
    httpHeaders.add(HttpHeaders.X_CONTENT_TYPE_OPTIONS, "nosniff");
    JsonFactory jsonFactory = objectMapper.getFactory();
    jsonFactory.setCharacterEscapes(HTMLCharacterEscapes.INSTANCE);
    JsonGenerator jsonGenerator = jsonFactory.createGenerator(outputStream, JsonEncoding.UTF8);
    // Important: we are NOT to close the underlying stream after
    // mapping, so we need to instruct generator:
    jsonGenerator.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
    // Pretty print?
    if (isPrettyPrintRequested()) {
        jsonGenerator.useDefaultPrettyPrinter();
    }
    // 04-Mar-2010, tatu: How about type we were given? (if any)
    JavaType rootType = null;
    if (genericType != null && value != null) {
        // generic.
        if (genericType.getClass() != Class.class) {
            // generic types are other implementations of 'java.lang.reflect.Type'
            // This is still not exactly right; should root type be further
            // specialized with 'value.getClass()'? Let's see how well this works before
            // trying to come up with more complete solution.
            rootType = objectMapper.getTypeFactory().constructType(genericType);
            // 
            if (rootType.getRawClass() == Object.class) {
                rootType = null;
            }
        }
    }
    String jsonpFunctionName = getJsonpFunctionName();
    if (jsonpFunctionName != null) {
        value = new JSONPObject(jsonpFunctionName, value, rootType);
        rootType = null;
    }
    ObjectWriter writer;
    if (rootType != null) {
        writer = objectMapper.writerFor(rootType);
    } else {
        writer = objectMapper.writer();
    }
    try {
        writer.writeValue(jsonGenerator, value);
        // add a newline so when you use curl it looks nice
        outputStream.write('\n');
    } catch (EOFException e) {
    // ignore EOFException
    // This happens when the client terminates the connection when data
    // is being written.  If the exception is allowed to propagate to
    // Jersey, the exception will be logged, but this error is not
    // important.  This is safe since the output stream is already
    // closed.
    }
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) JsonFactory(com.fasterxml.jackson.core.JsonFactory) EOFException(java.io.EOFException) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) JSONPObject(com.fasterxml.jackson.databind.util.JSONPObject) SerializableString(com.fasterxml.jackson.core.SerializableString)

Example 72 with ObjectWriter

use of com.fasterxml.jackson.databind.ObjectWriter in project kylo by Teradata.

the class FeedModelTransform method generateDifference.

/**
 * @param fromVer
 * @param toVer
 */
public EntityVersionDifference generateDifference(com.thinkbiganalytics.feedmgr.rest.model.EntityVersion fromVer, com.thinkbiganalytics.feedmgr.rest.model.EntityVersion toVer) {
    try {
        ObjectMapper om = new ObjectMapper();
        ObjectWriter ow = om.writer();
        ObjectReader or = om.reader();
        String fromEntStr = ow.writeValueAsString(fromVer.getEntity());
        String toEntStr = ow.writeValueAsString(toVer.getEntity());
        JsonNode fromNode = or.readTree(fromEntStr);
        JsonNode toNode = or.readTree(toEntStr);
        // Produce a patch showing the changes from the "to" node back into the "from" node.
        // This is because we will be providing the "to" entity content so the patch should show the original "from" values.
        JsonNode diff = JsonDiff.asJson(toNode, fromNode);
        com.thinkbiganalytics.feedmgr.rest.model.EntityVersion fromNoContent = new com.thinkbiganalytics.feedmgr.rest.model.EntityVersion(fromVer.getId(), fromVer.getName(), fromVer.getCreatedDate());
        return new EntityVersionDifference(fromNoContent, toVer, diff);
    } catch (IOException e) {
        throw new ModelTransformException("Failed to generate entity difference between entity versions " + fromVer.getId() + " and " + toVer.getId());
    }
}
Also used : ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) EntityVersion(com.thinkbiganalytics.metadata.api.versioning.EntityVersion) EntityVersionDifference(com.thinkbiganalytics.feedmgr.rest.model.EntityVersionDifference) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 73 with ObjectWriter

use of com.fasterxml.jackson.databind.ObjectWriter in project CzechIdMng by bcvsolutions.

the class NotificationRestTest method jsonify.

String jsonify(IdmNotificationDto dto) throws IOException {
    ObjectMapper m = new ObjectMapper();
    StringWriter sw = new StringWriter();
    ObjectWriter writer = m.writerFor(IdmNotificationDto.class);
    writer.writeValue(sw, dto);
    return sw.toString();
}
Also used : StringWriter(java.io.StringWriter) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 74 with ObjectWriter

use of com.fasterxml.jackson.databind.ObjectWriter in project syndesis-qe by syndesisio.

the class AbstractEndpoint method list.

public List<T> list(String id) {
    final ObjectMapper mapper = new ObjectMapper().registerModules(new Jdk8Module());
    mapper.configure(Feature.AUTO_CLOSE_SOURCE, true);
    final ObjectWriter ow = mapper.writer();
    final Class<ListResult<T>> listtype = (Class) ListResult.class;
    log.debug("GET : {}", getEndpointUrl(Optional.ofNullable(id)));
    final Invocation.Builder invocation = this.createInvocation(id);
    final JsonNode response = invocation.get(JsonNode.class);
    ListResult<T> result = null;
    try {
        result = Json.reader().forType(listtype).readValue(response.toString());
    } catch (IOException ex) {
        log.error("" + ex);
    }
    final List<T> ts = new ArrayList<>();
    for (int i = 0; i < result.getTotalCount(); i++) {
        T con = null;
        try {
            final String json = ow.writeValueAsString(result.getItems().get(i));
            con = Json.reader().forType(type).readValue(json);
        } catch (IOException ex) {
            log.error(ex.toString());
        }
        ts.add(con);
    }
    return ts;
}
Also used : Invocation(javax.ws.rs.client.Invocation) ArrayList(java.util.ArrayList) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) ListResult(io.syndesis.common.model.ListResult) Jdk8Module(com.fasterxml.jackson.datatype.jdk8.Jdk8Module) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 75 with ObjectWriter

use of com.fasterxml.jackson.databind.ObjectWriter in project CzechIdMng by bcvsolutions.

the class DefaultRecaptchaRestTest method jsonify.

private String jsonify(RecaptchaRequest dto) throws IOException {
    ObjectMapper m = new ObjectMapper();
    StringWriter sw = new StringWriter();
    ObjectWriter writer = m.writerFor(RecaptchaRequest.class);
    writer.writeValue(sw, dto);
    return sw.toString();
}
Also used : StringWriter(java.io.StringWriter) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)124 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)53 IOException (java.io.IOException)28 Test (org.junit.Test)27 File (java.io.File)13 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)12 JavaType (com.fasterxml.jackson.databind.JavaType)10 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)10 ArrayList (java.util.ArrayList)10 FileOutputStream (java.io.FileOutputStream)7 Map (java.util.Map)7 JCommander (com.beust.jcommander.JCommander)6 ParameterException (com.beust.jcommander.ParameterException)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 RateLimiter (com.google.common.util.concurrent.RateLimiter)6 FileInputStream (java.io.FileInputStream)6 OutputStream (java.io.OutputStream)6 Writer (java.io.Writer)6 HashMap (java.util.HashMap)6 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)5