List of usage examples for com.google.gson JsonParseException JsonParseException
public JsonParseException(Throwable cause)
From source file:org.rapla.server.jsonrpc.CallDeserializer.java
License:Apache License
@Override public CallType deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException, NoSuchRemoteMethodException { if (!json.isJsonObject()) { throw new JsonParseException("Expected object"); }/*from w ww. j a v a 2s.c o m*/ 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:org.rapla.server.jsonrpc.JsonServlet.java
License:Apache License
private String readBody(final ActiveCall call) throws IOException { if (!isBodyJson(call)) { throw new JsonParseException("Invalid Request Content-Type"); }/*from w w w.j a v a2 s . c o 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(JsonConstants.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:org.restexpress.plugin.gson.json.GsonTimepointSerializer.java
License:Apache License
@Override public Date deserialize(final JsonElement json, final Type typeOf, final JsonDeserializationContext context) throws JsonParseException { try {//w w w .j a v a 2s. c o m return HttpDateTimeFormat.parseAny(json.getAsJsonPrimitive().getAsString()); } catch (final ParseException e) { throw new JsonParseException(e); } }
From source file:org.scassandra.http.client.types.GsonVariableMatchDeserialiser.java
License:Apache License
@Override public MultiPrimeRequest.VariableMatch deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject jsonObject = (JsonObject) json; String type = jsonObject.get("type").getAsString(); if (type.equalsIgnoreCase("any")) { return new MultiPrimeRequest.AnyMatch(); } else if (type.equalsIgnoreCase("exact")) { String matcher = jsonObject.get("matcher").getAsString(); return new MultiPrimeRequest.ExactMatch(matcher); } else {/*from w w w . j a v a 2 s.com*/ throw new JsonParseException("Unexpected variable matcher type received: " + type); } }
From source file:org.spongepowered.common.mixin.core.text.MixinChatComponentSerializer.java
License:MIT License
@Inject(method = "deserialize(Lcom/google/gson/JsonElement;Ljava/lang/reflect/Type;Lcom/google/gson/JsonDeserializationContext;)Lnet/minecraft/util/IChatComponent;", at = @At(value = "INVOKE", target = "Lcom/google/gson/JsonElement;getAsJsonObject()Lcom/google/gson/JsonObject;", ordinal = 0, shift = Shift.BY, by = 3, remap = false), locals = LocalCapture.CAPTURE_FAILHARD, cancellable = true) public void deserializePlaceholder(JsonElement jsonElement, Type type, JsonDeserializationContext context, CallbackInfoReturnable<IChatComponent> cir, JsonObject jsonObject) { if (jsonObject.has("placeholderKey")) { final String placeholderKey = jsonObject.get("placeholderKey").getAsString(); final ChatComponentPlaceholder deserialized = new ChatComponentPlaceholder(placeholderKey); // Default stuff - START if (jsonObject.has("extra")) { JsonArray extraElements = jsonObject.getAsJsonArray("extra"); if (extraElements.size() == 0) { throw new JsonParseException("Unexpected empty array of components"); }//from ww w . j a va 2 s . co m // Sponge -- strip out the first extra element if it has been added to the extra (for vanilla compat) boolean first = true; for (JsonElement extraElement : extraElements) { if (first && jsonObject.has("fallbackAsExtra")) { deserialized.setFallback(this.deserialize(extraElement, type, context)); } else { deserialized.appendSibling(this.deserialize(extraElement, type, context)); } first = false; } } deserialized.setChatStyle(context.<ChatStyle>deserialize(jsonElement, ChatStyle.class)); // Default stuff - END cir.setReturnValue(deserialized); } }
From source file:org.spongepowered.plugin.meta.gson.ModMetadataAdapter.java
License:MIT License
@Override public PluginMetadata read(JsonReader in) throws IOException { in.beginObject();/* ww w . ja va 2 s .c om*/ final Set<String> processedKeys = new HashSet<>(); final PluginMetadata result = new PluginMetadata("unknown"); String id = null; while (in.hasNext()) { final String name = in.nextName(); if (!processedKeys.add(name)) { throw new JsonParseException("Duplicate key '" + name + "' in " + in); } switch (name) { case "modid": id = in.nextString(); result.setId(id); break; case "name": result.setName(in.nextString()); break; case "version": result.setVersion(in.nextString()); break; case "description": result.setDescription(in.nextString()); break; case "url": result.setUrl(in.nextString()); break; case "authorList": in.beginArray(); while (in.hasNext()) { result.addAuthor(in.nextString()); } in.endArray(); break; case "requiredMods": in.beginArray(); while (in.hasNext()) { result.addRequiredDependency(ModDependencyAdapter.INSTANCE.read(in)); } in.endArray(); break; case "dependencies": in.beginArray(); while (in.hasNext()) { result.loadAfter(ModDependencyAdapter.INSTANCE.read(in)); } in.endArray(); break; case "dependants": in.beginArray(); while (in.hasNext()) { result.loadBefore(ModDependencyAdapter.INSTANCE.read(in)); } in.endArray(); break; default: result.setExtension(name, this.gson.fromJson(in, getExtension(name))); } } in.endObject(); if (id == null) { throw new JsonParseException("Mod metadata is missing required element 'modid'"); } return result; }
From source file:org.sprintapi.hyperdata.gson.GsonHyperDataDeserializer.java
License:Apache License
@Override public HyperMap deserialize(JsonElement json, Type arg1, JsonDeserializationContext ctx) throws JsonParseException { if (!json.isJsonObject()) { throw new JsonParseException( "Unexpected " + json.getClass().getSimpleName() + ", expected JsonObject object."); }//from w w w.j a va 2 s .c o m JsonObject jsonObject = json.getAsJsonObject(); HyperHashMap hdata = null; if (jsonObject != null) { hdata = new HyperHashMap(); final Set<Entry<String, JsonElement>> entrySet = jsonObject.entrySet(); if (entrySet != null) { Map<String, Object> metadata = null; for (Entry<String, JsonElement> entry : entrySet) { if (entry.getKey().startsWith(Constants.META_CHAR)) { if (metadata == null) { metadata = new HashMap<String, Object>(); } JsonElement value = entry.getValue(); if (value != null) { metadata.put(entry.getKey().substring(1), ctx.deserialize(value, (value.isJsonObject() && value.getAsJsonObject().has(Constants.META_CHAR.concat(HREF_KEY))) ? HyperMap.class : Object.class)); } } else { JsonElement value = entry.getValue(); if (value != null) { hdata.put(entry.getKey(), ctx.deserialize(value, (value.isJsonObject() && value.getAsJsonObject().has(Constants.META_CHAR.concat(HREF_KEY))) ? HyperMap.class : Object.class)); } } } hdata.setMetadata(metadata); } } return hdata; }
From source file:org.sweble.wom3.serialization.Wom3JsonTypeAdapterBase.java
License:Open Source License
protected static Scope registerNsDecl(ScopeStack scopeStack, Scope scope, String entryName, JsonElement entryValue) {/*from w w w. j a v a 2 s .c o m*/ if (!entryValue.isJsonPrimitive()) throw new JsonParseException("Expected attribute '" + entryName + "' to be a string"); entryName = entryName.substring(1); String valueString = entryValue.getAsString(); if (entryName.equals(XMLNS_PREFIX)) { if (scope == null) scope = scopeStack.push(); scope.setXmlns(valueString); } else if (entryName.startsWith(XMLNS_COLON_PREFIX)) { String prefix = entryName.substring(XMLNS_COLON_PREFIX.length()); if (scope == null) scope = scopeStack.push(); scope.put(prefix, valueString); } return scope; }
From source file:org.sweble.wom3.serialization.Wom3JsonTypeAdapterBase.java
License:Open Source License
protected static void parseAttribute(ScopeStack scopeStack, Element elem, String entryName, JsonElement entryValue) {/*from ww w. jav a2 s.c om*/ if (!entryValue.isJsonPrimitive()) throw new JsonParseException("Expected attribute '" + entryName + "' to be a string"); entryName = entryName.substring(1); String valueString = entryValue.getAsString(); int i = entryName.indexOf(':'); if (i == -1) { if (XMLNS_PREFIX.equals(entryName)) { elem.setAttributeNS(XMLNS_URI, entryName, valueString); } else { elem.setAttribute(entryName, valueString); } } else { String prefix = entryName.substring(0, i); if (XMLNS_PREFIX.equals(prefix)) { elem.setAttributeNS(XMLNS_URI, entryName, valueString); } else { String nsUri = scopeStack.getNsUriForPrefix(prefix); if (nsUri == null) throw new NamespaceException("Namespace URI for prefix '" + prefix + "' not declared"); elem.setAttributeNS(nsUri, entryName, valueString); } } }
From source file:org.sweble.wom3.serialization.Wom3NodeCompactJsonTypeAdapter.java
License:Open Source License
private static Node fromJson(Wom3Document doc, JsonElement json, ScopeStack scopeStack) { if (json.isJsonPrimitive()) return doc.createTextNode(json.getAsString()); // Get node object if (!json.isJsonObject()) throw new JsonParseException("Expected JsonObject or JsonPrimitive"); JsonObject o = json.getAsJsonObject(); Scope scope = null;/*from w ww.ja v a 2 s . c om*/ String elemQName = null; ValueTypes valueType = null; JsonElement nodeValue = null; // Get node type Set<Entry<String, JsonElement>> entries = o.entrySet(); for (Entry<String, JsonElement> e : entries) { String entryName = e.getKey(); final JsonElement entryValue = e.getValue(); if (entryName.startsWith(ELEMENT_NAME_PREFIX)) { if (elemQName != null) throw new JsonParseException("Node name field occurred repeatedly"); elemQName = entryName.substring(1); nodeValue = entryValue; } else if (entryName.startsWith(SPECIAL_TYPE_PREFIX)) { if (valueType != null) throw new JsonParseException("Type name field occurred repeatedly"); if (entryName.equals(TYPE_RTD)) { valueType = ValueTypes.RTD; } else if (entryName.equals(TYPE_TEXT)) { valueType = ValueTypes.TEXT; } else if (entryName.equals(TYPE_ENTITY_REF)) { valueType = ValueTypes.ENTITY_REF; } else if (entryName.equals(TYPE_COMMENT)) { valueType = ValueTypes.COMMENT; } else if (entryName.equals(TYPE_CDATA)) { valueType = ValueTypes.CDATA; } else throw new JsonParseException("Unknown special type '" + entryName + "'"); nodeValue = entryValue; } else if (entryName.startsWith(ATTRIBUTE_PREFIX)) { scope = registerNsDecl(scopeStack, scope, entryName, entryValue); } else throw new JsonParseException("Unexpected field: '" + entryName + "'"); } Element elem = null; String defaultNsUri = null; if (elemQName != null) { defaultNsUri = scopeStack.getXmlns(); elem = createElement(doc, scopeStack, defaultNsUri, elemQName); } else if (valueType == null) throw new JsonParseException("Missing type name or node name"); for (Entry<String, JsonElement> e : entries) { String entryName = e.getKey(); JsonElement entryValue = e.getValue(); if (entryName.startsWith(ATTRIBUTE_PREFIX)) { if (elem == null) throw new JsonParseException("Value type '" + valueType + "' cannot have attributes"); parseAttribute(scopeStack, elem, entryName, entryValue); } } Node result; if (elem != null) { if (!nodeValue.isJsonArray()) throw new JsonParseException( "Expected member '" + ELEMENT_NAME_PREFIX + elemQName + "' to be an array."); JsonArray children = nodeValue.getAsJsonArray(); for (JsonElement child : children) elem.appendChild(fromJson(doc, child, scopeStack)); result = elem; } else { result = valueType.create(doc, nodeValue.getAsString()); } if (scope != null) scopeStack.pop(); return result; }