Search in sources :

Example 6 with GsonBuilder

use of com.google.gson.GsonBuilder in project zeppelin by apache.

the class MongoNotebookRepo method noteToDocument.

/**
   * Convert note to document
   */
private Document noteToDocument(Note note) {
    // note to JSON
    Gson gson = new GsonBuilder().create();
    String json = gson.toJson(note);
    // JSON to document
    Document doc = Document.parse(json);
    // set object id as note id
    doc.put("_id", note.getId());
    return doc;
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) Document(org.bson.Document)

Example 7 with GsonBuilder

use of com.google.gson.GsonBuilder in project zeppelin by apache.

the class S3NotebookRepo method getNote.

private Note getNote(String key) throws IOException {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setPrettyPrinting();
    Gson gson = gsonBuilder.registerTypeAdapter(Date.class, new NotebookImportDeserializer()).create();
    S3Object s3object;
    try {
        s3object = s3client.getObject(new GetObjectRequest(bucketName, key));
    } catch (AmazonClientException ace) {
        throw new IOException("Unable to retrieve object from S3: " + ace, ace);
    }
    Note note;
    try (InputStream ins = s3object.getObjectContent()) {
        String json = IOUtils.toString(ins, conf.getString(ConfVars.ZEPPELIN_ENCODING));
        note = gson.fromJson(json, Note.class);
    }
    for (Paragraph p : note.getParagraphs()) {
        if (p.getStatus() == Status.PENDING || p.getStatus() == Status.RUNNING) {
            p.setStatus(Status.ABORT);
        }
    }
    return note;
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) InputStream(java.io.InputStream) AmazonClientException(com.amazonaws.AmazonClientException) Note(org.apache.zeppelin.notebook.Note) Gson(com.google.gson.Gson) S3Object(com.amazonaws.services.s3.model.S3Object) IOException(java.io.IOException) NotebookImportDeserializer(org.apache.zeppelin.notebook.NotebookImportDeserializer) GetObjectRequest(com.amazonaws.services.s3.model.GetObjectRequest) Date(java.util.Date) Paragraph(org.apache.zeppelin.notebook.Paragraph)

Example 8 with GsonBuilder

use of com.google.gson.GsonBuilder in project kickmaterial by byoutline.

the class GlobalModule method providesGson.

@Provides
Gson providesGson() {
    GsonBuilder builder = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
    JsonDeserializer<DateTime> deserializer = (json, typeOfT, context) -> new DateTime(json.getAsJsonPrimitive().getAsLong() * 1000);
    builder.registerTypeAdapter(DateTime.class, deserializer);
    return builder.create();
}
Also used : OttoObservableCachedFieldWithArgBuilder(com.byoutline.ottocachedfield.OttoObservableCachedFieldWithArgBuilder) Bus(com.squareup.otto.Bus) CachedField(com.byoutline.cachedfield.CachedField) GsonBuilder(com.google.gson.GsonBuilder) KickMaterialService(com.byoutline.kickmaterial.api.KickMaterialService) KickMaterialRequestInterceptor(com.byoutline.kickmaterial.api.KickMaterialRequestInterceptor) Picasso(com.squareup.picasso.Picasso) FieldNamingPolicy(com.google.gson.FieldNamingPolicy) Module(dagger.Module) ObservableCachedFieldWithArg(com.byoutline.observablecachedfield.ObservableCachedFieldWithArg) Gson(com.google.gson.Gson) RetrofitHelper.apiValueProv(com.byoutline.ibuscachedfield.util.RetrofitHelper.apiValueProv) GsonConverterFactory(retrofit2.converter.gson.GsonConverterFactory) OttoCachedFieldBuilder(com.byoutline.ottocachedfield.OttoCachedFieldBuilder) KickMaterialApp(com.byoutline.kickmaterial.KickMaterialApp) LruCacheWithPlaceholders(com.byoutline.kickmaterial.utils.LruCacheWithPlaceholders) Nullable(javax.annotation.Nullable) Provides(dagger.Provides) CachedFieldWithArg(com.byoutline.cachedfield.CachedFieldWithArg) com.byoutline.kickmaterial.events(com.byoutline.kickmaterial.events) AccessTokenProvider(com.byoutline.kickmaterial.managers.AccessTokenProvider) DateTime(org.joda.time.DateTime) LoginManager(com.byoutline.kickmaterial.managers.LoginManager) Retrofit(retrofit2.Retrofit) List(java.util.List) OkHttpClient(okhttp3.OkHttpClient) SharedPreferences(android.content.SharedPreferences) JsonDeserializer(com.google.gson.JsonDeserializer) com.byoutline.kickmaterial.model(com.byoutline.kickmaterial.model) OttoCachedFieldWithArgBuilder(com.byoutline.ottocachedfield.OttoCachedFieldWithArgBuilder) GsonBuilder(com.google.gson.GsonBuilder) DateTime(org.joda.time.DateTime) Provides(dagger.Provides)

Example 9 with GsonBuilder

use of com.google.gson.GsonBuilder in project weave by continuuity.

the class YarnWeavePreparer method saveLocalFiles.

/**
   * Serializes the list of files that needs to localize from AM to Container.
   */
private void saveLocalFiles(Map<String, LocalFile> localFiles, Set<String> includes) throws IOException {
    Map<String, LocalFile> localize = ImmutableMap.copyOf(Maps.filterKeys(localFiles, Predicates.in(includes)));
    LOG.debug("Create and copy {}", Constants.Files.LOCALIZE_FILES);
    Location location = createTempLocation(Constants.Files.LOCALIZE_FILES);
    Writer writer = new OutputStreamWriter(location.getOutputStream(), Charsets.UTF_8);
    try {
        new GsonBuilder().registerTypeAdapter(LocalFile.class, new LocalFileCodec()).create().toJson(localize.values(), new TypeToken<List<LocalFile>>() {
        }.getType(), writer);
    } finally {
        writer.close();
    }
    LOG.debug("Done {}", Constants.Files.LOCALIZE_FILES);
    localFiles.put(Constants.Files.LOCALIZE_FILES, createLocalFile(Constants.Files.LOCALIZE_FILES, location));
}
Also used : LocalFileCodec(com.continuuity.weave.internal.json.LocalFileCodec) LocalFile(com.continuuity.weave.api.LocalFile) DefaultLocalFile(com.continuuity.weave.internal.DefaultLocalFile) GsonBuilder(com.google.gson.GsonBuilder) TypeToken(com.google.common.reflect.TypeToken) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) Location(com.continuuity.weave.filesystem.Location)

Example 10 with GsonBuilder

use of com.google.gson.GsonBuilder in project cryptomator by cryptomator.

the class WelcomeController method checkForUpdates.

private void checkForUpdates() {
    checkForUpdatesStatus.setText(localization.getString("welcome.checkForUpdates.label.currentlyChecking"));
    checkForUpdatesIndicator.setVisible(true);
    asyncTaskService.asyncTaskOf(() -> {
        RequestConfig requestConfig = //
        RequestConfig.custom().setConnectTimeout(//
        5000).setConnectionRequestTimeout(//
        5000).setSocketTimeout(//
        5000).build();
        HttpClientBuilder httpClientBuilder = //
        HttpClients.custom().disableCookieManagement().setDefaultRequestConfig(//
        requestConfig).setUserAgent("Cryptomator VersionChecker/" + ApplicationVersion.orElse("SNAPSHOT"));
        LOG.debug("Checking for updates...");
        try (CloseableHttpClient client = httpClientBuilder.build()) {
            HttpGet request = new HttpGet("https://cryptomator.org/downloads/latestVersion.json");
            try (CloseableHttpResponse response = client.execute(request)) {
                if (response.getStatusLine().getStatusCode() == 200 && response.getEntity() != null) {
                    try (InputStream in = response.getEntity().getContent()) {
                        Gson gson = new GsonBuilder().setLenient().create();
                        Reader utf8Reader = new InputStreamReader(in, StandardCharsets.UTF_8);
                        Map<String, String> map = gson.fromJson(utf8Reader, new TypeToken<Map<String, String>>() {
                        }.getType());
                        if (map != null) {
                            this.compareVersions(map);
                        }
                    }
                }
            }
        }
    }).andFinally(() -> {
        checkForUpdatesStatus.setText("");
        checkForUpdatesIndicator.setVisible(false);
    }).run();
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) InputStreamReader(java.io.InputStreamReader) GsonBuilder(com.google.gson.GsonBuilder) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) Gson(com.google.gson.Gson) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) TypeToken(com.google.gson.reflect.TypeToken) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Aggregations

GsonBuilder (com.google.gson.GsonBuilder)222 Gson (com.google.gson.Gson)135 IOException (java.io.IOException)40 Type (java.lang.reflect.Type)24 ArrayList (java.util.ArrayList)24 Map (java.util.Map)24 JsonElement (com.google.gson.JsonElement)20 HashMap (java.util.HashMap)20 Test (org.junit.Test)18 JsonObject (com.google.gson.JsonObject)15 JsonSyntaxException (com.google.gson.JsonSyntaxException)13 Date (java.util.Date)13 List (java.util.List)13 Retrofit (retrofit2.Retrofit)13 InputStream (java.io.InputStream)11 OkHttpClient (okhttp3.OkHttpClient)11 File (java.io.File)9 InputStreamReader (java.io.InputStreamReader)9 JsonParseException (com.google.gson.JsonParseException)8 URL (java.net.URL)8