private Object parseResult(JsonReader in, String id) { Object result; Type type = null; MethodProvider methodProvider = handler.getMethodProvider(); if (methodProvider != null && id != null) { String resolvedMethod = methodProvider.resolveMethod(id); if (resolvedMethod != null) { JsonRpcMethod jsonRpcMethod = handler.getJsonRpcMethod(resolvedMethod); if (jsonRpcMethod != null) type = jsonRpcMethod.getReturnType(); } } if (type == null) result = new JsonParser().parse(in); else result = gson.fromJson(in, type); return result; }
/** * Convert the json input into the result object corresponding to the call made * by id. * * If the id is not known until after parsing, call * {@link #parseResult(Object, String)} on the return value of this call for a * second chance conversion. * * @param in * json input to read from * @param id * id of request message this is in response to * @return correctly typed object if the correct expected type can be * determined, or a JsonElement representing the result */ protected Object parseResult(JsonReader in, String id) { Object result; Type type = null; MethodProvider methodProvider = handler.getMethodProvider(); if (methodProvider != null && id != null) { String resolvedMethod = methodProvider.resolveMethod(id); if (resolvedMethod != null) { JsonRpcMethod jsonRpcMethod = handler.getJsonRpcMethod(resolvedMethod); if (jsonRpcMethod != null) type = jsonRpcMethod.getReturnType(); } } if (type == null) result = new JsonParser().parse(in); else result = gson.fromJson(in, type); return result; }
/** * Convert the JsonElement into the result object corresponding to the call made * by id. If the result is already converted, does nothing. * * @param result * json element to read from * @param id * id of request message this is in response to * @return correctly typed object if the correct expected type can be * determined, or result unmodified if no conversion can be done. */ protected Object parseResult(Object result, String id) { if (isNull(result)) { return null; } if (result instanceof JsonElement) { // Type of result could not be resolved - try again with the parsed JSON tree MethodProvider methodProvider = handler.getMethodProvider(); if (methodProvider != null) { String resolvedMethod = methodProvider.resolveMethod(id); if (resolvedMethod != null) { JsonRpcMethod jsonRpcMethod = handler.getJsonRpcMethod(resolvedMethod); if (jsonRpcMethod != null) result = gson.fromJson((JsonElement) result, jsonRpcMethod.getReturnType()); } } } return result; }
@Override public Message read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; } in.beginObject(); String jsonrpc = null, id = null, method = null; Object rawParams = null; Object result = null; ResponseError error = null; while (in.hasNext()) { String name = in.nextName(); switch (name) { case "jsonrpc": { jsonrpc = in.nextString(); break; } case "id": { id = in.nextString(); break; } case "method": { method = in.nextString(); break; } case "params": { rawParams = parseParams(in, method); break; } case "result": { Type type = null; MethodProvider methodProvider = handler.getMethodProvider(); if (methodProvider != null && id != null) { String resolvedMethod = methodProvider.resolveMethod(id); if (resolvedMethod != null) { JsonRpcMethod jsonRpcMethod = handler.getJsonRpcMethod(resolvedMethod); if (jsonRpcMethod != null) type = jsonRpcMethod.getReturnType(); } } if (type == null) result = new JsonParser().parse(in); else result = gson.fromJson(in, type); break; } case "error": { error = gson.fromJson(in, ResponseError.class); break; } default: in.skipValue(); } } in.endObject(); Object params = parseParams(rawParams, method); return createMessage(jsonrpc, id, method, params, result, error); }