use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator in project template-compiler by Squarespace.
the class GeneralUtils method jsonPretty.
/**
* Formats the {@code node} as a string using the pretty printer.
*/
public static String jsonPretty(JsonNode node) throws IOException {
StringBuilder buf = new StringBuilder();
JsonGenerator gen = JSON_FACTORY.createGenerator(new StringBuilderWriter(buf));
gen.useDefaultPrettyPrinter();
gen.setCodec(JsonUtils.getMapper());
gen.writeTree(node);
return buf.toString();
}
use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator in project Java-Tutorial by gpcodervn.
the class JacksonStreamingWriterExample method main.
public static void main(String[] args) throws IOException {
File file = new File("data/result.json");
JsonFactory factory = new JsonFactory();
/**
* Write values in JSON format to a file
*/
JsonGenerator generator = factory.createGenerator(file, JsonEncoding.UTF8);
// {
generator.writeStartObject();
// "name" : "gpcoder"
generator.writeStringField("name", "GP Coder");
// "website" : "https://gpcoder.com"
generator.writeStringField("website", "https://gpcoder.com");
// "year" : 2017
generator.writeNumberField("year", 2017);
// "colors" :
generator.writeFieldName("posts");
// [
generator.writeStartArray();
// "Java Core"
generator.writeString("Java Core");
// "Design Pattern"
generator.writeString("Design Pattern");
// "Spring"
generator.writeString("Spring");
// ]
generator.writeEndArray();
// }
generator.writeEndObject();
generator.close();
System.out.println("Done!");
}
use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator in project data-prep by Talend.
the class CommonAPI method listErrors.
/**
* Describe the supported error codes.
*
* @param output the http response.
*/
@RequestMapping(value = "/api/errors", method = RequestMethod.GET, produces = APPLICATION_JSON_VALUE)
@ApiOperation(value = "Get all supported errors.", notes = "Returns the list of all supported errors.")
@Timed
public void listErrors(final OutputStream output) throws IOException {
LOG.debug("Listing supported error codes");
JsonFactory factory = new JsonFactory();
JsonGenerator generator = factory.createGenerator(output);
generator.setCodec(mapper);
// start the errors array
generator.writeStartArray();
// write the direct known errors
writeErrorsFromEnum(generator, CommonErrorCodes.values());
writeErrorsFromEnum(generator, APIErrorCodes.values());
// get dataset api errors
HystrixCommand<InputStream> datasetErrors = getCommand(ErrorList.class, GenericCommand.DATASET_GROUP, DATASET);
try (InputStream errorsInput = datasetErrors.execute()) {
writeErrorsFromApi(generator, errorsInput);
}
// get preparation api errors
HystrixCommand<InputStream> preparationErrors = getCommand(ErrorList.class, GenericCommand.PREPARATION_GROUP, PREPARATION);
try (InputStream errorsInput = preparationErrors.execute()) {
writeErrorsFromApi(generator, errorsInput);
}
// get transformation api errors
HystrixCommand<InputStream> transformationErrors = getCommand(ErrorList.class, GenericCommand.TRANSFORM_GROUP, TRANSFORMATION);
try (InputStream errorsInput = transformationErrors.execute()) {
writeErrorsFromApi(generator, errorsInput);
}
// close the errors array
generator.writeEndArray();
generator.flush();
}
use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator in project data-prep by Talend.
the class SearchAPI method doSearch.
private void doSearch(String name, List<String> filter, boolean strict, OutputStream output) {
if (LOG.isDebugEnabled()) {
LOG.debug("Searching dataprep for '{}' (pool: {})...", name, getConnectionStats());
}
try (final JsonGenerator generator = mapper.getFactory().createGenerator(output)) {
generator.writeStartObject();
// Write category information
generator.writeFieldName("categories");
generator.writeStartArray();
// Add static information about documentation category
generator.writeStartObject();
generator.writeStringField("type", "documentation");
generator.writeStringField("label", messagesBundle.getString(LocaleContextHolder.getLocale(), "search.documentation"));
generator.writeEndObject();
// Now the search types categories
searchDelegates.forEach(searchDelegate -> {
final String categoryLabel = messagesBundle.getString(LocaleContextHolder.getLocale(), "search." + searchDelegate.getSearchLabel());
try {
generator.writeStartObject();
generator.writeStringField("type", searchDelegate.getInventoryType());
generator.writeStringField("label", categoryLabel);
generator.writeEndObject();
} catch (IOException e) {
LOG.error("Unable to write category information for '{}'.", searchDelegate.getSearchCategory(), e);
}
});
generator.writeEndArray();
// Write results
searchDelegates.forEach(searchDelegate -> {
final String category = searchDelegate.getSearchCategory();
if (filter == null || filter.contains(category)) {
try {
generator.writeObjectField(category, searchDelegate.search(name, strict));
} catch (IOException e) {
LOG.error("Unable to search '{}'.", category, e);
}
}
});
generator.writeEndObject();
} catch (IOException e) {
throw new TDPException(UNABLE_TO_SEARCH_DATAPREP, e);
}
LOG.debug("Search done on for '{}' with filter '{}' (strict mode: {})", name, filter, strict);
}
use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator in project data-prep by Talend.
the class UpgradeAPI method fetchServerUpgradeVersions.
private List<UpgradeServerVersion> fetchServerUpgradeVersions(org.talend.dataprep.info.Version version) throws IOException {
final HttpPost post = new HttpPost(upgradeVersionLocation);
final String response;
final StringWriter content = new StringWriter();
try (final JsonGenerator generator = mapper.getFactory().createGenerator(content)) {
generator.writeStartObject();
generator.writeStringField("version", version.getVersionId());
generator.writeStringField("id", token);
generator.writeEndObject();
generator.flush();
post.setEntity(new StringEntity(content.toString(), ContentType.APPLICATION_JSON.withCharset(UTF_8)));
response = IOUtils.toString(httpClient.execute(post).getEntity().getContent(), UTF_8);
} finally {
post.releaseConnection();
}
// Read upgrade server response
return mapper.readerFor(new TypeReference<List<UpgradeServerVersion>>() {
}).readValue(response);
}
Aggregations