List of usage examples for com.google.gson JsonParseException JsonParseException
public JsonParseException(Throwable cause)
From source file:org.eclipse.leshan.server.bootstrap.demo.json.SecuritySerializer.java
License:Open Source License
@Override public JsonElement serialize(SecurityInfo src, Type typeOfSrc, JsonSerializationContext context) { JsonObject element = new JsonObject(); element.addProperty("endpoint", src.getEndpoint()); if (src.getIdentity() != null) { JsonObject psk = new JsonObject(); psk.addProperty("identity", src.getIdentity()); psk.addProperty("key", Hex.encodeHexString(src.getPreSharedKey())); element.add("psk", psk); }/*w w w .j a v a 2 s .co m*/ if (src.getRawPublicKey() != null) { JsonObject rpk = new JsonObject(); PublicKey rawPublicKey = src.getRawPublicKey(); if (rawPublicKey instanceof ECPublicKey) { ECPublicKey ecPublicKey = (ECPublicKey) rawPublicKey; // Get x coordinate byte[] x = ecPublicKey.getW().getAffineX().toByteArray(); if (x[0] == 0) x = Arrays.copyOfRange(x, 1, x.length); rpk.addProperty("x", Hex.encodeHexString(x)); // Get Y coordinate byte[] y = ecPublicKey.getW().getAffineY().toByteArray(); if (y[0] == 0) y = Arrays.copyOfRange(y, 1, y.length); rpk.addProperty("y", Hex.encodeHexString(y)); // Get Curves params rpk.addProperty("params", ecPublicKey.getParams().toString()); // Get raw public key in format PKCS8 (DER encoding) rpk.addProperty("pkcs8", Base64.encodeBase64String(ecPublicKey.getEncoded())); } else { throw new JsonParseException("Unsupported Public Key Format (only ECPublicKey supported)."); } element.add("rpk", rpk); } if (src.useX509Cert()) { element.addProperty("x509", true); } return element; }
From source file:org.eclipse.leshan.server.demo.servlet.json.LwM2mNodeDeserializer.java
License:Open Source License
@Override public LwM2mNode deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (json == null) { return null; }// w w w . j ava 2 s .c om LwM2mNode node; if (json.isJsonObject()) { JsonObject object = (JsonObject) json; if (!object.has("id")) { throw new JsonParseException("Missing id"); } int id = object.get("id").getAsInt(); if (object.has("instances")) { JsonArray array = object.get("instances").getAsJsonArray(); LwM2mObjectInstance[] instances = new LwM2mObjectInstance[array.size()]; for (int i = 0; i < array.size(); i++) { instances[i] = context.deserialize(array.get(i), LwM2mNode.class); } node = new LwM2mObject(id, instances); } else if (object.has("resources")) { JsonArray array = object.get("resources").getAsJsonArray(); LwM2mResource[] resources = new LwM2mResource[array.size()]; for (int i = 0; i < array.size(); i++) { resources[i] = context.deserialize(array.get(i), LwM2mNode.class); } node = new LwM2mObjectInstance(id, resources); } else if (object.has("value")) { // single value resource JsonPrimitive val = object.get("value").getAsJsonPrimitive(); org.eclipse.leshan.core.model.ResourceModel.Type expectedType = getTypeFor(val); node = LwM2mSingleResource.newResource(id, deserializeValue(val, expectedType), expectedType); } else if (object.has("values")) { // multi-instances resource Map<Integer, Object> values = new HashMap<>(); org.eclipse.leshan.core.model.ResourceModel.Type expectedType = null; for (Entry<String, JsonElement> entry : object.get("values").getAsJsonObject().entrySet()) { JsonPrimitive pval = entry.getValue().getAsJsonPrimitive(); expectedType = getTypeFor(pval); values.put(Integer.valueOf(entry.getKey()), deserializeValue(pval, expectedType)); } // use string by default; if (expectedType == null) expectedType = org.eclipse.leshan.core.model.ResourceModel.Type.STRING; node = LwM2mMultipleResource.newResource(id, values, expectedType); } else { throw new JsonParseException("Invalid node element"); } } else { throw new JsonParseException("Invalid node element"); } return node; }
From source file:org.eclipse.leshan.server.demo.servlet.json.SecurityDeserializer.java
License:Open Source License
@Override public SecurityInfo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (json == null) { return null; }/* w w w . ja va2s . c om*/ SecurityInfo info = null; if (json.isJsonObject()) { JsonObject object = (JsonObject) json; String endpoint; if (object.has("endpoint")) { endpoint = object.get("endpoint").getAsString(); } else { throw new JsonParseException("Missing endpoint"); } JsonObject psk = (JsonObject) object.get("psk"); JsonObject rpk = (JsonObject) object.get("rpk"); JsonPrimitive x509 = object.getAsJsonPrimitive("x509"); if (psk != null) { // PSK Deserialization String identity; if (psk.has("identity")) { identity = psk.get("identity").getAsString(); } else { throw new JsonParseException("Missing PSK identity"); } byte[] key; try { key = Hex.decodeHex(psk.get("key").getAsString().toCharArray()); } catch (IllegalArgumentException e) { throw new JsonParseException("key parameter must be a valid hex string", e); } info = SecurityInfo.newPreSharedKeyInfo(endpoint, identity, key); } else if (rpk != null) { PublicKey key; try { byte[] x = Hex.decodeHex(rpk.get("x").getAsString().toCharArray()); byte[] y = Hex.decodeHex(rpk.get("y").getAsString().toCharArray()); String params = rpk.get("params").getAsString(); AlgorithmParameters algoParameters = AlgorithmParameters.getInstance("EC"); algoParameters.init(new ECGenParameterSpec(params)); ECParameterSpec parameterSpec = algoParameters.getParameterSpec(ECParameterSpec.class); KeySpec keySpec = new ECPublicKeySpec(new ECPoint(new BigInteger(x), new BigInteger(y)), parameterSpec); key = KeyFactory.getInstance("EC").generatePublic(keySpec); } catch (IllegalArgumentException | InvalidKeySpecException | NoSuchAlgorithmException | InvalidParameterSpecException e) { throw new JsonParseException("Invalid security info content", e); } info = SecurityInfo.newRawPublicKeyInfo(endpoint, key); } else if (x509 != null && x509.getAsBoolean()) { info = SecurityInfo.newX509CertInfo(endpoint); } else { throw new JsonParseException("Invalid security info content"); } } return info; }
From source file:org.eclipse.leshan.standalone.servlet.json.LwM2mNodeDeserializer.java
License:Open Source License
@Override public LwM2mNode deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (json == null) { return null; }//from w w w. j a v a 2s . c om LwM2mNode node = null; if (json.isJsonObject()) { JsonObject object = (JsonObject) json; if (!object.has("id")) { throw new JsonParseException("Missing id"); } int id = object.get("id").getAsInt(); if (object.has("instances")) { JsonArray array = object.get("instances").getAsJsonArray(); LwM2mObjectInstance[] instances = new LwM2mObjectInstance[array.size()]; for (int i = 0; i < array.size(); i++) { instances[i] = context.deserialize(array.get(i), LwM2mNode.class); } node = new LwM2mObject(id, instances); } else if (object.has("resources")) { JsonArray array = object.get("resources").getAsJsonArray(); LwM2mResource[] resources = new LwM2mResource[array.size()]; for (int i = 0; i < array.size(); i++) { resources[i] = context.deserialize(array.get(i), LwM2mNode.class); } node = new LwM2mObjectInstance(id, resources); } else if (object.has("value")) { // single value resource node = new LwM2mResource(id, this.deserializeValue(object.get("value").getAsJsonPrimitive())); } else if (object.has("values")) { // multi-instances resource Collection<Value<?>> values = new ArrayList<>(); for (JsonElement val : object.get("values").getAsJsonArray()) { values.add(this.deserializeValue(val.getAsJsonPrimitive())); } node = new LwM2mResource(id, values.toArray(new Value<?>[0])); } else { throw new JsonParseException("Invalid node element"); } } else { throw new JsonParseException("Invalid node element"); } return node; }
From source file:org.eclipse.leshan.standalone.servlet.json.SecurityDeserializer.java
License:Open Source License
@Override public SecurityInfo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (json == null) { return null; }/*w ww . j a va 2 s.c o m*/ SecurityInfo info = null; if (json.isJsonObject()) { JsonObject object = (JsonObject) json; String endpoint = null; if (object.has("endpoint")) { endpoint = object.get("endpoint").getAsString(); } else { throw new JsonParseException("Missing endpoint"); } JsonObject psk = (JsonObject) object.get("psk"); JsonObject rpk = (JsonObject) object.get("rpk"); if (psk != null) { // PSK Deserialization String identity = null; if (psk.has("identity")) { identity = psk.get("identity").getAsString(); } else { throw new JsonParseException("Missing PSK identity"); } byte[] key; try { key = Hex.decodeHex(psk.get("key").getAsString().toCharArray()); } catch (DecoderException e) { throw new JsonParseException(e); } info = SecurityInfo.newPreSharedKeyInfo(endpoint, identity, key); } else if (rpk != null) { PublicKey key; try { byte[] x = Hex.decodeHex(rpk.get("x").getAsString().toCharArray()); byte[] y = Hex.decodeHex(rpk.get("y").getAsString().toCharArray()); String params = rpk.get("params").getAsString(); AlgorithmParameters algoParameters = AlgorithmParameters.getInstance("EC"); algoParameters.init(new ECGenParameterSpec(params)); ECParameterSpec parameterSpec = algoParameters.getParameterSpec(ECParameterSpec.class); KeySpec keySpec = new ECPublicKeySpec(new ECPoint(new BigInteger(x), new BigInteger(y)), parameterSpec); key = KeyFactory.getInstance("EC").generatePublic(keySpec); } catch (DecoderException | InvalidKeySpecException | NoSuchAlgorithmException | InvalidParameterSpecException e) { throw new JsonParseException("Invalid security info content", e); } info = SecurityInfo.newRawPublicKeyInfo(endpoint, key); } else { throw new JsonParseException("Invalid security info content"); } } return info; }
From source file:org.eclipse.leshan.standalone.servlet.json.SecuritySerializer.java
License:Open Source License
@Override public JsonElement serialize(SecurityInfo src, Type typeOfSrc, JsonSerializationContext context) { JsonObject element = new JsonObject(); element.addProperty("endpoint", src.getEndpoint()); if (src.getIdentity() != null) { JsonObject psk = new JsonObject(); psk.addProperty("identity", src.getIdentity()); psk.addProperty("key", Hex.encodeHexString(src.getPreSharedKey())); element.add("psk", psk); }/*w ww. j a v a 2 s.co m*/ if (src.getRawPublicKey() != null) { JsonObject rpk = new JsonObject(); PublicKey rawPublicKey = src.getRawPublicKey(); if (rawPublicKey instanceof ECPublicKey) { ECPublicKey ecPublicKey = (ECPublicKey) rawPublicKey; // Get x coordinate byte[] x = ecPublicKey.getW().getAffineX().toByteArray(); if (x[0] == 0) x = Arrays.copyOfRange(x, 1, x.length); rpk.addProperty("x", Hex.encodeHexString(x)); // Get Y coordinate byte[] y = ecPublicKey.getW().getAffineY().toByteArray(); if (y[0] == 0) y = Arrays.copyOfRange(y, 1, y.length); rpk.addProperty("y", Hex.encodeHexString(y)); // Get Curves params rpk.addProperty("params", ecPublicKey.getParams().toString()); } else { throw new JsonParseException("Unsupported Public Key Format (only ECPublicKey supported)."); } element.add("rpk", rpk); } return element; }
From source file:org.eclipse.lsp4j.jsonrpc.debug.adapters.DebugMessageTypeAdapter.java
License:Open Source License
private Message createMessage(String messageType, int seq, int request_seq, String method, boolean success, String errorMessage, Object params, Object body) throws JsonParseException { if (messageType == null) { throw new JsonParseException("Unable to identify the input message. Missing 'type' field."); }/*from ww w. j ava 2 s .co m*/ switch (messageType) { case "request": { DebugRequestMessage message = new DebugRequestMessage(); message.setId(seq); message.setMethod(method); message.setParams(params); return message; } case "event": { DebugNotificationMessage message = new DebugNotificationMessage(); message.setId(seq); message.setMethod(method); message.setParams(body); return message; } case "response": { DebugResponseMessage message = new DebugResponseMessage(); message.setId(request_seq); message.setResponseId(seq); message.setMethod(method); if (!success) { ResponseError error = new ResponseError(); error.setCode(ResponseErrorCode.UnknownErrorCode); error.setData(body); if (errorMessage == null) { // Some debug servers/clients don't provide a "message" field on an error. // Generally in those cases the body has some extra details to figure out // what went wrong. errorMessage = "Unset error message."; } error.setMessage(errorMessage); message.setError(error); } else { if (body 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(Integer.toString(request_seq)); if (resolvedMethod != null) { JsonRpcMethod jsonRpcMethod = handler.getJsonRpcMethod(resolvedMethod); if (jsonRpcMethod != null) { TypeAdapter<?> typeAdapter = null; Type returnType = jsonRpcMethod.getReturnType(); if (jsonRpcMethod.getReturnTypeAdapterFactory() != null) typeAdapter = jsonRpcMethod.getReturnTypeAdapterFactory().create(gson, TypeToken.get(returnType)); JsonElement jsonElement = (JsonElement) body; if (typeAdapter != null) body = typeAdapter.fromJsonTree(jsonElement); else body = gson.fromJson(jsonElement, returnType); } } } } message.setResult(body); } return message; } default: throw new JsonParseException("Unable to identify the input message."); } }
From source file:org.eclipse.lsp4j.jsonrpc.json.adapters.EitherTypeAdapter.java
License:Open Source License
protected Either<L, R> create(JsonToken nextToken, JsonReader in) throws IOException { boolean matchesLeft = left.isAssignable(nextToken); boolean matchesRight = right.isAssignable(nextToken); if (matchesLeft && matchesRight) { if (leftChecker != null || rightChecker != null) { JsonElement element = new JsonParser().parse(in); if (leftChecker != null && leftChecker.test(element)) // Parse the left alternative from the JSON element tree return createLeft(left.read(element)); if (rightChecker != null && rightChecker.test(element)) // Parse the right alternative from the JSON element tree return createRight(right.read(element)); }/*from w w w. j ava 2 s .c om*/ throw new JsonParseException( "Ambiguous Either type: token " + nextToken + " matches both alternatives."); } else if (matchesLeft) { // Parse the left alternative from the JSON stream return createLeft(left.read(in)); } else if (matchesRight) { // Parse the right alternative from the JSON stream return createRight(right.read(in)); } else { throw new JsonParseException( "Unexpected token " + nextToken + ": expected " + left + " | " + right + " tokens."); } }
From source file:org.eclipse.lsp4j.jsonrpc.json.adapters.MessageTypeAdapter.java
License:Open Source License
protected Message createMessage(String jsonrpc, Either<String, Number> id, String method, Object params, Object responseResult, ResponseError responseError) throws JsonParseException { if (id != null && method != null) { RequestMessage message = new RequestMessage(); message.setJsonrpc(jsonrpc);//w w w .j a v a2 s . co m message.setRawId(id); message.setMethod(method); message.setParams(params); return message; } else if (id != null) { ResponseMessage message = new ResponseMessage(); message.setJsonrpc(jsonrpc); message.setRawId(id); if (responseError != null) message.setError(responseError); else message.setResult(responseResult); return message; } else if (method != null) { NotificationMessage message = new NotificationMessage(); message.setJsonrpc(jsonrpc); message.setMethod(method); message.setParams(params); return message; } else { throw new JsonParseException("Unable to identify the input message."); } }
From source file:org.eclipse.lsp4j.jsonrpc.json.adapters.ThrowableTypeAdapter.java
License:Open Source License
@SuppressWarnings("unchecked") @Override// ww w . ja va 2 s. c o m public Throwable read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; } in.beginObject(); String message = null; Throwable cause = null; while (in.hasNext()) { String name = in.nextName(); switch (name) { case "message": { message = in.nextString(); break; } case "cause": { cause = read(in); break; } default: in.skipValue(); } } in.endObject(); try { Constructor<Throwable> constructor; if (message == null && cause == null) { constructor = (Constructor<Throwable>) typeToken.getRawType().getDeclaredConstructor(); return constructor.newInstance(); } else if (message == null) { constructor = (Constructor<Throwable>) typeToken.getRawType() .getDeclaredConstructor(Throwable.class); return constructor.newInstance(cause); } else if (cause == null) { constructor = (Constructor<Throwable>) typeToken.getRawType().getDeclaredConstructor(String.class); return constructor.newInstance(message); } else { constructor = (Constructor<Throwable>) typeToken.getRawType().getDeclaredConstructor(String.class, Throwable.class); return constructor.newInstance(message, cause); } } catch (NoSuchMethodException e) { if (message == null && cause == null) return new RuntimeException(); else if (message == null) return new RuntimeException(cause); else if (cause == null) return new RuntimeException(message); else return new RuntimeException(message, cause); } catch (Exception e) { throw new JsonParseException(e); } }