Example usage for com.fasterxml.jackson.databind JsonNode asText

List of usage examples for com.fasterxml.jackson.databind JsonNode asText

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind JsonNode asText.

Prototype

public abstract String asText();

Source Link

Usage

From source file:com.redhat.lightblue.mediator.Mediator.java

private void updatePredefinedFields(OperationContext ctx, CRUDController controller, String entity) {
    for (JsonDoc doc : ctx.getDocuments()) {
        PredefinedFields.updateArraySizes(factory.getNodeFactory(), doc);
        JsonNode node = doc.get(OBJECT_TYPE_PATH);
        if (node == null) {
            doc.modify(OBJECT_TYPE_PATH, factory.getNodeFactory().textNode(entity), false);
        } else if (!node.asText().equals(entity)) {
            throw Error.get(CrudConstants.ERR_INVALID_ENTITY, node.asText());
        }//from  ww  w .  j  a v a2s . c  om
        controller.updatePredefinedFields(ctx, doc);
    }
}

From source file:org.opendaylight.nemo.renderer.openflow.physicalnetwork.PhyConfigLoaderTest.java

public void testBuildPortAttributes() throws Exception {
    Class<PhyConfigLoader> class1 = PhyConfigLoader.class;
    Method method = class1.getDeclaredMethod("buildPortAttributes", new Class[] { JsonNode.class });
    method.setAccessible(true);/*from   w ww  . j a  v a  2  s.co m*/

    List<Attribute> attributeList;
    JsonNode attributes = mock(JsonNode.class);
    JsonNode portAttribute = mock(JsonNode.class);
    JsonNode jsonNode_temp = mock(JsonNode.class);

    when(attributes.size()).thenReturn(1);
    when(attributes.get(any(Integer.class))).thenReturn(portAttribute);
    //get into method "buildPortAttribute"
    when(portAttribute.path(any(String.class))).thenReturn(jsonNode_temp);
    when(jsonNode_temp.asText()).thenReturn(new String(""))//branch null
            .thenReturn(new String("zm"));
    attributeList = (List<Attribute>) method.invoke(phyConfigLoader, attributes);
    Assert.assertTrue(attributeList.size() == 0);
    //new AttributeName("zm");
    attributeList = (List<Attribute>) method.invoke(phyConfigLoader, attributes);
    Assert.assertTrue(attributeList.size() == 1);
    verify(portAttribute, times(3)).path(any(String.class));
    verify(jsonNode_temp, times(3)).asText();
}

From source file:org.opendaylight.nemo.renderer.cli.physicalnetwork.PhysicalResourceLoaderTest.java

@Test
public void testBuildLinks() throws Exception {
    List<PhysicalLink> list;
    Class<PhysicalResourceLoader> class1 = PhysicalResourceLoader.class;
    Method method = class1.getDeclaredMethod("buildLinks", new Class[] { JsonNode.class });
    method.setAccessible(true);/*w  ww. j  a v  a 2  s .com*/

    JsonNode linksRoot = mock(JsonNode.class);
    JsonNode links = mock(JsonNode.class);
    JsonNode link = mock(JsonNode.class);
    JsonNode link_temp_buildlink = mock(JsonNode.class);

    when(linksRoot.path(any(String.class))).thenReturn(links);
    when(links.size()).thenReturn(1);
    when(links.get(any(Integer.class))).thenReturn(link);
    //get into method "build link" args(link)
    when(link.get(any(String.class))).thenReturn(link_temp_buildlink);
    when(link_temp_buildlink.asText()).thenReturn(new String("1"))//new PhysicalLinkId(strLinkId)
            .thenReturn(new String("2"))//new PhysicalNodeId
            .thenReturn(new String("3"))//new PhysicalPortId
            .thenReturn(new String("4"))//new PhysicalNodeId
            .thenReturn(new String("5"))//new PhysicalPortId
            .thenReturn(new String(""));//linkNode.get("link-bandwidth").asText().equals("")
    when(link_temp_buildlink.asLong()).thenReturn((long) 1);

    list = (List<PhysicalLink>) method.invoke(physicalResourceLoader, linksRoot);
    Assert.assertTrue(list.size() == 1);
    verify(link_temp_buildlink, times(6)).asText();
}

From source file:de.ks.flatadocdb.defaults.DefaultEntityPersister.java

@Override
public Object load(Repository repository, EntityDescriptor descriptor, Path path,
        Map<Relation, Collection<String>> relationIds) {
    try {/*from  w  ww .j a  v a  2  s  .c om*/
        JsonNode jsonNode = mapper.readTree(path.toFile());
        descriptor.getAllRelations().forEach(rel -> {
            String name = rel.getRelationField().getName();
            JsonNode jsonValue = jsonNode.findValue(name);
            if (jsonValue.elements().hasNext()) {
                jsonValue = jsonValue.elements().next();
            }

            ArrayList<String> ids = new ArrayList<>();

            relationIds.put(rel, ids);

            if (jsonValue.isContainerNode()) {
                jsonValue.elements().forEachRemaining(id -> ids.add(id.asText()));
            } else if (!jsonValue.isNull()) {
                ids.add(jsonValue.asText());
            }
            log.debug("Found {} relation id's in {}: {}", ids.size(), name, ids);
        });
        return mapper.readValue(path.toFile(), descriptor.getEntityClass());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:clearfacts.cds.JsonDataSource.java

public Object getFieldValue(JRField jrField) throws JRException {
    if (currentJsonNode == null) {
        return null;
    }/*www .  j a v a 2s .com*/
    String expression = jrField.getDescription();
    if (expression == null || expression.length() == 0) {
        return null;
    }
    Object value = null;

    Class<?> valueClass = jrField.getValueClass();
    JsonNode selectedObject = getJsonData(currentJsonNode, expression);
    if (Object.class != valueClass) {
        if (selectedObject != null) {
            try {
                if (valueClass.equals(String.class)) {
                    value = selectedObject.asText();

                } else if (valueClass.equals(Boolean.class)) {
                    value = selectedObject.booleanValue();

                } else if (Number.class.isAssignableFrom(valueClass)) {
                    value = convertStringValue(selectedObject.asText(), valueClass);

                } else if (Date.class.isAssignableFrom(valueClass)) {
                    value = convertStringValue(selectedObject.asText(), valueClass);

                } else {
                    throw new JRException("Field '" + jrField.getName() + "' is of class '"
                            + valueClass.getName() + "' and cannot be converted");
                }
            } catch (Exception e) {
                throw new JRException("Unable to get value for field '" + jrField.getName() + "' of class '"
                        + valueClass.getName() + "'", e);
            }
        }
    } else {
        value = selectedObject;
    }

    return value;
}

From source file:org.eclipse.winery.bpmn2bpel.parser.Bpmn4JsonParser.java

protected Set<String> extractNodeTargetIds(JsonNode node) {
    Set<String> linkTargetIds = new HashSet<String>();
    /* Look for the 'connections' element within the node or its children */
    JsonNode connectionsNode = node.findValue(JsonKeys.CONNECTIONS);
    /*//from   ww w .  jav a  2 s  .c  o  m
     * The connection node hosts an array of all outgoing connections to
     * other nodes
     */
    if (connectionsNode != null && connectionsNode.isArray()) {
        Iterator<JsonNode> iter = connectionsNode.iterator();
        while (iter.hasNext()) {
            JsonNode connectionEntry = (JsonNode) iter.next();
            /*
             * Should always be true as the connection entry is the id of
             * the target node
             */
            if (connectionEntry.isTextual()) {
                linkTargetIds.add(connectionEntry.asText());
            } else {
                // TODO warn
            }

        }
    } else {
        log.debug("Node with id '" + node.get(JsonKeys.ID) + "' has no connections to other nodes");
        return null;
    }
    return linkTargetIds;
}

From source file:com.baasbox.commands.UsersResource.java

protected JsonNode changeUsername(JsonNode command, JsonCallback unused) throws CommandException {
    String username = getUsername(command);
    JsonNode newUsernameJson = getParamField(command, "newUsername");
    if (newUsernameJson == null || !newUsernameJson.isTextual())
        throw new CommandExecutionException(command, "invalid new username: " + newUsernameJson);
    String newUsername = newUsernameJson.asText();
    try {/*from   ww w  .  ja  v a 2  s  .  c  om*/
        UserService.changeUsername(username, newUsername);
    } catch (UserNotFoundException | OpenTransactionException | SqlInjectionException e) {
        throw new CommandExecutionException(command, ExceptionUtils.getMessage(e), e);
    }
    return NullNode.getInstance();
}

From source file:com.baasbox.commands.UsersResource.java

protected JsonNode changePassword(JsonNode command, JsonCallback unused) throws CommandException {
    String username = getUsername(command);
    JsonNode newPasswordJson = getParamField(command, "newPassword");
    if (newPasswordJson == null || !newPasswordJson.isTextual())
        throw new CommandExecutionException(command, "invalid new password: " + newPasswordJson);
    String newPassword = newPasswordJson.asText();
    try {/*from  w w  w . j  av a2s. co m*/
        UserService.changePassword(username, newPassword);
    } catch (UserNotFoundException | OpenTransactionException | SqlInjectionException e) {
        throw new CommandExecutionException(command, ExceptionUtils.getMessage(e), e);
    }
    return NullNode.getInstance();
}

From source file:org.teavm.flavour.json.test.SerializerTest.java

@Test
public void writesProperty() {
    A obj = new A();
    obj.setA("foo");
    obj.setB(23);/*  w w w.j a  v  a  2s  . c om*/
    JsonNode node = JSONRunner.serialize(obj);

    assertTrue("Root node shoud be JSON object", node.isObject());

    assertTrue("Property `a' exists", node.has("a"));
    JsonNode aNode = node.get("a");
    assertEquals("foo", aNode.asText());

    assertTrue("Property `b' exists", node.has("b"));
    JsonNode bNode = node.get("b");
    assertEquals(23, bNode.asInt());
}