List of usage examples for com.google.gson JsonParseException JsonParseException
public JsonParseException(Throwable cause)
From source file:com.github.autermann.matlab.json.MatlabValueSerializer.java
License:Open Source License
private MatlabType getType(JsonObject json) throws JsonParseException { String type = json.get(MatlabJSONConstants.TYPE).getAsString(); try {//from ww w. j av a2s . co m return MatlabType.fromString(type); } catch (IllegalArgumentException e) { throw new JsonParseException("Unknown type: " + type); } }
From source file:com.github.kyriosdata.regras.infraestrutura.CustomRegraTypeAdapterFactory.java
License:Creative Commons License
@Override public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) { if (type.getRawType() != Regra.class) return null; final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type); return new TypeAdapter<T>() { @Override/*from w ww . j a va 2 s. c om*/ public void write(final JsonWriter jsonWriter, final T t) throws IOException { jsonWriter.beginObject(); jsonWriter.name("tipo"); jsonWriter.value(getTipo(t.getClass().getName())); jsonWriter.name("obj"); delegate.write(jsonWriter, t); jsonWriter.endObject(); } @Override public T read(final JsonReader jsonReader) throws IOException, JsonParseException { JsonElement tree = Streams.parse(jsonReader); JsonObject object = tree.getAsJsonObject(); String clazz = object.get("tipo").getAsString(); JsonElement obj = object.get("obj").getAsJsonObject(); if ("expressao".equals(clazz)) { return (T) gson.getDelegateAdapter(CustomRegraTypeAdapterFactory.this, TypeToken.get(RegraExpressao.class)).fromJsonTree(obj); } if ("pontosPorRelato".equals(clazz)) { return (T) gson.getDelegateAdapter(CustomRegraTypeAdapterFactory.this, TypeToken.get(RegraPontosPorRelato.class)).fromJsonTree(obj); } throw new JsonParseException("Erro ao desserializar " + type + ". No uma regra?"); } }; }
From source file:com.github.sheigutn.pushbullet.gson.RuntimeTypeAdapterFactory.java
License:Apache License
public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) { if (type.getRawType() != baseType) { return null; }//ww w.ja v a 2 s . c o m if (baseDelegateAdapter == null) { baseDelegateAdapter = gson.getDelegateAdapter(RuntimeTypeAdapterFactory.this, TypeToken.get(baseType)); } final Map<String, TypeAdapter<?>> labelToDelegate = new LinkedHashMap<String, TypeAdapter<?>>(); final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate = new LinkedHashMap<Class<?>, TypeAdapter<?>>(); for (Map.Entry<String, Class<?>> entry : labelToSubtype.entrySet()) { TypeAdapter<?> delegate = gson.getDelegateAdapter(this, TypeToken.get(entry.getValue())); labelToDelegate.put(entry.getKey(), delegate); subtypeToDelegate.put(entry.getValue(), delegate); } return new TypeAdapter<R>() { @SuppressWarnings("unchecked") @Override public R read(JsonReader in) throws IOException { JsonElement jsonElement = Streams.parse(in); JsonElement labelJsonElement = jsonElement.getAsJsonObject().get(typeFieldName); //remove(typeFieldName) changed to get(typeFieldName) if (labelJsonElement == null) { return (R) baseDelegateAdapter.fromJsonTree(jsonElement); } String label = labelJsonElement.getAsString(); @SuppressWarnings("unchecked") // registration requires that subtype extends T TypeAdapter<R> delegate = (TypeAdapter<R>) labelToDelegate.get(label); if (delegate == null) { throw new JsonParseException("cannot deserialize " + baseType + " subtype named " + label + "; did you forget to register a subtype?"); } return delegate.fromJsonTree(jsonElement); } @Override public void write(JsonWriter out, R value) throws IOException { Class<?> srcType = value.getClass(); String label = subtypeToLabel.get(srcType); @SuppressWarnings("unchecked") // registration requires that subtype extends T TypeAdapter<R> delegate = (TypeAdapter<R>) subtypeToDelegate.get(srcType); if (delegate == null) { throw new JsonParseException( "cannot serialize " + srcType.getName() + "; did you forget to register a subtype?"); } JsonObject jsonObject = delegate.toJsonTree(value).getAsJsonObject(); //if (jsonObject.has(typeFieldName)) { // throw new JsonParseException("cannot serialize " + srcType.getName() // + " because it already defines a field named " + typeFieldName); <-- Commented //} JsonObject clone = new JsonObject(); clone.add(typeFieldName, new JsonPrimitive(label)); for (Map.Entry<String, JsonElement> e : jsonObject.entrySet()) { clone.add(e.getKey(), e.getValue()); } Streams.write(clone, out); } }.nullSafe(); }
From source file:com.goide.dlv.JsonReaderEx.java
License:Apache License
/** * Throws a new IO exception with the given message and a context snippet * with this reader's content.//w ww. ja va2 s . co m */ @NotNull private JsonParseException createParseError(String message) { throw new JsonParseException(message + " at line " + getLineNumber() + " column " + getColumnNumber()); }
From source file:com.google.belay.server.CapabilityAdapter.java
License:Open Source License
@Override public Capability deserialize(JsonElement elem, Type typeOfSrc, JsonDeserializationContext context) throws JsonParseException { JsonObject obj = elem.getAsJsonObject(); JsonElement capElem = obj.get("@"); if (capElem == null) { throw new JsonParseException("capability object missing '@' field"); }/*ww w. j a v a 2s.c om*/ String capUrlStr = capElem.getAsString(); if (capUrlStr == null) { throw new JsonParseException("'@' field is not a string"); } try { URL capUrl = new URL(capUrlStr); return new Capability(capUrl); } catch (MalformedURLException e) { throw new JsonParseException("'@' field does not map to a valid url"); } }
From source file:com.google.cloud.tools.eclipse.appengine.deploy.AppEngineDeployOutput.java
License:Apache License
/** * Parse the raw JSON output of the deployment. * * @return the output of gcloud app deploy * @throws JsonParseException if unable to extract the deploy output information needed *//* w w w . ja va2s . c om*/ public static AppEngineDeployOutput parse(String jsonOutput) throws JsonParseException { AppEngineDeployOutput deployOutput = new Gson().fromJson(jsonOutput, AppEngineDeployOutput.class); if (deployOutput == null || deployOutput.versions == null || deployOutput.versions.size() != 1) { throw new JsonParseException("Cannot get app version: unexpected gcloud JSON output format"); } return deployOutput; }
From source file:com.google.cloud.tools.intellij.appengine.cloud.AppEngineDeploy.java
License:Apache License
/** * Parse the raw json output of the deployment. * * @return an object modeling the output of a deploy command * @throws JsonParseException if unable to extract the deploy output information needed *//* www .j a va 2 s . c o m*/ @VisibleForTesting static DeployOutput parseDeployOutput(String jsonOutput) throws JsonParseException { Type deployOutputType = new TypeToken<DeployOutput>() { }.getType(); DeployOutput deployOutput = new Gson().fromJson(jsonOutput, deployOutputType); if (deployOutput == null || deployOutput.versions == null || deployOutput.versions.size() != 1) { throw new JsonParseException("Cannot get app version: unexpected gcloud JSON output format"); } return deployOutput; }
From source file:com.google.devtools.moe.client.gson.GsonUtil.java
License:Apache License
@SuppressWarnings("unchecked") // Type is assured by gson's context.deserialize method. public static <T> T getPropertyOrLegacy(JsonDeserializationContext context, Class<T> type, JsonElement json, String elementName, String legacyName) { JsonObject obj = json.getAsJsonObject(); JsonElement element = obj.get(legacyName); if (element == null) { element = obj.get(elementName);//from w ww . j ava 2 s. c om } if (element == null) { throw new JsonParseException(type.getSimpleName() + " is missing a " + elementName); } return (T) context.deserialize(element, type); }
From source file:com.google.gdt.googleapi.core.ApiDirectoryListingJsonCodec.java
License:Open Source License
protected void populateApiInfoFromJson(URL baseURL, JsonObject object, MutableApiInfo info) { if (object.has("name")) { info.setName(object.get("name").getAsString()); }/*w ww.jav a 2 s . c om*/ if (object.has("version")) { info.setVersion(object.get("version").getAsString()); } if (object.has("title")) { info.setDisplayName(object.get("title").getAsString()); } if (object.has("publisher")) { info.setPublisher(object.get("publisher").getAsString()); } if (object.has("description")) { info.setDescription(object.get("description").getAsString()); } if (object.has("icons")) { JsonObject iconLinks = object.getAsJsonObject("icons"); Set<Entry<String, JsonElement>> iconLinksEntrySet = iconLinks.entrySet(); for (Entry<String, JsonElement> entry : iconLinksEntrySet) { try { info.putIconLink(entry.getKey(), new URL(baseURL, entry.getValue().getAsString())); } catch (MalformedURLException e) { // TODO Add logging warn } } } if (object.has("labels")) { JsonArray labelsJsonArray = object.getAsJsonArray("labels"); for (JsonElement labelElement : labelsJsonArray) { info.addLabel(labelElement.getAsString()); } } if (object.has("releaseDate")) { try { LocalDate date = new LocalDate(object.get("releaseDate").getAsString()); info.setReleaseDate(date); } catch (IllegalArgumentException e) { throw new JsonParseException(e); } } if (object.has("releaseNotesLink")) { try { info.setReleaseNotesLink(new URL(baseURL, object.get("releaseNotesLink").getAsString())); } catch (MalformedURLException e) { // TODO Add logging warn } } if (object.has("ranking")) { info.setRanking(object.get("ranking").getAsInt()); } if (object.has("discoveryLink")) { try { info.setDiscoveryLink(new URL(baseURL, object.get("discoveryLink").getAsString())); } catch (MalformedURLException e) { // TODO Add logging warn } } if (object.has("documentationLink")) { try { info.setDocumentationLink(new URL(baseURL, object.get("documentationLink").getAsString())); } catch (MalformedURLException e) { // TODO Add logging warn } } if (object.has("downloadLink")) { try { info.setDownloadLink(new URL(baseURL, object.get("downloadLink").getAsString())); } catch (MalformedURLException e) { // TODO Add logging warn } } if (object.has("tosLink")) { try { info.setTosLink(new URL(baseURL, object.get("tosLink").getAsString())); } catch (MalformedURLException e) { // TODO Add logging warn } } }
From source file:com.google.gerrit.server.events.EventDeserializer.java
License:Apache License
@Override public Event deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (!json.isJsonObject()) { throw new JsonParseException("Not an object"); }//from w ww .ja v a 2s. c o m JsonElement typeJson = json.getAsJsonObject().get("type"); if (typeJson == null || !typeJson.isJsonPrimitive() || !typeJson.getAsJsonPrimitive().isString()) { throw new JsonParseException("Type is not a string: " + typeJson); } String type = typeJson.getAsJsonPrimitive().getAsString(); Class<?> cls = EventTypes.getClass(type); if (cls == null) { throw new JsonParseException("Unknown event type: " + type); } return context.deserialize(json, cls); }