Example usage for com.google.gson JsonParseException JsonParseException

List of usage examples for com.google.gson JsonParseException JsonParseException

Introduction

In this page you can find the example usage for com.google.gson JsonParseException JsonParseException.

Prototype

public JsonParseException(Throwable cause) 

Source Link

Document

Creates exception with the specified cause.

Usage

From source file:com.thoughtworks.go.api.representers.ConfigurationPropertyRepresenter.java

License:Apache License

public static ConfigurationProperty fromJSON(JsonReader jsonReader) {
    try {//from   ww  w  .  j  ava  2  s  .  c  o m
        String key = jsonReader.getString("key");
        String value = jsonReader.optString("value").orElse(null);
        String encryptedValue = jsonReader.optString("encrypted_value").orElse(null);
        return new ConfigurationProperty().deserialize(key, value, encryptedValue);
    } catch (Exception e) {
        throw new JsonParseException("Could not parse configuration property");
    }
}

From source file:com.thoughtworks.go.api.util.GsonTransformer.java

License:Apache License

public JsonReader jsonReaderFrom(String string) {
    try {/*from   w w  w  .  ja va  2  s  .  c  o m*/
        if (StringUtils.isBlank(string)) {
            string = "{}";
        }
        return new JsonReader(GSON.fromJson(string, JsonElement.class).getAsJsonObject());
    } catch (Exception e) {
        throw new JsonParseException(e);
    }
}

From source file:com.thoughtworks.go.api.util.GsonTransformer.java

License:Apache License

public JsonReader jsonReaderFrom(Map map) {
    try {/*w w w . jav a  2 s  .  c om*/
        return new JsonReader(GSON.toJsonTree(map).getAsJsonObject());
    } catch (Exception e) {
        throw new JsonParseException(e);
    }
}

From source file:com.thoughtworks.go.apiv1.admin.security.representers.ConfigurationPropertyRepresenter.java

License:Apache License

public static ConfigurationProperty fromJSON(JsonReader jsonReader) {
    try {/*from   w ww  . ja  va 2 s.c  o m*/
        String key = jsonReader.getString("key");
        String value = jsonReader.optString("value").orElse(null);
        String encryptedValue = jsonReader.optString("encrypted_value").orElse(null);
        return ConfigurationProperty.deserialize(key, value, encryptedValue);
    } catch (Exception e) {
        throw new JsonParseException("Could not parse configuration property");
    }
}

From source file:com.thoughtworks.go.apiv1.admin.security.representers.RoleRepresenter.java

License:Apache License

public static Role fromJSON(JsonReader jsonReader) {
    Role model;//from w  w  w. j  a  v  a 2 s. c o m
    String type = jsonReader.optString("type").orElse("");

    if ("gocd".equals(type)) {
        model = GoCDRoleConfigRepresenter.fromJSON(jsonReader.readJsonObject("attributes"));
    } else if ("plugin".equals(type)) {
        model = PluginRoleConfigRepresenter.fromJSON(jsonReader.readJsonObject("attributes"));
    } else {
        throw new JsonParseException("Invalid role type '%s'. It has to be one of 'gocd' or 'plugin'");
    }

    model.setName(new CaseInsensitiveString(jsonReader.optString("name").orElse(null)));

    return model;
}

From source file:com.thoughtworks.go.plugin.access.configrepo.codec.ArtifactTypeAdapter.java

License:Apache License

@Override
protected Class<?> classForName(String typeName, String origin) {
    if (typeName.equals("external"))
        return CRPluggableArtifact.class;
    if (typeName.equals("build") || typeName.equals("test"))
        return CRBuiltInArtifact.class;
    else/*www.j  av  a  2 s  . c  om*/
        throw new JsonParseException(String.format("Invalid or unknown task type '%s'", typeName));
}

From source file:com.thoughtworks.go.plugin.configrepo.codec.MaterialTypeAdapter.java

License:Apache License

@Override
protected Class<?> classForName(String typeName, String origin) {
    if (typeName.equals(CRDependencyMaterial.TYPE_NAME))
        return CRDependencyMaterial.class;
    if (typeName.equals(CRPackageMaterial.TYPE_NAME))
        return CRPackageMaterial.class;
    if (typeName.equals(CRPluggableScmMaterial.TYPE_NAME))
        return CRPluggableScmMaterial.class;
    if (typeName.equals(CRGitMaterial.TYPE_NAME))
        return CRGitMaterial.class;
    if (typeName.equals(CRHgMaterial.TYPE_NAME))
        return CRHgMaterial.class;
    if (typeName.equals(CRSvnMaterial.TYPE_NAME))
        return CRSvnMaterial.class;
    if (typeName.equals(CRP4Material.TYPE_NAME))
        return CRP4Material.class;
    if (typeName.equals(CRTfsMaterial.TYPE_NAME))
        return CRTfsMaterial.class;
    if (typeName.equals(CRConfigMaterial.TYPE_NAME))
        return CRConfigMaterial.class;
    else//ww w. j a  va  2  s  . c  o  m
        throw new JsonParseException(String.format("Invalid or unknown material type '%s'", typeName));
}

From source file:com.thoughtworks.go.plugin.configrepo.codec.TaskTypeAdapter.java

License:Apache License

@Override
protected Class<?> classForName(String typeName, String origin) {
    if (typeName.equals(CRExecTask.TYPE_NAME))
        return CRExecTask.class;
    if (typeName.equals(CRBuildFramework.rake.toString()))
        return CRBuildTask.class;
    if (typeName.equals(CRBuildFramework.ant.toString()))
        return CRBuildTask.class;
    if (typeName.equals(CRBuildFramework.nant.toString()))
        return CRNantTask.class;
    if (typeName.equals(CRPluggableTask.TYPE_NAME))
        return CRPluggableTask.class;

    if (typeName.equals(CRAbstractFetchTask.TYPE_NAME)) {
        return CRAbstractFetchTask.ArtifactOrigin.getArtifactOrigin(origin).getArtifactTaskClass();
    }/*from www .j a v a 2  s  . c  om*/

    throw new JsonParseException(String.format("Invalid or unknown task type '%s'.", typeName));
}

From source file:com.truethat.android.external.gson.RuntimeTypeAdapterFactory.java

License:Apache License

/**
 * This method was modified, to include {@link #typeFieldName} in serialized registered sub types.
 *//*from w w  w . j av  a2s. c o m*/
@Nullable
public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
    if (type.getRawType() != baseType && !labelToSubtype.containsKey(type.getRawType().getSimpleName())) {
        return null;
    }

    final Map<String, TypeAdapter<?>> labelToDelegate = new LinkedHashMap<>();
    final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate = new LinkedHashMap<>();
    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>() {
        @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);
            }
            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);
        }

        @Override
        public R read(JsonReader in) throws IOException {
            JsonElement jsonElement = Streams.parse(in);
            JsonElement labelJsonElement = jsonElement.getAsJsonObject().remove(typeFieldName);
            if (labelJsonElement == null) {
                throw new JsonParseException("cannot deserialize " + baseType
                        + " because it does not define a field named " + typeFieldName);
            }
            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);
        }
    }.nullSafe();
}

From source file:com.urswolfer.gerrit.client.rest.gson.DateDeserializer.java

License:Apache License

@Override
public Date deserialize(JsonElement jsonElement, Type type,
        JsonDeserializationContext jsonDeserializationContext) {
    String date = jsonElement.getAsString();
    try {/*  ww w  .j  a va 2 s  . c o  m*/
        return DATE_FORMAT.get().parse(date);
    } catch (ParseException e) {
        throw new JsonParseException(e);
    }
}