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.
}
}
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());
}
}
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();
}
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;
}
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();
}
Aggregations