Search in sources :

Example 16 with Gson

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

the class MongoNotebookRepo method documentToNote.

/**
   * Convert document to note
   */
private Note documentToNote(Document doc) {
    // document to JSON
    String json = doc.toJson();
    // JSON to note
    Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new NotebookImportDeserializer()).create();
    Note note = gson.fromJson(json, Note.class);
    for (Paragraph p : note.getParagraphs()) {
        if (p.getStatus() == Job.Status.PENDING || p.getStatus() == Job.Status.RUNNING) {
            p.setStatus(Job.Status.ABORT);
        }
        List<ApplicationState> appStates = p.getAllApplicationStates();
        if (appStates != null) {
            for (ApplicationState app : appStates) {
                if (app.getStatus() != ApplicationState.Status.ERROR) {
                    app.setStatus(ApplicationState.Status.UNLOADED);
                }
            }
        }
    }
    return note;
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) Note(org.apache.zeppelin.notebook.Note) ApplicationState(org.apache.zeppelin.notebook.ApplicationState) Gson(com.google.gson.Gson) NotebookImportDeserializer(org.apache.zeppelin.notebook.NotebookImportDeserializer) Paragraph(org.apache.zeppelin.notebook.Paragraph)

Example 17 with Gson

use of com.google.gson.Gson 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 18 with Gson

use of com.google.gson.Gson 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 19 with Gson

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

the class HeliumBundleFactory method getWebpackResultFromOutput.

private WebpackResult getWebpackResultFromOutput(String output) {
    BufferedReader reader = new BufferedReader(new StringReader(output));
    String line;
    boolean webpackRunDetected = false;
    boolean resultJsonDetected = false;
    StringBuffer sb = new StringBuffer();
    try {
        while ((line = reader.readLine()) != null) {
            if (!webpackRunDetected) {
                if (line.contains("webpack.js") && line.endsWith("--json")) {
                    webpackRunDetected = true;
                }
                continue;
            }
            if (!resultJsonDetected) {
                if (line.equals("{")) {
                    sb.append(line);
                    resultJsonDetected = true;
                }
                continue;
            }
            if (resultJsonDetected && webpackRunDetected) {
                sb.append(line);
            }
        }
        Gson gson = new Gson();
        return gson.fromJson(sb.toString(), WebpackResult.class);
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
        return new WebpackResult();
    }
}
Also used : Gson(com.google.gson.Gson)

Example 20 with Gson

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

the class HeliumLocalRegistryTest method testGetAllPackage.

@Test
public void testGetAllPackage() throws IOException {
    // given
    File r1Path = new File(tmpDir, "r1");
    HeliumLocalRegistry r1 = new HeliumLocalRegistry("r1", r1Path.getAbsolutePath());
    assertEquals(0, r1.getAll().size());
    // when
    Gson gson = new Gson();
    HeliumPackage pkg1 = new HeliumPackage(HeliumType.APPLICATION, "app1", "desc1", "artifact1", "classname1", new String[][] {}, "license", "");
    FileUtils.writeStringToFile(new File(r1Path, "pkg1.json"), gson.toJson(pkg1));
    // then
    assertEquals(1, r1.getAll().size());
}
Also used : Gson(com.google.gson.Gson) File(java.io.File) Test(org.junit.Test)

Aggregations

Gson (com.google.gson.Gson)1060 Test (org.junit.Test)236 HashMap (java.util.HashMap)200 JsonObject (com.google.gson.JsonObject)138 GsonBuilder (com.google.gson.GsonBuilder)135 CommandWrapper (ClientServerApi.CommandWrapper)123 CommandExecuter (CommandHandler.CommandExecuter)119 CriticalError (SQLDatabase.SQLDatabaseException.CriticalError)118 IOException (java.io.IOException)116 JsonSyntaxException (com.google.gson.JsonSyntaxException)100 ClientNotConnected (SQLDatabase.SQLDatabaseException.ClientNotConnected)96 ArrayList (java.util.ArrayList)94 Type (java.lang.reflect.Type)65 JsonElement (com.google.gson.JsonElement)57 ProductNotExistInCatalog (SQLDatabase.SQLDatabaseException.ProductNotExistInCatalog)53 SmartCode (BasicCommonClasses.SmartCode)50 Map (java.util.Map)45 InputStreamReader (java.io.InputStreamReader)41 BroadcastClientMessage (org.bigbluebutton.red5.client.messaging.BroadcastClientMessage)41 Location (BasicCommonClasses.Location)40