List of usage examples for com.fasterxml.jackson.databind JsonNode toString
public abstract String toString();
From source file:org.springframework.tuple.JsonStringToTupleConverter.java
@Override public Tuple convert(String source) { TupleBuilder builder = TupleBuilder.tuple(); try {/* w ww . j a v a 2 s .c o m*/ JsonNode root = mapper.readTree(source); for (Iterator<Entry<String, JsonNode>> it = root.fields(); it.hasNext();) { Entry<String, JsonNode> entry = it.next(); String name = entry.getKey(); JsonNode node = entry.getValue(); if (node.isObject()) { // tuple builder.addEntry(name, convert(node.toString())); } else if (node.isArray()) { builder.addEntry(name, nodeToList(node)); } else if (node.isNull()) { builder.addEntry(name, null); } else if (node.isBoolean()) { builder.addEntry(name, node.booleanValue()); } else if (node.isNumber()) { builder.addEntry(name, node.numberValue()); } else { builder.addEntry(name, mapper.treeToValue(node, Object.class)); } } } catch (Exception e) { throw new RuntimeException(e); } return builder.build(); }
From source file:com.redhat.lightblue.metadata.parser.ExtensionsTest.java
@Test public void convertHookTest() throws Exception { Extensions<JsonNode> ex = new Extensions<>(); HookTestCfgParser hookParser = new HookTestCfgParser(); ex.registerHookConfigurationParser("testHook", hookParser); ex.registerDataStoreParser("test", new TestDataStoreParser()); EntityInfo ei = new EntityInfo("test"); ArrayList<Hook> hooks = new ArrayList<>(); Hook hook = new Hook("testHook"); hook.setInsert(true);/*from w w w .ja v a2 s . c o m*/ hook.setProjection(new FieldProjection(new Path("*"), true, true)); hook.setConfiguration(new TestHookCfg("test")); hooks.add(hook); ei.getHooks().setHooks(hooks); JSONMetadataParser parser = new JSONMetadataParser(ex, new DefaultTypes(), nodeFactory); JsonNode node = parser.convert(ei); System.out.println(node); JSONAssert.assertEquals(node.toString(), json("{'name':'test','datastore':{'backend':'test' }, " + "'hooks':[ " + "{'name':'testHook','actions':['insert']," + "'projection':{'field':'*','include':true,'recursive':true}," + "'configuration':{'testField':'test'} } ] }").toString(), false); }
From source file:com.ericsson.eiffel.remrem.publish.service.EventTemplateHandler.java
private JsonNode jsonPathHandlerAdd(JsonNode updatedJson, String jsonKey, Object pojo) { updatedJson = JsonPath.using(configuration).parse(updatedJson.toString()).add(jsonKey, pojo).json(); return updatedJson; }
From source file:com.infinities.keystone4j.admin.v3.role.RoleV3ResourceTest.java
@Test public void testUpdateRole() throws ClientProtocolException, IOException { role1.setName("name"); role1.setDescription("description"); RoleWrapper wrapper = new RoleWrapper(role1); PatchClient client = new PatchClient("http://localhost:9998/v3/roles/" + role1.getId()); JsonNode node = client.connect(wrapper); System.err.println(node.toString()); JsonNode roleJ = node.get("role"); assertEquals(role1.getId(), roleJ.get("id").asText()); assertEquals(role1.getName(), roleJ.get("name").asText()); assertEquals(role1.getDescription(), roleJ.get("description").asText()); }
From source file:com.github.restdriver.matchers.HasJsonPath.java
@Override public boolean matchesSafely(JsonNode jsonNode) { Object jsonPathResult = null; try {/*from w w w . ja va 2s.c o m*/ jsonPathResult = JsonPath.read(jsonNode.toString(), jsonPath); if (matcher == null) { return jsonPathResult != null; } boolean initialMatchResult = matcher.matches(jsonPathResult); // if matcher is for longs and jsonPath returns an integer, do our best if (!initialMatchResult && jsonPathResult instanceof Integer) { return matcher.matches(intToLong(jsonPathResult)); } return initialMatchResult; } catch (InvalidPathException e) { return false; } catch (ClassCastException cce) { if (matcher.matches(intToLong(jsonPathResult))) { return true; } else { throw new RuntimeJsonTypeMismatchException( "JSONpath returned a type unsuitable for matching with the given matcher: " + cce.getMessage(), cce); } } }
From source file:com.mapr.data.sputnik.log.Log4JLogger.java
public void testme(String event) { ObjectMapper mapper = new ObjectMapper(); try {//from ww w . jav a 2s .c o m JsonNode node = mapper.readTree(event); Iterator<String> fieldNames = node.fieldNames(); while (fieldNames.hasNext()) { String fieldName = fieldNames.next(); JsonNode fieldValue = node.get(fieldName); byte[] x = Bytes.toBytes(fieldName); byte[] y = Bytes.toBytes(fieldValue.toString()); } } catch (Exception e) { e.printStackTrace(); } // printAll("root", node); // System.out.print("\n"); }
From source file:controllers.Parser.java
/** * Constructor from JSON object (typically from a POST request) * @param json/*from ww w . j ava 2 s . c o m*/ * @throws IllegalArgumentException */ public Parser(JsonNode json) throws IllegalArgumentException { Logger.info("POST Request: " + json.toString()); Map<String, String> map = new HashMap<String, String>(); // first we convert the json to a map for (Iterator<String> iterator = json.fieldNames(); iterator.hasNext();) { String key = iterator.next(); String value = json.get(key).asText(); Logger.debug(key + ":" + value); // IE sends empty parameters and this results in queries returning // 0 result. if (value.length() > 0) { map.put(key, key + ":\"" + value + "\""); } } extractParameters(map); }
From source file:com.ericsson.eiffel.remrem.publish.service.EventTemplateHandler.java
private JsonNode jsonPathHandlerSet(JsonNode updatedJson, String jsonKey, Object JsonValue) { updatedJson = JsonPath.using(configuration).parse(updatedJson.toString()).set(jsonKey, JsonValue).json(); return updatedJson; }
From source file:com.squarespace.template.plugins.platform.CommerceUtilsTest.java
@Test public void testGetItemVariantOptions() { Map<String, JsonNode> jsonMap = loadJson(CommerceUtilsTest.class, "get-item-variant-options.json"); for (String testKey : extractTests(jsonMap)) { JsonNode item = jsonMap.get(testKey); JsonNode actual = CommerceUtils.getItemVariantOptions(item); JsonNode expected = jsonMap.get(testKey + "-expected"); JsonAssert.assertJsonEquals(actual.toString(), expected.toString()); }/*from w w w .ja va2 s. c om*/ }
From source file:com.redhat.lightblue.eval.ArrayRangeProjectorTest.java
@Test public void array_range_projection_with_no_match_returns_empty_node() throws Exception { Projection p = EvalTestContext/*from w w w . j a va 2 s.c om*/ .projectionFromJson("{'field':'field7','range':[5,6],'project':{'field':'elemf3'}}"); Projector projector = Projector.getInstance(p, md); JsonNode expectedNode = JsonUtils.json("{}".replace('\'', '\"')); JsonDoc pdoc = projector.project(jsonDoc, JSON_NODE_FACTORY); Assert.assertEquals(expectedNode.toString(), pdoc.toString()); }