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);
}
}
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;
}
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;
}
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;
}
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;
}
}
Aggregations