Search in sources :

Example 21 with ObjectWriter

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectWriter in project data-prep by Talend.

the class ConfiguredCacheWriter method write.

public void write(final ContentCacheKey key, final Object object) throws IOException {
    final ObjectMapper mapper = new ObjectMapper();
    final ObjectWriter objectWriter = mapper.writerFor(object.getClass());
    try (final OutputStream output = contentCache.put(key, ttl)) {
        objectWriter.writeValue(output, object);
        LOGGER.debug("New metadata cache entry -> {}.", key.getKey());
    }
}
Also used : OutputStream(java.io.OutputStream) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 22 with ObjectWriter

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectWriter in project cloudconductor-agent-redhat by cinovo.

the class AbstractApiHandler method request.

protected final HTTPRequest request(String path, Object obj, JavaType type) throws SerializationException {
    try {
        ObjectWriter bodyWriter = AbstractApiHandler.mapper.writer();
        if ((type != null) && ((obj instanceof Set) || (obj instanceof List))) {
            bodyWriter = bodyWriter.forType(type);
        }
        String body = bodyWriter.writeValueAsString(obj);
        return this.request(path).body(body).header("Content-Type", MediaType.APPLICATION_JSON);
    } catch (IOException e) {
        throw new SerializationException();
    }
}
Also used : Set(java.util.Set) SerializationException(de.cinovo.cloudconductor.api.lib.exceptions.SerializationException) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) List(java.util.List) IOException(java.io.IOException)

Example 23 with ObjectWriter

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectWriter in project rskj by rsksmart.

the class NetworkStateExporter method exportStatus.

public boolean exportStatus(String outputFile, String accountKey, boolean exportStorageKeys, boolean exportCode) {
    RepositorySnapshot frozenRepository = repositoryLocator.snapshotAt(blockchain.getBestBlock().getHeader());
    File dumpFile = new File(outputFile);
    try (FileWriter fw = new FileWriter(dumpFile.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw)) {
        JsonNodeFactory jsonFactory = new JsonNodeFactory(false);
        ObjectNode mainNode = jsonFactory.objectNode();
        if (accountKey.length() == 0) {
            exportAllAccounts(mainNode, frozenRepository, exportStorageKeys, exportCode);
        } else {
            RskAddress addr = new RskAddress(accountKey);
            exportAccount(addr, mainNode, frozenRepository, exportStorageKeys, exportCode);
        }
        ObjectMapper mapper = new ObjectMapper();
        ObjectWriter writer = mapper.writerWithDefaultPrettyPrinter();
        bw.write(writer.writeValueAsString(mainNode));
        return true;
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        panicProcessor.panic("dumpstate", e.getMessage());
        return false;
    }
}
Also used : RepositorySnapshot(co.rsk.db.RepositorySnapshot) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) FileWriter(java.io.FileWriter) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) BufferedWriter(java.io.BufferedWriter) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory)

Example 24 with ObjectWriter

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectWriter in project keycloak by keycloak.

the class Serialization method from.

public static <T> T from(T orig, T target) {
    if (orig == null) {
        return null;
    }
    @SuppressWarnings("unchecked") final Class<T> origClass = (Class<T>) orig.getClass();
    // Naive solution but will do.
    try {
        ObjectReader reader = MAPPER.readerForUpdating(target);
        ObjectWriter writer = WRITERS.computeIfAbsent(origClass, MAPPER::writerFor);
        final T res;
        res = reader.readValue(writer.writeValueAsBytes(orig));
        if (res != target) {
            throw new IllegalStateException("Should clone into desired target");
        }
        return res;
    } catch (IOException ex) {
        throw new IllegalStateException(ex);
    }
}
Also used : ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) IOException(java.io.IOException)

Example 25 with ObjectWriter

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectWriter in project ignite by apache.

the class DiscreteNaiveBayesModel method toJSON.

/**
 * {@inheritDoc}
 */
@Override
public void toJSON(Path path) {
    ObjectMapper mapper = new ObjectMapper().configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    mapper.addMixIn(DiscreteNaiveBayesModel.class, JSONModelMixIn.class);
    ObjectWriter writer = mapper.writerFor(DiscreteNaiveBayesModel.class).withAttribute("formatVersion", JSONModel.JSON_MODEL_FORMAT_VERSION).withAttribute("timestamp", System.currentTimeMillis()).withAttribute("uid", "dt_" + UUID.randomUUID().toString()).withAttribute("modelClass", DiscreteNaiveBayesModel.class.getSimpleName());
    try {
        File file = new File(path.toAbsolutePath().toString());
        writer.writeValue(file, this);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) IOException(java.io.IOException) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)140 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)61 IOException (java.io.IOException)31 Test (org.junit.Test)30 File (java.io.File)17 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)15 ArrayList (java.util.ArrayList)12 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)11 JavaType (com.fasterxml.jackson.databind.JavaType)10 JsonNode (com.fasterxml.jackson.databind.JsonNode)10 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)7 FileOutputStream (java.io.FileOutputStream)7 OutputStream (java.io.OutputStream)7 StringWriter (java.io.StringWriter)7 Map (java.util.Map)7 JCommander (com.beust.jcommander.JCommander)6 ParameterException (com.beust.jcommander.ParameterException)6 SimpleFilterProvider (com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider)6 RateLimiter (com.google.common.util.concurrent.RateLimiter)6 FileInputStream (java.io.FileInputStream)6