下面列出了com.fasterxml.jackson.databind.node.JsonNodeFactory#instance ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void testHostIDNtoASCII() throws MalformedURLException {
ObjectNode filterParams = new ObjectNode(JsonNodeFactory.instance);
filterParams.put("hostIDNtoASCII", true);
URLFilter urlFilter = createFilter(filterParams);
URL testSourceUrl = new URL("http://www.example.com/");
String inputURL = "http://señal6.com.ar/";
String expectedURL = "http://xn--seal6-pta.com.ar/";
String normalizedUrl = urlFilter.filter(testSourceUrl, new Metadata(),
inputURL);
assertEquals("Failed to filter query string", expectedURL,
normalizedUrl);
inputURL = "http://сфера.укр/";
expectedURL = "http://xn--80aj7acp.xn--j1amh/";
normalizedUrl = urlFilter.filter(testSourceUrl, new Metadata(),
inputURL);
assertEquals("Failed to filter query string", expectedURL,
normalizedUrl);
}
private ObjectNode organizeColumns(JsonNode items, AttributeDefinition partitionKey, Optional<AttributeDefinition> clusteringKey) {
ObjectNode itemsClone = new ObjectNode(JsonNodeFactory.instance);
ObjectNode jsonBlobNode = new ObjectNode(JsonNodeFactory.instance);
for (Iterator<Map.Entry<String, JsonNode>> it = items.fields(); it.hasNext(); ) {
Map.Entry<String, JsonNode> item = it.next();
String itemKey = item.getKey();
if ( partitionKey.getAttributeName().equals(itemKey) ||
(clusteringKey.isPresent() && clusteringKey.get().getAttributeName().equals(itemKey)))
itemsClone.put("\"" + itemKey + "\"", stripDynamoTypes(item.getValue()));
else
jsonBlobNode.put(itemKey, item.getValue());
}
itemsClone.put("json_blob", items);
return itemsClone;
}
private static PluginResult getResult(String action, JsonNode data, JsonNode error) {
JsonNodeFactory factory = JsonNodeFactory.instance;
ObjectNode resultObject = factory.objectNode();
if (action != null) {
resultObject.set(JsParams.General.ACTION, factory.textNode(action));
}
if (data != null) {
resultObject.set(JsParams.General.DATA, data);
}
if (error != null) {
resultObject.set(JsParams.General.ERROR, error);
}
return new PluginResult(PluginResult.Status.OK, resultObject.toString());
}
/**
* 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;
}
protected void afterProducer(Exchange exchange) throws IOException {
Message in = exchange.getIn();
JsonNodeFactory factory = JsonNodeFactory.instance;
ObjectNode statusNode = factory.objectNode();
int response = 400;
String info = "No Information";
HttpStatusCode code = in.getBody(HttpStatusCode.class);
if (code != null) {
response = code.getStatusCode();
info = code.getInfo();
}
statusNode.put("Response", response);
statusNode.put("Information", info);
String json = OBJECT_MAPPER.writeValueAsString(statusNode);
in.setBody(json);
}
/** Create JSON objects from the database model */
public RootJSON(NodeModel node, boolean includeCreator) {
super(JsonNodeFactory.instance);
// create special root level JSON object
this.put(IdeaJSON.MAPJS_JSON_ID_KEY, MAPJS_JSON_ROOT_ID_VALUE);
this.put("formatVersion", "3");
if (node != null) {
this.put(IdeaJSON.MAPJS_JSON_TITLE_KEY, node.getConcept().getText());
}
// start the recursion to create the ideas objects
ObjectNode ideas = JsonNodeFactory.instance.objectNode();
ideas.set("1", new IdeaJSON(node, 0, includeCreator));
this.set(IdeaJSON.MAPJS_JSON_IDEAS_KEY, ideas);
}
@Test
public void deserialize() throws IOException {
Map<String, JsonNode> kids = new HashMap<>();
kids.put(READONLY, node);
kids.put(DISPOSED, node);
kids.put(ENCRYPTED_BYTES, node);
kids.put(BASE64_SHA1_HASH, node);
ObjectNode tree = new ObjectNode(JsonNodeFactory.instance, kids);
String testString = "randomTestString";
byte[] encryptedBytes = EncryptorFactory.getInstance().getDefaultEncryptor().encrypt(testString.getBytes());
String encryptedString = Base64.getEncoder().encodeToString(encryptedBytes);
when(jp.readValueAsTree()).thenReturn(tree);
when(node.asText()).thenReturn(encryptedString);
assertEquals(Boolean.FALSE, ReflectionTestUtils.getField(deserializer.deserialize(jp, ctx), READONLY));
kids.remove(READONLY);
assertEquals(Boolean.FALSE, ReflectionTestUtils.getField(deserializer.deserialize(jp, ctx), DISPOSED));
kids.remove(DISPOSED);
assertEquals(encryptedString,
ReflectionTestUtils.getField(deserializer.deserialize(jp, ctx), BASE64_SHA1_HASH));
kids.remove(BASE64_SHA1_HASH);
GuardedString expected = new GuardedString(new String(testString.getBytes()).toCharArray());
assertTrue(EqualsBuilder.reflectionEquals(ReflectionTestUtils.getField(expected, ENCRYPTED_BYTES),
ReflectionTestUtils.getField(deserializer.deserialize(jp, ctx), ENCRYPTED_BYTES)));
}
@Test
public void testHashes() throws MalformedURLException {
ObjectNode filterParams = new ObjectNode(JsonNodeFactory.instance);
filterParams.put("removeHashes", true);
URLFilter urlFilter = createFilter(filterParams);
URL testSourceUrl = new URL("http://florida-chemical.com");
String in = "http://www.florida-chemical.com/Diacetone-Alcohol-DAA-99.html?xid_0b629=12854b827878df26423d933a5baf86d5";
String out = "http://www.florida-chemical.com/Diacetone-Alcohol-DAA-99.html";
String normalizedUrl = urlFilter.filter(testSourceUrl, new Metadata(),
in);
assertEquals("Failed to filter query string", out, normalizedUrl);
in = "http://www.maroongroupllc.com/maroon/login/auth;jsessionid=8DBFC2FEDBD740BBC8B4D1A504A6DE7F";
out = "http://www.maroongroupllc.com/maroon/login/auth";
normalizedUrl = urlFilter.filter(testSourceUrl, new Metadata(), in);
assertEquals("Failed to filter query string", out, normalizedUrl);
}
private static JsonNode createErrorNode(int errorCode, String errorDescription) {
JsonNodeFactory factory = JsonNodeFactory.instance;
ObjectNode errorData = factory.objectNode();
errorData.set(JsParams.Error.CODE, factory.numberNode(errorCode));
errorData.set(JsParams.Error.DESCRIPTION, factory.textNode(errorDescription));
return errorData;
}
@Override
public JsonNode writeParser( final IParser parser ) throws IOException {
ArrayNode array = new ArrayNode( JsonNodeFactory.instance );
for ( int i = 0 ; i < parser.size() ; i++ ) {
IParser childParser = parser.getParser( i );
if ( childParser.isMap() || childParser.isStruct() ) {
array.add( JacksonParserToJsonObject.getFromObjectParser( childParser ) );
} else if ( childParser.isArray() ) {
array.add( JacksonParserToJsonObject.getFromArrayParser( childParser ) );
} else {
array.add( PrimitiveObjectToJsonNode.get( parser.get( i ) ) );
}
}
return array;
}
@Override
public JsonNode writeParser( final IParser parser ) throws IOException {
ObjectNode objectNode = new ObjectNode( JsonNodeFactory.instance );
for ( String key : parser.getAllKey() ) {
IParser childParser = parser.getParser( key );
if ( childParser.isMap() || childParser.isStruct() ) {
objectNode.put( key , JacksonParserToJsonObject.getFromObjectParser( childParser ) );
} else if ( childParser.isArray() ) {
objectNode.put( key , JacksonParserToJsonObject.getFromArrayParser( childParser ) );
} else {
objectNode.put( key , PrimitiveObjectToJsonNode.get( parser.get( key ) ) );
}
}
return objectNode;
}
private URLFilter createFilter(boolean removeAnchor,
List<String> queryElementsToRemove) {
ObjectNode filterParams = new ObjectNode(JsonNodeFactory.instance);
filterParams.set("queryElementsToRemove",
getArrayNode(queryElementsToRemove));
filterParams.put("removeAnchorPart", Boolean.valueOf(removeAnchor));
return createFilter(filterParams);
}
private void addComponentMeta(ObjectNode root, ClassLoader classLoader) {
// is there any custom Camel components in this library?
ObjectNode component = new ObjectNode(JsonNodeFactory.instance);
ObjectNode componentMeta = getComponentMeta(classLoader);
if (componentMeta != null) {
component.set("meta", componentMeta);
}
addOptionalSchemaAsString(classLoader, component, "schema", "camel-component-schema.json");
if (component.size() > 0) {
root.set("component", component);
}
}
/**
* Get the status of the given job if it exists.
*
* @param id The id of the job to get status for
* @return The status of the job as one of: {@link JobStatus}
* @throws NotFoundException When no job with {@literal id} exists
*/
@GetMapping(value = "/{id}/status", produces = MediaType.APPLICATION_JSON_VALUE)
public JsonNode getJobStatus(@PathVariable("id") final String id) throws NotFoundException {
log.info("[getJobStatus] Called for job with id: {}", id);
final JsonNodeFactory factory = JsonNodeFactory.instance;
return factory
.objectNode()
.set(
"status",
factory.textNode(DtoConverters.toV3JobStatus(this.persistenceService.getJobStatus(id)).toString())
);
}
private URLFilter createFilter(int length, int repet) {
BasicURLFilter filter = new BasicURLFilter();
ObjectNode filterParams = new ObjectNode(JsonNodeFactory.instance);
filterParams.put("maxPathRepetition", repet);
filterParams.put("maxLength", length);
Map<String, Object> conf = new HashMap<>();
filter.configure(conf, filterParams);
return filter;
}
@Query
public JsonResult getJsonArray(int count) {
JsonNodeFactory jsonNodeFactory = JsonNodeFactory.instance;
ArrayNode arrayNode = new ArrayNode(jsonNodeFactory);
for (int i = 0; i < count; i++) {
arrayNode.add(i);
}
JsonResult jsonResult = new JsonResult();
jsonResult.jsonNode = arrayNode;
return jsonResult;
}
public Converter() {
jsonNodeFactory = JsonNodeFactory.instance;
}
private JsonEventToLogger(Logger logger) {
this(JsonNodeFactory.instance, logger);
}
public NotifyResponseJSON(int ok, Long requestId, Long nodeId) {
super(JsonNodeFactory.instance);
this.put(OK, ok);
this.put(REQUEST_ID_KEY, requestId);
this.put(NODE_ID_KEY, nodeId);
}
private URLFilter createFilter(boolean removeAnchor, boolean checkValidURI) {
ObjectNode filterParams = new ObjectNode(JsonNodeFactory.instance);
filterParams.put("removeAnchorPart", Boolean.valueOf(removeAnchor));
filterParams.put("checkValidURI", Boolean.valueOf(checkValidURI));
return createFilter(filterParams);
}