Search in sources :

Example 56 with JsonGenerator

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();
}
Also used : StringBuilderWriter(org.apache.commons.io.output.StringBuilderWriter) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator)

Example 57 with JsonGenerator

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!");
}
Also used : JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) File(java.io.File)

Example 58 with JsonGenerator

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();
}
Also used : InputStream(java.io.InputStream) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) Timed(org.talend.dataprep.metrics.Timed) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 59 with JsonGenerator

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);
}
Also used : TDPException(org.talend.dataprep.exception.TDPException) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) IOException(java.io.IOException)

Example 60 with JsonGenerator

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);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) StringWriter(java.io.StringWriter) UpgradeServerVersion(org.talend.dataprep.api.service.upgrade.UpgradeServerVersion) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) TypeReference(com.fasterxml.jackson.core.type.TypeReference)

Aggregations

JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)704 KriptonJsonContext (com.abubusoft.kripton.KriptonJsonContext)257 KriptonByteArrayOutputStream (com.abubusoft.kripton.common.KriptonByteArrayOutputStream)257 KriptonRuntimeException (com.abubusoft.kripton.exception.KriptonRuntimeException)257 JacksonWrapperSerializer (com.abubusoft.kripton.persistence.JacksonWrapperSerializer)257 IOException (java.io.IOException)169 StringWriter (java.io.StringWriter)144 JsonFactory (com.fasterxml.jackson.core.JsonFactory)101 ByteArrayOutputStream (java.io.ByteArrayOutputStream)66 Map (java.util.Map)57 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)54 HashMap (java.util.HashMap)40 Test (org.junit.Test)30 File (java.io.File)27 ArrayList (java.util.ArrayList)25 OutputStream (java.io.OutputStream)24 Writer (java.io.Writer)22 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)21 SerializerProvider (com.fasterxml.jackson.databind.SerializerProvider)20 FileOutputStream (java.io.FileOutputStream)19