use of com.google.gson.GsonBuilder in project che by eclipse.
the class StackLoaderTest method dtoShouldBeSerialized.
@Test
public void dtoShouldBeSerialized() {
StackDto stackDtoDescriptor = newDto(StackDto.class).withName("nameWorkspaceConfig");
StackComponentDto stackComponentDto = newDto(StackComponentDto.class).withName("java").withVersion("1.8");
stackDtoDescriptor.setComponents(Collections.singletonList(stackComponentDto));
stackDtoDescriptor.setTags(Arrays.asList("some teg1", "some teg2"));
stackDtoDescriptor.setDescription("description");
stackDtoDescriptor.setId("someId");
stackDtoDescriptor.setScope("scope");
stackDtoDescriptor.setCreator("Created in Codenvy");
Map<String, String> attributes = new HashMap<>();
attributes.put("attribute1", "valute attribute1");
Link link = newDto(Link.class).withHref("some url").withMethod("get").withRel("someRel").withConsumes("consumes").withProduces("produces");
HashMap<String, List<String>> projectMap = new HashMap<>();
projectMap.put("test", Arrays.asList("test", "test2"));
ProjectProblemDto projectProblem = newDto(ProjectProblemDto.class).withCode(100).withMessage("message");
SourceStorageDto sourceStorageDto = newDto(SourceStorageDto.class).withType("some type").withParameters(attributes).withLocation("location");
ProjectConfigDto projectConfigDto = newDto(ProjectConfigDto.class).withName("project").withPath("somePath").withAttributes(projectMap).withType("maven type").withDescription("some project description").withLinks(Collections.singletonList(link)).withMixins(Collections.singletonList("mixin time")).withProblems(Collections.singletonList(projectProblem)).withSource(sourceStorageDto);
EnvironmentRecipeDto environmentRecipe = newDto(EnvironmentRecipeDto.class).withContent("some content").withContentType("some content type").withType("someType");
Map<String, ServerConf2Dto> servers = new HashMap<>();
servers.put("server1Ref", newDto(ServerConf2Dto.class).withPort("8080/tcp").withProtocol("http").withProperties(singletonMap("key", "value")));
Map<String, ExtendedMachineDto> machines = new HashMap<>();
machines.put("someMachineName", newDto(ExtendedMachineDto.class).withAgents(Arrays.asList("agent1", "agent2")).withServers(servers).withAttributes(singletonMap("memoryLimitBytes", "" + 512L * 1024L * 1024L)));
EnvironmentDto environmentDto = newDto(EnvironmentDto.class).withRecipe(environmentRecipe).withMachines(machines);
CommandDto commandDto = newDto(CommandDto.class).withType("command type").withName("command name").withCommandLine("command line");
WorkspaceConfigDto workspaceConfigDto = newDto(WorkspaceConfigDto.class).withName("SomeWorkspaceConfig").withDescription("some workspace").withLinks(Collections.singletonList(link)).withDefaultEnv("some Default Env name").withProjects(Collections.singletonList(projectConfigDto)).withEnvironments(singletonMap("name", environmentDto)).withCommands(Collections.singletonList(commandDto));
stackDtoDescriptor.setWorkspaceConfig(workspaceConfigDto);
Gson GSON = new GsonBuilder().create();
GSON.fromJson(stackDtoDescriptor.toString(), StackImpl.class);
}
use of com.google.gson.GsonBuilder in project zeppelin by apache.
the class Notebook method exportNote.
/**
* Export existing note.
*
* @param noteId - the note ID to clone
* @return Note JSON
* @throws IOException, IllegalArgumentException
*/
public String exportNote(String noteId) throws IOException, IllegalArgumentException {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setPrettyPrinting();
Gson gson = gsonBuilder.create();
Note note = getNote(noteId);
if (note == null) {
throw new IllegalArgumentException(noteId + " not found");
}
return gson.toJson(note);
}
use of com.google.gson.GsonBuilder in project zeppelin by apache.
the class Notebook method importNote.
/**
* import JSON as a new note.
*
* @param sourceJson - the note JSON to import
* @param noteName - the name of the new note
* @return note ID
* @throws IOException
*/
public Note importNote(String sourceJson, String noteName, AuthenticationInfo subject) throws IOException {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setPrettyPrinting();
Gson gson = gsonBuilder.registerTypeAdapter(Date.class, new NotebookImportDeserializer()).create();
JsonReader reader = new JsonReader(new StringReader(sourceJson));
reader.setLenient(true);
Note newNote;
try {
Note oldNote = gson.fromJson(reader, Note.class);
convertFromSingleResultToMultipleResultsFormat(oldNote);
newNote = createNote(subject);
if (noteName != null)
newNote.setName(noteName);
else
newNote.setName(oldNote.getName());
List<Paragraph> paragraphs = oldNote.getParagraphs();
for (Paragraph p : paragraphs) {
newNote.addCloneParagraph(p);
}
notebookAuthorization.setNewNotePermissions(newNote.getId(), subject);
newNote.persist(subject);
} catch (IOException e) {
logger.error(e.toString(), e);
throw e;
}
return newNote;
}
use of com.google.gson.GsonBuilder 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.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;
}
Aggregations