List of usage examples for com.google.gson JsonParseException JsonParseException
public JsonParseException(Throwable cause)
From source file:com.google.gerrit.server.events.SupplierDeserializer.java
License:Apache License
@Override public Supplier<?> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { checkArgument(typeOfT instanceof ParameterizedType); ParameterizedType parameterizedType = (ParameterizedType) typeOfT; if (parameterizedType.getActualTypeArguments().length != 1) { throw new JsonParseException("Expected one parameter type in Supplier interface."); }// w w w .ja v a 2s . com Type supplierOf = parameterizedType.getActualTypeArguments()[0]; return Suppliers.ofInstance(context.deserialize(json, supplierOf)); }
From source file:com.google.gwtjsonrpc.server.CallDeserializer.java
License:Apache License
public CallType deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException, NoSuchRemoteMethodException { if (!json.isJsonObject()) { throw new JsonParseException("Expected object"); }/* ww w . j a va 2 s . c om*/ final JsonObject in = json.getAsJsonObject(); req.id = in.get("id"); final JsonElement jsonrpc = in.get("jsonrpc"); final JsonElement version = in.get("version"); if (isString(jsonrpc) && version == null) { final String v = jsonrpc.getAsString(); if ("2.0".equals(v)) { req.versionName = "jsonrpc"; req.versionValue = jsonrpc; } else { throw new JsonParseException("Expected jsonrpc=2.0"); } } else if (isString(version) && jsonrpc == null) { final String v = version.getAsString(); if ("1.1".equals(v)) { req.versionName = "version"; req.versionValue = version; } else { throw new JsonParseException("Expected version=1.1"); } } else { throw new JsonParseException("Expected version=1.1 or jsonrpc=2.0"); } final JsonElement method = in.get("method"); if (!isString(method)) { throw new JsonParseException("Expected method name as string"); } req.method = server.lookupMethod(method.getAsString()); if (req.method == null) { throw new NoSuchRemoteMethodException(); } final JsonElement callback = in.get("callback"); if (callback != null) { if (!isString(callback)) { throw new JsonParseException("Expected callback as string"); } req.callback = callback.getAsString(); } final JsonElement xsrfKey = in.get("xsrfKey"); if (xsrfKey != null) { if (!isString(xsrfKey)) { throw new JsonParseException("Expected xsrfKey as string"); } req.xsrfKeyIn = xsrfKey.getAsString(); } final Type[] paramTypes = req.method.getParamTypes(); final JsonElement params = in.get("params"); if (params != null) { if (!params.isJsonArray()) { throw new JsonParseException("Expected params array"); } final JsonArray paramsArray = params.getAsJsonArray(); if (paramsArray.size() != paramTypes.length) { throw new JsonParseException("Expected " + paramTypes.length + " parameter values in params array"); } final Object[] r = new Object[paramTypes.length]; for (int i = 0; i < r.length; i++) { final JsonElement v = paramsArray.get(i); if (v != null) { r[i] = context.deserialize(v, paramTypes[i]); } } req.params = r; } else { if (paramTypes.length != 0) { throw new JsonParseException("Expected params array"); } req.params = JsonServlet.NO_PARAMS; } return req; }
From source file:com.google.gwtjsonrpc.server.JsonServlet.java
License:Apache License
private String readBody(final ActiveCall call) throws IOException { if (!isBodyJson(call)) { throw new JsonParseException("Invalid Request Content-Type"); }/*from www. ja va2s. co m*/ if (!isBodyUTF8(call)) { throw new JsonParseException("Invalid Request Character-Encoding"); } final int len = call.httpRequest.getContentLength(); if (len < 0) { throw new JsonParseException("Invalid Request Content-Length"); } if (len == 0) { throw new JsonParseException("Invalid Request POST Body Required"); } if (len > maxRequestSize()) { throw new JsonParseException("Invalid Request POST Body Too Large"); } final InputStream in = call.httpRequest.getInputStream(); if (in == null) { throw new JsonParseException("Invalid Request POST Body Required"); } try { final byte[] body = new byte[len]; int off = 0; while (off < len) { final int n = in.read(body, off, len - off); if (n <= 0) { throw new JsonParseException("Invalid Request Incomplete Body"); } off += n; } final CharsetDecoder d = Charset.forName(JsonUtil.JSON_ENC).newDecoder(); d.onMalformedInput(CodingErrorAction.REPORT); d.onUnmappableCharacter(CodingErrorAction.REPORT); try { return d.decode(ByteBuffer.wrap(body)).toString(); } catch (CharacterCodingException e) { throw new JsonParseException("Invalid Request Not UTF-8", e); } } finally { in.close(); } }
From source file:com.google.gwtjsonrpc.server.MapDeserializer.java
License:Apache License
public Map<Object, Object> deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException { final Type kt = ((ParameterizedType) typeOfT).getActualTypeArguments()[0]; final Type vt = ((ParameterizedType) typeOfT).getActualTypeArguments()[1]; if (json.isJsonNull()) { return null; }//from w w w .j a v a 2 s .com if (kt == String.class) { if (!json.isJsonObject()) { throw new JsonParseException("Expected object for map type"); } final JsonObject p = (JsonObject) json; final Map<Object, Object> r = createInstance(typeOfT); for (final Map.Entry<String, JsonElement> e : p.entrySet()) { final Object v = context.deserialize(e.getValue(), vt); r.put(e.getKey(), v); } return r; } else { if (!json.isJsonArray()) { throw new JsonParseException("Expected array for map type"); } final JsonArray p = (JsonArray) json; final Map<Object, Object> r = createInstance(typeOfT); for (int n = 0; n < p.size();) { final Object k = context.deserialize(p.get(n++), kt); final Object v = context.deserialize(p.get(n++), vt); r.put(k, v); } return r; } }
From source file:com.google.gwtjsonrpc.server.SqlDateDeserializer.java
License:Apache License
public java.sql.Date deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException { if (json.isJsonNull()) { return null; }/*w w w .ja v a 2 s. c o m*/ if (!json.isJsonPrimitive()) { throw new JsonParseException("Expected string for date type"); } final JsonPrimitive p = (JsonPrimitive) json; if (!p.isString()) { throw new JsonParseException("Expected string for date type"); } try { return java.sql.Date.valueOf(p.getAsString()); } catch (IllegalArgumentException e) { throw new JsonParseException("Not a date string"); } }
From source file:com.google.gwtjsonrpc.server.SqlTimestampDeserializer.java
License:Apache License
public java.sql.Timestamp deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException { if (json.isJsonNull()) { return null; }//from w w w . j a v a 2s .co m if (!json.isJsonPrimitive()) { throw new JsonParseException("Expected string for timestamp type"); } final JsonPrimitive p = (JsonPrimitive) json; if (!p.isString()) { throw new JsonParseException("Expected string for timestamp type"); } return JavaSqlTimestamp_JsonSerializer.parseTimestamp(p.getAsString()); }
From source file:com.google.iosched.server.input.DataSourceInput.java
License:Open Source License
public JsonArray fetch(EnumType entityType) throws IOException { JsonElement element = getFetcher().fetch(entityType, null); if (element == null) { return null; }//from w ww. ja v a 2 s . c om if (element.isJsonArray()) { return element.getAsJsonArray(); } else { throw new JsonParseException( "Invalid response from DataSourceInput. Expected " + "a JsonArray, but fetched " + element.getClass().getName() + ". Entity fetcher is " + getFetcher()); } }
From source file:com.google.javascript.jscomp.NpmCommandLineRunner.java
License:Apache License
private JsonElement nullParseCheck(JsonElement e) { if (e == null) { throw new JsonParseException("Couldn't retrieve element."); }/*from w w w . j a v a 2 s .c om*/ return e; }
From source file:com.google.nigori.common.TypeAdapterByteString.java
License:Apache License
@SuppressWarnings("deprecation") // see comment below @Override//from www . j av a 2s.c o m public ByteString deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { byte[] jsonBytes = json.getAsString().getBytes(); // (drt24) Since android ships with an ancient version of org.apache.commons.codec which // overrides any version we ship we have to use old deprecated methods. if (Base64.isArrayByteBase64(jsonBytes)) { return ByteString.copyFrom(Base64.decodeBase64(jsonBytes)); } else { throw new JsonParseException("JSON element is not correctly base64 encoded."); } }
From source file:com.google.nigori.common.TypeAdapterProtobuf.java
License:Apache License
@Override public GeneratedMessage deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject jsonObject = json.getAsJsonObject(); @SuppressWarnings("unchecked") Class<? extends GeneratedMessage> protoClass = (Class<? extends GeneratedMessage>) typeOfT; try {//from w w w. ja va2 s . c o m // Invoke the ProtoClass.newBuilder() method Object protoBuilder = getCachedMethod(protoClass, "newBuilder").invoke(null); Class<?> builderClass = protoBuilder.getClass(); Descriptor protoDescriptor = (Descriptor) getCachedMethod(protoClass, "getDescriptor").invoke(null); // Call setters on all of the available fields for (FieldDescriptor fieldDescriptor : protoDescriptor.getFields()) { String name = fieldDescriptor.getName(); if (jsonObject.has(name)) { JsonElement jsonElement = jsonObject.get(name); String fieldName = camelCaseField(name + "_"); Field field = protoClass.getDeclaredField(fieldName); Type fieldType = field.getGenericType(); if (fieldType.equals(Object.class)) { // TODO(drt24): this is very evil. // In NigoriMessages protobuf strings are stored in a field of type Object so that they // can use either String of ByteString as the implementation, however this causes a type // error when calling the set method. So we make a (potentially false) assumption that // all fields of type Object in NigoriMessages have that type because they actually // should have Strings set. fieldType = String.class; } Object fieldValue = context.deserialize(jsonElement, fieldType); if (fieldDescriptor.getJavaType() == FieldDescriptor.JavaType.ENUM) { Method methodVD = getCachedMethod(fieldValue.getClass(), "getValueDescriptor"); fieldValue = methodVD.invoke(fieldValue); } Method method = getCachedMethod(builderClass, "setField", FieldDescriptor.class, Object.class); method.invoke(protoBuilder, fieldDescriptor, fieldValue); } } // Invoke the build method to return the final proto return (GeneratedMessage) getCachedMethod(builderClass, "build").invoke(protoBuilder); } catch (SecurityException e) { throw new JsonParseException(e); } catch (NoSuchMethodException e) { throw new JsonParseException(e); } catch (IllegalArgumentException e) { throw new JsonParseException(e); } catch (IllegalAccessException e) { throw new JsonParseException(e); } catch (InvocationTargetException e) { throw new JsonParseException(e); } catch (NoSuchFieldException e) { throw new JsonParseException(e); } }