Search in sources :

Example 76 with GsonBuilder

use of com.google.gson.GsonBuilder in project Geolosys by oitsjustjose.

the class Geolosys method getOresConfig.

@Nonnull
private ConfigOres getOresConfig(File configDir) {
    try {
        FileReader fr = new FileReader(configDir.getAbsolutePath() + "/geolosys_ores.json".replace("/", File.separator));
        BufferedReader br = new BufferedReader(fr);
        String line;
        StringBuilder json = new StringBuilder();
        try {
            while ((line = br.readLine()) != null) {
                json.append(line);
            }
            Gson gson = new GsonBuilder().setPrettyPrinting().create();
            ConfigOres ret = gson.fromJson(json.toString(), ConfigOres.class);
            ret.validate(configDir);
            return ret;
        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (FileNotFoundException e) {
        ConfigOres props = new ConfigOres();
        props.populateConfigs();
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String json = gson.toJson(props);
        try {
            FileWriter fw = new FileWriter(configDir.getAbsolutePath() + "/geolosys_ores.json".replace("/", File.separator));
            fw.write(json);
            fw.close();
            return props;
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    return getOresConfig(configDir);
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) Nonnull(javax.annotation.Nonnull)

Example 77 with GsonBuilder

use of com.google.gson.GsonBuilder in project linuxtools by eclipse.

the class OSIORestPatchUpdateTask method addHttpRequestEntities.

@SuppressWarnings("deprecation")
@Override
protected void addHttpRequestEntities(HttpRequestBase request) throws OSIORestException {
    super.addHttpRequestEntities(request);
    try {
        Gson gson = new GsonBuilder().registerTypeAdapter(OldAttributes.class, new TaskAttributeTypeAdapter(getClient().getLocation())).create();
        StringEntity requestEntity = new StringEntity(gson.toJson(oldAttributes));
        ((HttpPatch) request).setEntity(requestEntity);
    } catch (UnsupportedEncodingException e) {
        Throwables.propagate(new CoreException(// $NON-NLS-1$
        new Status(IStatus.ERROR, OSIORestCore.ID_PLUGIN, "Can not build HttpRequest", e)));
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) StringEntity(org.apache.http.entity.StringEntity) CoreException(org.eclipse.core.runtime.CoreException) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpPatch(org.apache.http.client.methods.HttpPatch)

Example 78 with GsonBuilder

use of com.google.gson.GsonBuilder in project linuxtools by eclipse.

the class OSIORestPostNewCommentTask method addHttpRequestEntities.

@SuppressWarnings("deprecation")
@Override
protected void addHttpRequestEntities(HttpRequestBase request) throws OSIORestException {
    super.addHttpRequestEntities(request);
    try {
        Gson gson = new GsonBuilder().registerTypeAdapter(OldAttributes.class, new TaskAttributeTypeAdapter(getClient().getLocation())).create();
        StringEntity requestEntity = new StringEntity(gson.toJson(oldAttributes));
        ((HttpPost) request).setEntity(requestEntity);
    } catch (UnsupportedEncodingException e) {
        Throwables.propagate(new CoreException(// $NON-NLS-1$
        new Status(IStatus.ERROR, OSIORestCore.ID_PLUGIN, "Can not build HttpRequest", e)));
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) StringEntity(org.apache.http.entity.StringEntity) HttpPost(org.apache.http.client.methods.HttpPost) CoreException(org.eclipse.core.runtime.CoreException) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 79 with GsonBuilder

use of com.google.gson.GsonBuilder in project linuxtools by eclipse.

the class OSIORestPostNewLabelTask method addHttpRequestEntities.

@SuppressWarnings("deprecation")
@Override
protected void addHttpRequestEntities(HttpRequestBase request) throws OSIORestException {
    super.addHttpRequestEntities(request);
    try {
        Gson gson = new GsonBuilder().registerTypeAdapter(String.class, new TaskAttributeTypeAdapter()).create();
        StringEntity requestEntity = new StringEntity(gson.toJson(newLabel));
        ((HttpPost) request).setEntity(requestEntity);
    } catch (UnsupportedEncodingException e) {
        Throwables.propagate(new CoreException(// $NON-NLS-1$
        new Status(IStatus.ERROR, OSIORestCore.ID_PLUGIN, "Can not build HttpRequest", e)));
    }
}
Also used : HttpStatus(org.apache.http.HttpStatus) Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) StringEntity(org.apache.http.entity.StringEntity) HttpPost(org.apache.http.client.methods.HttpPost) CoreException(org.eclipse.core.runtime.CoreException) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 80 with GsonBuilder

use of com.google.gson.GsonBuilder in project linuxtools by eclipse.

the class OSIORestPostNewTask method addHttpRequestEntities.

@SuppressWarnings("deprecation")
@Override
protected void addHttpRequestEntities(HttpRequestBase request) throws OSIORestException {
    super.addHttpRequestEntities(request);
    try {
        Gson gson = new GsonBuilder().registerTypeAdapter(TaskData.class, new TaskAttributeTypeAdapter(getClient().getLocation())).create();
        StringEntity requestEntity = new StringEntity(gson.toJson(taskData));
        ((HttpPost) request).setEntity(requestEntity);
    } catch (UnsupportedEncodingException e) {
        Throwables.propagate(new CoreException(// $NON-NLS-1$
        new Status(IStatus.ERROR, OSIORestCore.ID_PLUGIN, "Can not build HttpRequest", e)));
    }
}
Also used : HttpStatus(org.apache.http.HttpStatus) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) StringEntity(org.apache.http.entity.StringEntity) HttpPost(org.apache.http.client.methods.HttpPost) CoreException(org.eclipse.core.runtime.CoreException) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) UnsupportedEncodingException(java.io.UnsupportedEncodingException) TaskData(org.eclipse.mylyn.tasks.core.data.TaskData)

Aggregations

GsonBuilder (com.google.gson.GsonBuilder)1067 Gson (com.google.gson.Gson)803 IOException (java.io.IOException)185 Test (org.junit.Test)141 ArrayList (java.util.ArrayList)101 JsonObject (com.google.gson.JsonObject)90 File (java.io.File)80 JsonElement (com.google.gson.JsonElement)78 HashMap (java.util.HashMap)67 List (java.util.List)62 Map (java.util.Map)59 Retrofit (retrofit2.Retrofit)56 Type (java.lang.reflect.Type)52 FileNotFoundException (java.io.FileNotFoundException)42 TypeToken (com.google.gson.reflect.TypeToken)40 ResponseBody (okhttp3.ResponseBody)39 FileOutputStream (java.io.FileOutputStream)38 Call (retrofit2.Call)38 JsonSyntaxException (com.google.gson.JsonSyntaxException)37 JsonParser (com.google.gson.JsonParser)36