Search in sources :

Example 6 with MinimalPrettyPrinter

use of com.fasterxml.jackson.core.util.MinimalPrettyPrinter in project drill by apache.

the class JsonRecordWriter method init.

@Override
public void init(Map<String, String> writerOptions) throws IOException {
    this.location = writerOptions.get("location");
    this.prefix = writerOptions.get("prefix");
    this.fieldDelimiter = writerOptions.get("separator");
    this.extension = writerOptions.get("extension");
    this.useExtendedOutput = Boolean.parseBoolean(writerOptions.get("extended"));
    this.skipNullFields = Boolean.parseBoolean(writerOptions.get("skipnulls"));
    final boolean uglify = Boolean.parseBoolean(writerOptions.get("uglify"));
    Configuration conf = new Configuration();
    conf.set(FileSystem.FS_DEFAULT_NAME_KEY, writerOptions.get(FileSystem.FS_DEFAULT_NAME_KEY));
    this.fs = FileSystem.get(conf);
    Path fileName = new Path(location, prefix + "_" + index + "." + extension);
    try {
        // json writer does not support partitions, so only one file can be created
        // and thus only one location should be deleted in case of abort
        // to ensure that our writer was the first to create output file,
        // we create empty output file first and fail if file exists
        cleanUpLocation = storageStrategy.createFileAndApply(fs, fileName);
        // since empty output file will be overwritten (some file systems may restrict append option)
        // we need to re-apply file permission
        stream = fs.create(fileName);
        storageStrategy.applyToFile(fs, fileName);
        JsonGenerator generator = factory.createGenerator(stream).useDefaultPrettyPrinter();
        if (uglify) {
            generator = generator.setPrettyPrinter(new MinimalPrettyPrinter(LINE_FEED));
        }
        if (useExtendedOutput) {
            gen = new ExtendedJsonOutput(generator);
        } else {
            gen = new BasicJsonOutput(generator);
        }
        logger.debug("Created file: {}", fileName);
    } catch (IOException ex) {
        logger.error("Unable to create file: " + fileName, ex);
        throw ex;
    }
}
Also used : Path(org.apache.hadoop.fs.Path) MinimalPrettyPrinter(com.fasterxml.jackson.core.util.MinimalPrettyPrinter) ExtendedJsonOutput(org.apache.drill.exec.vector.complex.fn.ExtendedJsonOutput) Configuration(org.apache.hadoop.conf.Configuration) BasicJsonOutput(org.apache.drill.exec.vector.complex.fn.BasicJsonOutput) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) IOException(java.io.IOException)

Example 7 with MinimalPrettyPrinter

use of com.fasterxml.jackson.core.util.MinimalPrettyPrinter in project cas by apereo.

the class ReturnRestfulAttributeReleasePolicy method getAttributesInternal.

@Override
public Map<String, Object> getAttributesInternal(final Principal principal, final Map<String, Object> attributes, final RegisteredService service) {
    try (StringWriter writer = new StringWriter()) {
        MAPPER.writer(new MinimalPrettyPrinter()).writeValue(writer, attributes);
        final HttpResponse response = HttpUtils.executePost(this.endpoint, writer.toString(), CollectionUtils.wrap("principal", principal.getId(), "service", service.getServiceId()));
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            return MAPPER.readValue(response.getEntity().getContent(), new TypeReference<Map<String, Object>>() {
            });
        }
    } catch (final Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    return new HashMap<>(0);
}
Also used : MinimalPrettyPrinter(com.fasterxml.jackson.core.util.MinimalPrettyPrinter) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) HttpResponse(org.apache.http.HttpResponse) HashMap(java.util.HashMap) Map(java.util.Map)

Example 8 with MinimalPrettyPrinter

use of com.fasterxml.jackson.core.util.MinimalPrettyPrinter in project cas by apereo.

the class AbstractJacksonBackedStringSerializer method to.

@Override
@SneakyThrows
public void to(final File out, final T object) {
    try (StringWriter writer = new StringWriter()) {
        this.objectMapper.writer(this.prettyPrinter).writeValue(writer, object);
        if (isJsonFormat()) {
            try (Writer fileWriter = Files.newBufferedWriter(out.toPath(), StandardCharsets.UTF_8)) {
                final Stringify opt = this.prettyPrinter instanceof MinimalPrettyPrinter ? Stringify.PLAIN : Stringify.FORMATTED;
                JsonValue.readHjson(writer.toString()).writeTo(fileWriter, opt);
                fileWriter.flush();
            }
        } else {
            FileUtils.write(out, writer.toString(), StandardCharsets.UTF_8);
        }
    }
}
Also used : Stringify(org.hjson.Stringify) MinimalPrettyPrinter(com.fasterxml.jackson.core.util.MinimalPrettyPrinter) StringWriter(java.io.StringWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer) SneakyThrows(lombok.SneakyThrows)

Example 9 with MinimalPrettyPrinter

use of com.fasterxml.jackson.core.util.MinimalPrettyPrinter in project cas by apereo.

the class AbstractJacksonBackedStringSerializer method to.

@Override
@SneakyThrows
public void to(final Writer out, final T object) {
    try (StringWriter writer = new StringWriter()) {
        this.objectMapper.writer(this.prettyPrinter).writeValue(writer, object);
        if (isJsonFormat()) {
            final Stringify opt = this.prettyPrinter instanceof MinimalPrettyPrinter ? Stringify.PLAIN : Stringify.FORMATTED;
            JsonValue.readHjson(writer.toString()).writeTo(out, opt);
        } else {
            IOUtils.write(writer.toString(), out);
        }
    }
}
Also used : Stringify(org.hjson.Stringify) MinimalPrettyPrinter(com.fasterxml.jackson.core.util.MinimalPrettyPrinter) StringWriter(java.io.StringWriter) SneakyThrows(lombok.SneakyThrows)

Example 10 with MinimalPrettyPrinter

use of com.fasterxml.jackson.core.util.MinimalPrettyPrinter in project cas by apereo.

the class U2FRestResourceDeviceRepository method writeDevicesBackToResource.

@Override
protected void writeDevicesBackToResource(final List<U2FDeviceRegistration> list) {
    try (StringWriter writer = new StringWriter()) {
        final Map<String, List<U2FDeviceRegistration>> newDevices = new HashMap<>();
        newDevices.put(MAP_KEY_SERVICES, list);
        mapper.writer(new MinimalPrettyPrinter()).writeValue(writer, newDevices);
        HttpUtils.executePost(restProperties.getUrl(), restProperties.getBasicAuthUsername(), restProperties.getBasicAuthPassword(), writer.toString());
    } catch (final Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
}
Also used : MinimalPrettyPrinter(com.fasterxml.jackson.core.util.MinimalPrettyPrinter) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) List(java.util.List)

Aggregations

MinimalPrettyPrinter (com.fasterxml.jackson.core.util.MinimalPrettyPrinter)10 StringWriter (java.io.StringWriter)4 JsonFactory (com.fasterxml.jackson.core.JsonFactory)3 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 HashMap (java.util.HashMap)2 SneakyThrows (lombok.SneakyThrows)2 TimelineEntity (org.apache.hadoop.yarn.api.records.timeline.TimelineEntity)2 Stringify (org.hjson.Stringify)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 IOException (java.io.IOException)1 Writer (java.io.Writer)1 List (java.util.List)1 Map (java.util.Map)1 BasicJsonOutput (org.apache.drill.exec.vector.complex.fn.BasicJsonOutput)1 ExtendedJsonOutput (org.apache.drill.exec.vector.complex.fn.ExtendedJsonOutput)1 Configuration (org.apache.hadoop.conf.Configuration)1 FSDataOutputStream (org.apache.hadoop.fs.FSDataOutputStream)1 Path (org.apache.hadoop.fs.Path)1