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;
}
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;
}
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;
}
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();
}
}
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());
}
Aggregations