Example usage for com.google.gson GsonBuilder setPrettyPrinting

List of usage examples for com.google.gson GsonBuilder setPrettyPrinting

Introduction

In this page you can find the example usage for com.google.gson GsonBuilder setPrettyPrinting.

Prototype

public GsonBuilder setPrettyPrinting() 

Source Link

Document

Configures Gson to output Json that fits in a page for pretty printing.

Usage

From source file:org.apache.zeppelin.notebook.Notebook.java

License:Apache License

/**
 * import JSON as a new note./*from  w w w  . j a v a  2  s .c  o m*/
 *
 * @param sourceJson - the note JSON to import
 * @param noteName   - the name of the new note
 * @return notebook 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);
        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);
        }

        newNote.persist(subject);
    } catch (IOException e) {
        logger.error(e.toString(), e);
        throw e;
    }

    return newNote;
}

From source file:org.apache.zeppelin.notebook.NotebookAuthorization.java

License:Apache License

public static NotebookAuthorization init(ZeppelinConfiguration config) {
    if (instance == null) {
        instance = new NotebookAuthorization();
        conf = config;//from  ww  w . j  av a2s.  c  o m
        filePath = conf.getNotebookAuthorizationPath();
        GsonBuilder builder = new GsonBuilder();
        builder.setPrettyPrinting();
        gson = builder.create();
        try {
            loadFromFile();
        } catch (IOException e) {
            LOG.error("Error loading NotebookAuthorization", e);
        }
    }
    return instance;
}

From source file:org.apache.zeppelin.notebook.repo.AzureNotebookRepo.java

License:Apache License

private Note getNote(String noteId) throws IOException {
    InputStream ins = null;/*from   www .j  ava2  s .  c  o m*/

    try {
        CloudFileDirectory dir = rootDir.getDirectoryReference(noteId);
        CloudFile file = dir.getFileReference("note.json");

        ins = file.openRead();
    } catch (URISyntaxException | StorageException e) {
        String msg = String.format("Error reading notebook %s from Azure storage", noteId);

        LOG.error(msg, e);

        throw new IOException(msg, e);
    }

    String json = IOUtils.toString(ins, conf.getString(ZeppelinConfiguration.ConfVars.ZEPPELIN_ENCODING));
    ins.close();

    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setPrettyPrinting();
    Gson gson = 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);
        }
    }

    return note;
}

From source file:org.apache.zeppelin.notebook.repo.AzureNotebookRepo.java

License:Apache License

@Override
public void save(Note note, AuthenticationInfo subject) throws IOException {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setPrettyPrinting();
    Gson gson = gsonBuilder.create();//  w  w  w  .  j  a  v a2s  .c om
    String json = gson.toJson(note);

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    Writer writer = new OutputStreamWriter(output);
    writer.write(json);
    writer.close();
    output.close();

    byte[] buffer = output.toByteArray();

    try {
        CloudFileDirectory dir = rootDir.getDirectoryReference(note.getId());
        dir.createIfNotExists();

        CloudFile cloudFile = dir.getFileReference("note.json");
        cloudFile.uploadFromByteArray(buffer, 0, buffer.length);
    } catch (URISyntaxException | StorageException e) {
        String msg = String.format("Error saving notebook %s to Azure storage", note.getId());

        LOG.error(msg, e);

        throw new IOException(msg, e);
    }
}

From source file:org.apache.zeppelin.notebook.repo.S3NotebookRepo.java

License:Apache License

private Note getNote(String key) throws IOException {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setPrettyPrinting();
    Gson gson = gsonBuilder.registerTypeAdapter(Date.class, new NotebookImportDeserializer()).create();

    S3Object s3object;/*from w  ww .j av  a 2 s . com*/
    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;
}

From source file:org.apache.zeppelin.notebook.repo.S3NotebookRepo.java

License:Apache License

@Override
public void save(Note note, AuthenticationInfo subject) throws IOException {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setPrettyPrinting();
    Gson gson = gsonBuilder.create();// w ww  .  jav  a 2  s. co m
    String json = gson.toJson(note);
    String key = user + "/" + "notebook" + "/" + note.getId() + "/" + "note.json";

    File file = File.createTempFile("note", "json");
    try {
        Writer writer = new OutputStreamWriter(new FileOutputStream(file));
        writer.write(json);
        writer.close();
        s3client.putObject(new PutObjectRequest(bucketName, key, file));
    } catch (AmazonClientException ace) {
        throw new IOException("Unable to store note in S3: " + ace, ace);
    } finally {
        FileUtils.deleteQuietly(file);
    }
}

From source file:org.apache.zeppelin.notebook.repo.VFSNotebookRepo.java

License:Apache License

private Note getNote(FileObject noteDir) throws IOException {
    if (!isDirectory(noteDir)) {
        throw new IOException(noteDir.getName().toString() + " is not a directory");
    }//from ww  w.jav a2s.  c om

    FileObject noteJson = noteDir.resolveFile("note.json", NameScope.CHILD);
    if (!noteJson.exists()) {
        throw new IOException(noteJson.getName().toString() + " not found");
    }

    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setPrettyPrinting();
    Gson gson = gsonBuilder.create();

    FileContent content = noteJson.getContent();
    InputStream ins = content.getInputStream();
    String json = IOUtils.toString(ins, conf.getString(ConfVars.ZEPPELIN_ENCODING));
    ins.close();

    Note note = gson.fromJson(json, Note.class);
    //    note.setReplLoader(replLoader);
    //    note.jobListenerFactory = jobListenerFactory;

    for (Paragraph p : note.getParagraphs()) {
        if (p.getStatus() == Status.PENDING || p.getStatus() == Status.RUNNING) {
            p.setStatus(Status.ABORT);
        }
    }

    return note;
}

From source file:org.apache.zeppelin.notebook.repo.VFSNotebookRepo.java

License:Apache License

@Override
public void save(Note note) throws IOException {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setPrettyPrinting();
    Gson gson = gsonBuilder.create();/*from  ww w .  j  a v  a  2 s  .  c  o  m*/
    String json = gson.toJson(note);

    FileObject rootDir = getRootDir();

    FileObject noteDir = rootDir.resolveFile(note.id(), NameScope.CHILD);

    if (!noteDir.exists()) {
        noteDir.createFolder();
    }
    if (!isDirectory(noteDir)) {
        throw new IOException(noteDir.getName().toString() + " is not a directory");
    }

    FileObject noteJson = noteDir.resolveFile("note.json", NameScope.CHILD);
    // false means not appending. creates file if not exists
    OutputStream out = noteJson.getContent().getOutputStream(false);
    out.write(json.getBytes(conf.getString(ConfVars.ZEPPELIN_ENCODING)));
    out.close();
}

From source file:org.apache.zeppelin.rest.JsonResponse.java

License:Apache License

@Override
public String toString() {
    GsonBuilder gsonBuilder = new GsonBuilder().registerTypeAdapter(InterpreterSetting.InterpreterInfo.class,
            new InterpreterInfoSerializer());
    if (pretty) {
        gsonBuilder.setPrettyPrinting();
    }/* w ww.j av  a2 s .  c o  m*/
    gsonBuilder.setExclusionStrategies(new JsonExclusionStrategy());
    Gson gson = gsonBuilder.create();
    return gson.toJson(this);
}

From source file:org.apache.zeppelin.server.JsonResponse.java

License:Apache License

@Override
public String toString() {
    GsonBuilder gsonBuilder = new GsonBuilder();
    if (pretty) {
        gsonBuilder.setPrettyPrinting();
    }/*from  w  w w .j  a  v a 2s .c  o  m*/
    gsonBuilder.setExclusionStrategies(new JsonExclusionStrategy());
    Gson gson = gsonBuilder.create();
    return gson.toJson(this);
}