List of usage examples for com.fasterxml.jackson.databind.node TextNode textValue
public String textValue()
From source file:com.brainlounge.zooterrain.netty.WebSocketServerInboundHandler.java
private void handleWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) { // Check for closing frame if (frame instanceof CloseWebSocketFrame) { handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame.retain()); return;/*from w w w.j a v a 2 s . co m*/ } if (frame instanceof PingWebSocketFrame) { ctx.channel().write(new PongWebSocketFrame(frame.content().retain())); return; } if (!(frame instanceof TextWebSocketFrame)) { throw new UnsupportedOperationException( String.format("%s frame types not supported", frame.getClass().getName())); } String request = ((TextWebSocketFrame) frame).text(); if (logger.isLoggable(Level.FINE)) { logger.fine(String.format("%s received %s", ctx.channel(), request)); } // expecting JSON, parse now TreeNode jsonRequest; ClientRequest.Type requestType; try { final JsonParser parser = jsonMapper.getFactory().createParser(request); jsonRequest = parser.readValueAsTree(); TextNode type = (TextNode) jsonRequest.get("r"); requestType = ClientRequest.Type.valueOf(type.textValue()); } catch (Exception e) { logger.info("parsing JSON failed for '" + request + "'"); // TODO return error to client return; } if (requestType == ClientRequest.Type.i) { // client requested initial data // sending connection string info final ControlMessage handshakeInfo = new ControlMessage(zkStateObserver.getZkConnection(), ControlMessage.Type.H); writeClientMessage(ctx, handshakeInfo); // sending initial znodes try { zkStateObserver.initialTree("/", 6, Sets.<ZkStateListener>newHashSet(new OutboundConnector(ctx))); } catch (InterruptedException e) { e.printStackTrace(); } } else if (requestType == ClientRequest.Type.b) { final String znode; try { znode = ((TextNode) jsonRequest.get("z")).textValue(); } catch (Exception e) { return; // TODO return error } final DataMessage dataMessage = zkStateObserver.retrieveNodeData(znode); if (dataMessage != null) { writeClientMessage(ctx, dataMessage); } } else { System.out.println("unknown, unhandled client request = " + request); } }
From source file:io.apiman.test.common.json.JsonCompare.java
/** * Asserts that the JSON document matches what we expected. * @param expectedJson//from www .j a va 2s . c o m * @param actualJson */ public void assertJson(JsonNode expectedJson, JsonNode actualJson) { if (expectedJson instanceof ArrayNode) { JsonNode actualValue = actualJson; ArrayNode expectedArray = (ArrayNode) expectedJson; Assert.assertEquals( message("Expected JSON array but found non-array [{0}] instead.", actualValue.getClass().getSimpleName()), expectedJson.getClass(), actualValue.getClass()); ArrayNode actualArray = (ArrayNode) actualValue; Assert.assertEquals(message("Array size mismatch."), expectedArray.size(), actualArray.size()); JsonNode[] expected = new JsonNode[expectedArray.size()]; JsonNode[] actual = new JsonNode[actualArray.size()]; for (int idx = 0; idx < expected.length; idx++) { expected[idx] = expectedArray.get(idx); actual[idx] = actualArray.get(idx); } // If strict ordering is disabled, then sort both arrays if (getArrayOrdering() == JsonArrayOrderingType.any) { Comparator<? super JsonNode> comparator = new Comparator<JsonNode>() { @Override public int compare(JsonNode o1, JsonNode o2) { String str1 = o1.asText(); String str2 = o2.asText(); if (o1.isObject() && o2.isObject()) { // Try name (PermissionBean only) JsonNode o1NameNode = o1.get("name"); JsonNode o2NameNode = o2.get("name"); if (o1NameNode != null && o2NameNode != null) { str1 = o1NameNode.asText(); str2 = o2NameNode.asText(); } // Try username (UserBean only) JsonNode o1UsernameNode = o1.get("username"); JsonNode o2UsernameNode = o2.get("username"); if (o1UsernameNode != null && o2UsernameNode != null) { str1 = o1UsernameNode.asText(); str2 = o2UsernameNode.asText(); } // Try version (*VersionBeans) JsonNode o1VersionNode = o1.get("version"); JsonNode o2VersionNode = o2.get("version"); if (o1VersionNode != null && o2VersionNode != null) { str1 = o1VersionNode.asText(); str2 = o2VersionNode.asText(); } // Try OrganizationBean.id (Orgs) JsonNode o1OrgNode = o1.get("OrganizationBean"); JsonNode o2OrgNode = o2.get("OrganizationBean"); if (o1OrgNode != null && o2OrgNode != null) { str1 = o1OrgNode.get("id").asText(); str2 = o2OrgNode.get("id").asText(); } // Try ClientBean.id (Orgs) JsonNode o1ClientNode = o1.get("ClientBean"); JsonNode o2ClientNode = o2.get("ClientBean"); if (o1ClientNode != null && o2ClientNode != null) { str1 = o1ClientNode.get("id").asText(); str2 = o2ClientNode.get("id").asText(); } // Try PlanBean.id (Orgs) JsonNode o1PlanNode = o1.get("PlanBean"); JsonNode o2PlanNode = o2.get("PlanBean"); if (o1PlanNode != null && o2PlanNode != null) { str1 = o1PlanNode.get("id").asText(); str2 = o2PlanNode.get("id").asText(); } // Try ApiBean.id (Orgs) JsonNode o1ApiNode = o1.get("ApiBean"); JsonNode o2ApiNode = o2.get("ApiBean"); if (o1ApiNode != null && o2ApiNode != null) { str1 = o1ApiNode.get("id").asText(); str2 = o2ApiNode.get("id").asText(); } // Try Id (all other beans) JsonNode o1IdNode = o1.get("id"); JsonNode o2IdNode = o2.get("id"); if (o1IdNode != null && o2IdNode != null) { if (o1IdNode.isNumber()) { return new Long(o1IdNode.asLong()).compareTo(o2IdNode.asLong()); } str1 = o1IdNode.asText(); str2 = o2IdNode.asText(); } } int cmp = str1.compareTo(str2); if (cmp == 0) cmp = 1; return cmp; } }; Arrays.sort(expected, comparator); Arrays.sort(actual, comparator); } for (int idx = 0; idx < expected.length; idx++) { currentPath.push(idx); assertJson(expected[idx], actual[idx]); currentPath.pop(); } } else { Iterator<Entry<String, JsonNode>> fields = expectedJson.fields(); Set<String> expectedFieldNames = new HashSet<>(); while (fields.hasNext()) { Entry<String, JsonNode> entry = fields.next(); String expectedFieldName = entry.getKey(); expectedFieldNames.add(expectedFieldName); JsonNode expectedValue = entry.getValue(); currentPath.push(expectedFieldName); if (expectedValue instanceof TextNode) { TextNode tn = (TextNode) expectedValue; String expected = tn.textValue(); JsonNode actualValue = actualJson.get(expectedFieldName); if (isIgnoreCase()) { expected = expected.toLowerCase(); if (actualValue == null) { actualValue = actualJson.get(expectedFieldName.toLowerCase()); } } Assert.assertNotNull( message("Expected JSON text field \"{0}\" with value \"{1}\" but was not found.", expectedFieldName, expected), actualValue); Assert.assertEquals(message( "Expected JSON text field \"{0}\" with value \"{1}\" but found non-text [{2}] field with that name instead.", expectedFieldName, expected, actualValue.getClass().getSimpleName()), TextNode.class, actualValue.getClass()); String actual = ((TextNode) actualValue).textValue(); if (isIgnoreCase()) { if (actual != null) { actual = actual.toLowerCase(); } } if (!expected.equals("*")) { Assert.assertEquals(message("Value mismatch for text field \"{0}\".", expectedFieldName), expected, actual); } } else if (expectedValue.isNumber()) { NumericNode numeric = (NumericNode) expectedValue; Number expected = numeric.numberValue(); JsonNode actualValue = actualJson.get(expectedFieldName); try { Assert.assertNotNull( message("Expected JSON numeric field \"{0}\" with value \"{1}\" but was not found.", expectedFieldName, expected), actualValue); } catch (Error e) { throw e; } Assert.assertTrue(message( "Expected JSON numeric field \"{0}\" with value \"{1}\" but found non-numeric [{2}] field with that name instead.", expectedFieldName, expected, actualValue.getClass().getSimpleName()), actualValue.isNumber()); Number actual = ((NumericNode) actualValue).numberValue(); if (!"id".equals(expectedFieldName) || isCompareNumericIds()) { Assert.assertEquals(message("Value mismatch for numeric field \"{0}\".", expectedFieldName), expected, actual); } } else if (expectedValue instanceof BooleanNode) { BooleanNode bool = (BooleanNode) expectedValue; Boolean expected = bool.booleanValue(); JsonNode actualValue = actualJson.get(expectedFieldName); Assert.assertNotNull( message("Expected JSON boolean field \"{0}\" with value \"{1}\" but was not found.", expectedFieldName, expected), actualValue); Assert.assertEquals(message( "Expected JSON boolean field \"{0}\" with value \"{1}\" but found non-boolean [{2}] field with that name instead.", expectedFieldName, expected, actualValue.getClass().getSimpleName()), expectedValue.getClass(), actualValue.getClass()); Boolean actual = ((BooleanNode) actualValue).booleanValue(); Assert.assertEquals(message("Value mismatch for boolean field \"{0}\".", expectedFieldName), expected, actual); } else if (expectedValue instanceof ObjectNode) { JsonNode actualValue = actualJson.get(expectedFieldName); Assert.assertNotNull( message("Expected parent JSON field \"{0}\" but was not found.", expectedFieldName), actualValue); Assert.assertEquals( message("Expected parent JSON field \"{0}\" but found field of type \"{1}\".", expectedFieldName, actualValue.getClass().getSimpleName()), ObjectNode.class, actualValue.getClass()); assertJson(expectedValue, actualValue); } else if (expectedValue instanceof ArrayNode) { JsonNode actualValue = actualJson.get(expectedFieldName); Assert.assertNotNull( message("Expected JSON array field \"{0}\" but was not found.", expectedFieldName), actualValue); ArrayNode expectedArray = (ArrayNode) expectedValue; Assert.assertEquals(message( "Expected JSON array field \"{0}\" but found non-array [{1}] field with that name instead.", expectedFieldName, actualValue.getClass().getSimpleName()), expectedValue.getClass(), actualValue.getClass()); ArrayNode actualArray = (ArrayNode) actualValue; Assert.assertEquals(message("Field \"{0}\" array size mismatch.", expectedFieldName), expectedArray.size(), actualArray.size()); assertJson(expectedArray, actualArray); } else if (expectedValue instanceof NullNode) { JsonNode actualValue = actualJson.get(expectedFieldName); Assert.assertNotNull( message("Expected Null JSON field \"{0}\" but was not found.", expectedFieldName), actualValue); Assert.assertEquals( message("Expected Null JSON field \"{0}\" but found field of type \"{0}\".", expectedFieldName, actualValue.getClass().getSimpleName()), NullNode.class, actualValue.getClass()); } else { Assert.fail(message("Unsupported field type: {0}", expectedValue.getClass().getSimpleName())); } currentPath.pop(); } if (getMissingField() == JsonMissingFieldType.fail) { Set<String> actualFieldNames = new HashSet(); Iterator<String> names = actualJson.fieldNames(); while (names.hasNext()) { actualFieldNames.add(names.next()); } actualFieldNames.removeAll(expectedFieldNames); Assert.assertTrue(message("Found unexpected fields: {0}", StringUtils.join(actualFieldNames, ", ")), actualFieldNames.isEmpty()); } } }
From source file:com.aol.one.patch.DefaultPatcherTest.java
@Test public void testReplaceForSuccess() throws PatchException { List<PatchOperation> operations = new ArrayList<>(); TextNode strNode = new TextNode("1.1String"); DoubleNode doubleNode = new DoubleNode(10.1d); LongNode longNode = new LongNode(200L); TextNode childStrNode = new TextNode("2.1String"); DoubleNode childDoubleNode = new DoubleNode(102.1); operations.add(new ReplaceOperation("/doubleField", doubleNode)); operations.add(new ReplaceOperation("/strField", strNode)); operations.add(new ReplaceOperation("/longField", longNode)); operations.add(new ReplaceOperation("/child/strField", childStrNode)); operations.add(new ReplaceOperation("/child/doubleField", childDoubleNode)); patcher.patch(testObject, operations); assertThat(testObject.getDoubleField(), is(doubleNode.asDouble())); assertThat(testObject.getStrField(), is(strNode.asText())); assertThat(testObject.getLongField(), is(longNode.longValue())); verify(testObject).setStrField(strNode.asText()); verify(testObject).setDoubleField(doubleNode.doubleValue()); // replacement of long field via setLongField verify(testObject).setLongField(longNode.longValue()); // child/* w ww .ja va2s .c o m*/ assertThat(childTestObject.getDoubleField(), is(childDoubleNode.asDouble())); assertThat(childTestObject.getStrField(), is(childStrNode.asText())); verify(childTestObject).setStrField(childStrNode.textValue()); verify(childTestObject).setDoubleField(childDoubleNode.doubleValue()); }
From source file:io.swagger.parser.util.SwaggerDeserializer.java
public Path pathRef(TextNode ref, String location, ParseResult result) { RefPath output = new RefPath(); output.set$ref(ref.textValue()); return output; }