Example usage for com.fasterxml.jackson.databind.node ObjectNode get

List of usage examples for com.fasterxml.jackson.databind.node ObjectNode get

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind.node ObjectNode get.

Prototype

public JsonNode get(String paramString) 

Source Link

Usage

From source file:org.onosproject.codec.impl.OpticalConnectivityIntentCodec.java

@Override
public OpticalConnectivityIntent decode(ObjectNode json, CodecContext context) {

    OpticalConnectivityIntent.Builder builder = OpticalConnectivityIntent.builder();
    IntentCodec.intentAttributes(json, context, builder);
    ObjectNode ingressJson = get(json, INGRESS_POINT);
    ObjectNode egressJson = get(json, EGRESS_POINT);
    JsonCodec<ConnectPoint> connectPointCodec = context.codec(ConnectPoint.class);
    if (ingressJson != null) {
        ConnectPoint src = connectPointCodec.decode(ingressJson, context);
        builder.src(src);//from w  ww.  j  a  va 2s  .co  m
    }

    if (egressJson != null) {
        ConnectPoint dst = connectPointCodec.decode(egressJson, context);
        builder.dst(dst);
    }

    builder.signalType(OduSignalType.ODU4); //Automate this assignment

    ObjectNode bidJson = get(json, BIDIRECTIONAL);
    if (bidJson != null) {
        boolean isBidirectional = bidJson.get(BIDIRECTIONAL).booleanValue();
        builder.bidirectional(isBidirectional);
    }
    return builder.build();
}

From source file:com.helger.wsdlgen.exchange.InterfaceReader.java

@Nullable
public static WGInterface readInterface(@Nonnull final IReadableResource aRes) {
    final ObjectNode aInterfaceNode = _readAsJSON(aRes);
    if (aInterfaceNode == null)
        return null;

    final String sInterfaceName = _getChildAsText(aInterfaceNode, "$name");
    final String sInterfaceNamespace = _getChildAsText(aInterfaceNode, "$namespace");
    final String sInterfaceEndpoint = _getChildAsText(aInterfaceNode, "$endpoint");

    final WGInterface aInterface = new WGInterface(sInterfaceName, sInterfaceNamespace, sInterfaceEndpoint);
    // Dont't format global interface comment
    aInterface.setDocumentation(_getChildAsText(aInterfaceNode, "$doc"));

    // Add all types
    final ObjectNode aTypesNode = (ObjectNode) aInterfaceNode.get("$types");
    if (aTypesNode != null) {
        // Read all contained types
        for (final Map.Entry<String, JsonNode> aTypeEntry : _getAllChildren(aTypesNode).entrySet()) {
            final String sTypeName = aTypeEntry.getKey();
            final JsonNode aTypeNode = aTypeEntry.getValue();

            if (!sTypeName.startsWith("$")) {
                final boolean bIsSimpleType = sTypeName.startsWith("#");
                if (bIsSimpleType) {
                    // Simple type
                    final WGSimpleType aSimpleType = new WGSimpleType(sInterfaceNamespace,
                            sTypeName.substring(1));
                    final WGTypeDef aSimpleTypeDef = new WGTypeDef(aSimpleType);
                    for (final Map.Entry<String, JsonNode> aChildTypeEntry : _getAllChildren(aTypeNode)
                            .entrySet()) {
                        final String sTypeChildName = aChildTypeEntry.getKey();
                        final JsonNode aTypeChildNode = aChildTypeEntry.getValue();

                        if (sTypeChildName.equals("$doc"))
                            aSimpleTypeDef.setDocumentation(_getDocumentation(aTypeChildNode.asText()));
                        else if (sTypeChildName.equals("$extension")) {
                            final String sExtension = aTypeChildNode.asText();
                            final IWGType aExtensionType = aInterface.getTypeOfName(sExtension);
                            if (aExtensionType == null)
                                throw new IllegalArgumentException("Simple type '" + aSimpleType.getName()
                                        + "' has invalid extension type '" + sExtension + "'");
                            aSimpleType.setExtension(aExtensionType);
                        } else if (sTypeChildName.equals("$restriction")) {
                            final String sRestriction = aTypeChildNode.asText();
                            final IWGType aRestrictionType = aInterface.getTypeOfName(sRestriction);
                            if (aRestrictionType == null)
                                throw new IllegalArgumentException("Simple type '" + aSimpleType.getName()
                                        + "' has invalid restriction type '" + sRestriction + "'");
                            aSimpleType.setRestriction(aRestrictionType);
                        } else if (sTypeChildName.equals("$enum")) {
                            final ArrayNode aEntries = (ArrayNode) aTypeChildNode;
                            if (aEntries == null)
                                throw new IllegalArgumentException(
                                        "Simple type '" + aSimpleType.getName() + "' has invalid enum entries");

                            // Convert all to string
                            final List<WGEnumEntry> aEnumEntries = new ArrayList<WGEnumEntry>();
                            for (final JsonNode aPropValue : aEntries) {
                                if (aPropValue instanceof ArrayNode) {
                                    // [key, documentation]
                                    final ArrayNode aProvValueList = (ArrayNode) aPropValue;
                                    aEnumEntries.add(new WGEnumEntry(aProvValueList.get(0).asText(),
                                            aProvValueList.get(1).asText()));
                                } else {
                                    // Just the key, without documentation
                                    aEnumEntries.add(new WGEnumEntry(aPropValue.asText()));
                                }/* w  w w . j a  v a2 s.  c o m*/
                            }
                            aSimpleType.setEnumEntries(aEnumEntries);
                        } else if (sTypeChildName.equals("$maxlength")) {
                            final int nMaxLength = aTypeChildNode.asInt(-1);
                            if (nMaxLength <= 0)
                                throw new IllegalArgumentException("Simple type '" + aSimpleType.getName()
                                        + "' has invalid maxLength definition '" + aTypeChildNode.asText()
                                        + "'");

                            aSimpleType.setMaxLength(nMaxLength);
                        } else if (!sTypeChildName.startsWith("$")) {
                            // Only attributes allowed!
                            if (!sTypeChildName.startsWith("@"))
                                throw new IllegalArgumentException("Simple type '" + aSimpleType.getName()
                                        + "' may only have attributes and not '" + sTypeChildName + "'");

                            final WGTypeDef aTypeDef = _readTypeDef(aInterface, sTypeChildName, aTypeChildNode);
                            aSimpleType.addChildAttribute(sTypeChildName.substring(1), aTypeDef);
                        } else
                            throw new IllegalStateException("Unhandled simple type child: " + sTypeChildName);
                    }
                    aInterface.addType(aSimpleTypeDef);
                } else {
                    // Complex type
                    final WGComplexType aComplexType = new WGComplexType(sInterfaceNamespace, sTypeName);
                    final WGTypeDef aComplexTypeDef = new WGTypeDef(aComplexType);
                    for (final Map.Entry<String, JsonNode> aChildTypeEntry : _getAllChildren(aTypeNode)
                            .entrySet()) {
                        final String sTypeChildName = aChildTypeEntry.getKey();
                        final JsonNode aTypeChildNode = aChildTypeEntry.getValue();

                        if (sTypeChildName.equals("$doc"))
                            aComplexTypeDef.setDocumentation(_getDocumentation(aTypeChildNode.asText()));
                        else if (sTypeChildName.equals("$type"))
                            aComplexType.setType(EComplexType.getFromTagNameOrThrow(aTypeChildNode.asText()));
                        else if (!sTypeChildName.startsWith("$")) {
                            final WGTypeDef aChildTypeDef = _readTypeDef(aInterface, sTypeChildName,
                                    aTypeChildNode);

                            // Attribute or element?
                            if (sTypeChildName.startsWith("@"))
                                aComplexType.addChildAttribute(sTypeChildName.substring(1), aChildTypeDef);
                            else
                                aComplexType.addChildElement(sTypeChildName, aChildTypeDef);
                        } else
                            throw new IllegalStateException("Unhandled complex type child: " + sTypeChildName);
                    }
                    aInterface.addType(aComplexTypeDef);
                }
            } else
                throw new IllegalStateException("Unhandled special type: " + sTypeName);
        }
    }

    // Add all methods
    for (final Map.Entry<String, JsonNode> aMethodEntry : _getAllChildren(aInterfaceNode).entrySet()) {
        final String sMethodName = aMethodEntry.getKey();

        if (!sMethodName.startsWith("$")) {
            final ObjectNode aMethodNode = (ObjectNode) aMethodEntry.getValue();
            final WGMethod aMethod = new WGMethod(sMethodName);

            // Input
            final JsonNode aInputNode = aMethodNode.get("$input");
            if (aInputNode != null && aInputNode.isObject()) {
                aMethod.markHavingInput();
                for (final Map.Entry<String, JsonNode> aEntry : _getAllChildren(aInputNode).entrySet()) {
                    final String sParamName = aEntry.getKey();
                    final String sParamType = aEntry.getValue().asText();
                    final IWGType aParamType = aInterface.getTypeOfName(sParamType);
                    if (aParamType == null)
                        throw new IllegalArgumentException("Input type '" + sParamType + "' of method '"
                                + sMethodName + "' for element '" + sParamName + "' not found");
                    aMethod.addInputParam(sParamName, aParamType);
                }
            }

            // Output
            final JsonNode aOutputNode = aMethodNode.get("$output");
            if (aOutputNode != null && aOutputNode.isObject()) {
                aMethod.markHavingOutput();
                for (final Map.Entry<String, JsonNode> aEntry : _getAllChildren(aOutputNode).entrySet()) {
                    final String sParamName = aEntry.getKey();
                    final String sParamType = aEntry.getValue().asText();
                    final IWGType aParamType = aInterface.getTypeOfName(sParamType);
                    if (aParamType == null)
                        throw new IllegalArgumentException("Output type '" + sParamType + "' of method '"
                                + sMethodName + "' for element '" + sParamName + "' not found");
                    aMethod.addOutputParam(sParamName, aParamType);
                }
            }

            // Fault
            final JsonNode aFaultNode = aMethodNode.get("$fault");
            if (aFaultNode != null && aFaultNode.isObject()) {
                aMethod.markHavingFault();
                for (final Map.Entry<String, JsonNode> aEntry : _getAllChildren(aFaultNode).entrySet()) {
                    final String sParamName = aEntry.getKey();
                    final String sParamType = aEntry.getValue().asText();
                    final IWGType aParamType = aInterface.getTypeOfName(sParamType);
                    if (aParamType == null)
                        throw new IllegalArgumentException("Fault type '" + sParamType + "' of method '"
                                + sMethodName + "' for element '" + sParamName + "' not found");
                    aMethod.addFaultParam(sParamName, aParamType);
                }
            }

            aInterface.addMethod(aMethod);
        }
    }

    return aInterface;
}

From source file:com.redhat.lightblue.crud.CRUDFindRequest.java

/**
 * Parses a find request from a json object. Unrecognized elements are
 * ignored.// w w w .j a va  2 s .  c  om
 */
public void fromJson(ObjectNode node) {
    JsonNode x = node.get("query");
    if (x != null) {
        query = QueryExpression.fromJson(x);
    }
    x = node.get("projection");
    if (x != null) {
        projection = Projection.fromJson(x);
    }
    x = node.get("sort");
    if (x != null) {
        sort = Sort.fromJson(x);
    }
    x = node.get("range");
    if (x instanceof ArrayNode && ((ArrayNode) x).size() == 2) {
        from = ((ArrayNode) x).get(0).asLong();
        to = ((ArrayNode) x).get(1).asLong();
    }
}

From source file:easyrpc.client.serialization.jsonrpc.JSONCaller.java

@Override
public Object deserializeResponse(Class returnType, byte[] response) {
    try {/*  w  w  w.  ja  v  a2s. com*/
        ObjectNode resp = (ObjectNode) MAPPER.readTree(response);

        String jsonversion = resp.get("jsonrpc").textValue();
        if (!"2.0".equals(jsonversion)) {
            throw new SerializationException(
                    "'jsonrpc' value must be '2.0' and actually is '" + jsonversion + "'");
        }

        // todo: differentiate exceptions as defined in the interfaces
        if (resp.has("error")) {
            JsonNode error = resp.get("error");
            throw new RemoteMethodException(error.toString());
        }
        //            System.out.println("resp.get(\"result\").toString() = " + resp.toString());
        if (!returnType.equals(Void.class) && !returnType.equals(void.class)) {
            Object result = MAPPER.treeToValue(resp.get("result"), returnType);
            return result;
        } else {
            return null;
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.lendingclub.mercator.docker.DockerSerializerModule.java

void renameAttribute(ObjectNode x, String key, String key2) {
    JsonNode val = x.get(key);
    if (val != null) {
        x.remove(key);//from  www . ja v a  2s .c o m
        x.set(key2, val);
    }
}

From source file:com.squarespace.template.ReferenceScannerTest.java

@Test
public void testTextBytes() throws CodeException {
    ObjectNode result = scan("{.section a}abcde{.or}fghij{.end}");
    render(result);//from  ww w  . j  a  v a2s .  co  m
    assertEquals(result.get("textBytes").asInt(), 10);
}

From source file:com.squarespace.template.ReferenceScannerTest.java

@Test
public void testFormatters() throws CodeException {
    ObjectNode result = scan("{a|json|html|json}{b|json}{c|html}");
    render(result);/*from  www.  j av  a  2s . co  m*/
    assertEquals(result.get("formatters").get("json").asInt(), 3);
    assertEquals(result.get("formatters").get("html").asInt(), 2);
}

From source file:com.strandls.alchemy.rest.client.exception.ExceptionPayloadDeserializer.java

@SuppressWarnings("unchecked")
@Override//  w ww. java 2s  .  c o  m
public ExceptionPayload deserialize(final JsonParser jp, final DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    final ObjectMapper sourceObjectMapper = ((ObjectMapper) jp.getCodec());

    final ObjectNode tree = jp.readValueAsTree();

    String message = null;
    if (tree.has("exceptionMessage")) {
        message = tree.get("exceptionMessage").asText();
    }

    Throwable exception = null;
    try {
        final String className = tree.get("exceptionClassFQN").asText();
        final Class<? extends Throwable> clazz = (Class<? extends Throwable>) ReflectionUtils
                .forName(className);
        exception = sourceObjectMapper.treeToValue(tree.get("exception"), clazz);
    } catch (final Throwable t) {
        log.warn("Error deserializing exception class", t);
        exception = new InternalServerErrorException(message);
    }

    return new ExceptionPayload(exception.getClass().getName(), message, exception);
}

From source file:br.com.criativasoft.opendevice.core.json.CommandDeserialize.java

@Override
public Command deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {

    ObjectMapper mapper = (ObjectMapper) jp.getCodec();
    ObjectNode root = (ObjectNode) mapper.readTree(jp);
    Class<? extends Command> commandClass = null;

    JsonNode jsonType = root.get("type");
    int type = jsonType.intValue();

    CommandType commandType = CommandType.getByCode(type);

    if (commandType == null)
        throw new IllegalArgumentException("type of command must be provided !");

    if (DeviceCommand.isCompatible(commandType)) {
        return mapper.readValue(root.toString(), DeviceCommand.class);
    } else {/*w  w w  .  j a  v a2  s .com*/

        Class<? extends Command> cmdClass = registry.get(commandType);

        if (cmdClass == null) {
            throw new IllegalArgumentException(
                    "Command type not supported!! You need configure in CommandDeserialize");
        }

        return mapper.readValue(root.toString(), cmdClass);

    }

}

From source file:com.googlecode.jsonrpc4j.DefaultExceptionResolver.java

/**
 * Creates a {@link JsonRpcClientException} from the given
 * {@link ObjectNode}./*from  w ww  . j a  va 2 s  .c  o  m*/
 * @param errorObject the error object
 * @return the exception
 */
private JsonRpcClientException createJsonRpcClientException(ObjectNode errorObject) {
    int code = errorObject.has("code") ? errorObject.get("code").asInt() : 0;
    return new JsonRpcClientException(code, errorObject.get("message").asText(), errorObject.get("data"));
}