Search in sources :

Example 16 with JsonNodeFactory

use of com.fasterxml.jackson.databind.node.JsonNodeFactory in project logging-log4j2 by apache.

the class ContextDataJsonAttributeConverter method convertToDatabaseColumn.

@Override
public String convertToDatabaseColumn(final ReadOnlyStringMap contextData) {
    if (contextData == null) {
        return null;
    }
    try {
        final JsonNodeFactory factory = OBJECT_MAPPER.getNodeFactory();
        final ObjectNode root = factory.objectNode();
        contextData.forEach((key, value) -> {
            // we will cheat here and write the toString of the Object... meh, but ok.
            root.put(key, String.valueOf(value));
        });
        return OBJECT_MAPPER.writeValueAsString(root);
    } catch (final Exception e) {
        throw new PersistenceException("Failed to convert contextData to JSON string.", e);
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) PersistenceException(javax.persistence.PersistenceException) IOException(java.io.IOException) PersistenceException(javax.persistence.PersistenceException) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory)

Example 17 with JsonNodeFactory

use of com.fasterxml.jackson.databind.node.JsonNodeFactory in project spring-sync by spring-projects.

the class JsonPatchPatchConverter method convert.

/**
 * Renders a {@link Patch} as a {@link JsonNode}.
 * @param patch the patch
 * @return a {@link JsonNode} containing JSON Patch.
 */
public JsonNode convert(Patch patch) {
    List<PatchOperation> operations = patch.getOperations();
    JsonNodeFactory nodeFactory = JsonNodeFactory.instance;
    ArrayNode patchNode = nodeFactory.arrayNode();
    for (PatchOperation operation : operations) {
        ObjectNode opNode = nodeFactory.objectNode();
        opNode.set("op", nodeFactory.textNode(operation.getOp()));
        opNode.set("path", nodeFactory.textNode(operation.getPath()));
        if (operation instanceof FromOperation) {
            FromOperation fromOp = (FromOperation) operation;
            opNode.set("from", nodeFactory.textNode(fromOp.getFrom()));
        }
        Object value = operation.getValue();
        if (value != null) {
            opNode.set("value", MAPPER.valueToTree(value));
        }
        patchNode.add(opNode);
    }
    return patchNode;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) FromOperation(org.springframework.sync.FromOperation) PatchOperation(org.springframework.sync.PatchOperation) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory)

Example 18 with JsonNodeFactory

use of com.fasterxml.jackson.databind.node.JsonNodeFactory in project native-navigation by airbnb.

the class ConversionUtil method toJsonObject.

/**
 * Converts a {@link ReadableMap} into an Json {@link ObjectNode}
 */
static ObjectNode toJsonObject(ReadableMap readableMap) {
    JsonNodeFactory nodeFactory = JsonNodeFactory.instance;
    ObjectNode result = nodeFactory.objectNode();
    ReadableMapKeySetIterator iterator = readableMap.keySetIterator();
    while (iterator.hasNextKey()) {
        String key = iterator.nextKey();
        ReadableType type = readableMap.getType(key);
        switch(type) {
            case Null:
                result.putNull(key);
                break;
            case Boolean:
                result.put(key, readableMap.getBoolean(key));
                break;
            case Number:
                result.put(key, readableMap.getDouble(key));
                break;
            case String:
                result.put(key, readableMap.getString(key));
                break;
            case Map:
                result.set(key, toJsonObject(readableMap.getMap(key)));
                break;
            case Array:
                result.set(key, toJsonArray(readableMap.getArray(key)));
                break;
            default:
                Log.e(TAG, "Could not convert object with key: " + key + ".");
        }
    }
    return result;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory)

Example 19 with JsonNodeFactory

use of com.fasterxml.jackson.databind.node.JsonNodeFactory in project native-navigation by airbnb.

the class ConversionUtil method toJsonArray.

/**
 * Converts a {@link ReadableArray} into an Json {@link ArrayNode}
 */
static ArrayNode toJsonArray(ReadableArray readableArray) {
    JsonNodeFactory nodeFactory = JsonNodeFactory.instance;
    ArrayNode result = nodeFactory.arrayNode();
    for (int i = 0; i < readableArray.size(); i++) {
        ReadableType indexType = readableArray.getType(i);
        switch(indexType) {
            case Null:
                result.addNull();
                break;
            case Boolean:
                result.add(readableArray.getBoolean(i));
                break;
            case Number:
                result.add(readableArray.getDouble(i));
                break;
            case String:
                result.add(readableArray.getString(i));
                break;
            case Map:
                result.add(toJsonObject(readableArray.getMap(i)));
                break;
            case Array:
                result.add(toJsonArray(readableArray.getArray(i)));
                break;
            default:
                Log.e(TAG, "Could not convert object at index " + i + ".");
        }
    }
    return result;
}
Also used : ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory)

Example 20 with JsonNodeFactory

use of com.fasterxml.jackson.databind.node.JsonNodeFactory 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)

Aggregations

JsonNodeFactory (com.fasterxml.jackson.databind.node.JsonNodeFactory)35 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)28 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)17 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 IOException (java.io.IOException)5 Test (org.junit.Test)5 Blueprint (com.sequenceiq.cloudbreak.domain.Blueprint)4 Cluster (com.sequenceiq.cloudbreak.domain.Cluster)4 Gateway (com.sequenceiq.cloudbreak.domain.Gateway)4 HostGroup (com.sequenceiq.cloudbreak.domain.HostGroup)4 InstanceGroup (com.sequenceiq.cloudbreak.domain.InstanceGroup)4 HashSet (java.util.HashSet)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 File (java.io.File)3 ArrayList (java.util.ArrayList)3 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)2 EventEntity (io.gravitee.management.model.EventEntity)2 BufferedWriter (java.io.BufferedWriter)2 FileWriter (java.io.FileWriter)2 PersistenceException (javax.persistence.PersistenceException)2