Example usage for com.fasterxml.jackson.databind ObjectReader withType

List of usage examples for com.fasterxml.jackson.databind ObjectReader withType

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind ObjectReader withType.

Prototype

public ObjectReader withType(TypeReference<?> valueTypeRef) 

Source Link

Document

Method for constructing a new reader instance that is configured to data bind into specified type.

Usage

From source file:de.dfki.kiara.jsonrpc.JsonRpcMessage.java

@Override
public ResponseObject getResponseObject(TypeToken<?> returnType) throws MessageDeserializationException {
    if (this.response != null) {
        return this.response;
    }/*w  ww .  j  a v  a2  s  .c om*/

    if (this.kind != Kind.RESPONSE && this.kind != Kind.EXCEPTION) {
        throw new IllegalStateException("not a response message");
    }

    if (this.kind == Kind.RESPONSE) {
        try {
            final ObjectReader reader = protocol.getObjectReader();
            final JsonParser parser = reader.treeAsTokens(this.params);
            final Object result = reader.withType(returnType.getType()).readValue(parser);
            this.response = new Message.ResponseObject(result, false);
            return this.response;
        } catch (JsonProcessingException ex) {
            throw new MessageDeserializationException(ex);
        } catch (IOException ex) {
            throw new MessageDeserializationException(ex);
        }
    } else {
        JsonRpcError jsonRpcError;
        try {
            jsonRpcError = protocol.getObjectReader().treeToValue(this.error, JsonRpcError.class);
        } catch (JsonProcessingException ex) {
            throw new MessageDeserializationException(ex);
        }
        this.response = new Message.ResponseObject(new GenericRemoteException(jsonRpcError.getMessage(),
                jsonRpcError.getCode(), jsonRpcError.getData()), true);
        return this.response;
    }
}

From source file:de.dfki.kiara.jsonrpc.JsonRpcMessage.java

@Override
public RequestObject getRequestObject(TypeToken<?>[] paramTypes) throws MessageDeserializationException {
    if (this.request != null) {
        return this.request;
    }//w ww  .  jav  a2  s  .  c o  m

    if (this.kind != Kind.REQUEST) {
        throw new IllegalStateException("not a request message");
    }

    Object[] args = null;
    if (params != null) {
        if (!params.isArray() && !params.isObject()) {
            throw new MessageDeserializationException("Member 'params' is neither array nor object");
        }

        if (params.isArray()) {
            args = new Object[paramTypes.length];

            int i = 0; // parameter index
            final int numParams = params.size();
            final int numParamTypes = paramTypes.length;
            final ObjectReader reader = protocol.getObjectReader();
            for (int j = 0; j < numParamTypes; ++j) {
                if (paramTypes[j] == null) // this parameter will be set later
                    continue;
                if (i >= numParams)
                    throw new MessageDeserializationException(
                            "Parameter index " + i + " is out of bounds, 'params' size is: " + numParams);
                try {
                    final JsonParser parser = reader.treeAsTokens(params.get(i));
                    args[j] = reader.withType(paramTypes[j].getType()).readValue(parser);
                } catch (JsonProcessingException ex) {
                    throw new MessageDeserializationException(ex);
                } catch (IOException ex) {
                    throw new MessageDeserializationException(ex);
                }
                ++i;
            }

            if (i != numParams)
                throw new MessageDeserializationException(
                        "Deserialzed " + i + " parameters, but required " + numParams);

        } else {
            throw new UnsupportedOperationException("Object is not supported as 'params' member");
        }
    }
    this.request = new RequestObject(methodName, args);
    return this.request;
}