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:io.hops.hopsworks.api.zeppelin.server.JsonResponse.java

License:Apache License

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

From source file:it.jugpadova.blol.FeedsBo.java

License:Apache License

/**
 * Build an JSON serialization from the list of events.
 *
 * @param events The list of events//  ww w . j a  va 2  s . c  om
 * @param baseUrl The application base URL
 * @return The JSON serialization
 */
public String buildJson(List<Event> events, String baseUrl, boolean prettyPrinting) {
    String result = null;
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Event.class, new EventJsonSerializer());
    if (prettyPrinting) {
        gsonBuilder.setPrettyPrinting();
    }
    Gson gson = gsonBuilder.create();
    result = gson.toJson(events);
    return result;
}

From source file:jetbrains.buildServer.sharedResources.server.report.UsedResourcesSerializer.java

License:Apache License

public List<UsedResource> read(@NotNull final Reader reader) {
    final GsonBuilder builder = new GsonBuilder();
    builder.setFieldNamingStrategy(STRATEGY);
    builder.registerTypeAdapter(Resource.class,
            (InstanceCreator<Resource>) type -> ResourceFactory.newInfiniteResource("", "", "", true));
    builder.registerTypeAdapter(Lock.class, (InstanceCreator<Lock>) type -> new Lock("", LockType.READ, ""));
    builder.registerTypeAdapterFactory(MY_TYPE_ADAPTER_FACTORY);
    return builder.setPrettyPrinting().create().fromJson(reader, new TypeToken<List<UsedResource>>() {
    }.getType());/*www. j  ava 2  s.  c o  m*/
}

From source file:jetbrains.buildServer.sharedResources.server.report.UsedResourcesSerializer.java

License:Apache License

public void write(Collection<UsedResource> usedResources, Writer writer) {
    final GsonBuilder builder = new GsonBuilder();
    builder.setFieldNamingStrategy(STRATEGY);
    builder.setPrettyPrinting();
    builder.create().toJson(usedResources, writer);
}

From source file:loadmaprenderer.WhiteboxGuiClone.java

License:Open Source License

private void saveMap() {
    if (numOpenMaps < 1) {
        return;/* w w  w .  ja v  a 2 s.  c  o  m*/
    } // do nothing
    if (selectedMapAndLayer[0] == -1) {
        selectedMapAndLayer[0] = activeMap;
    }
    if (openMaps.get(selectedMapAndLayer[0]).getFileName().equals("")) {
        saveMapAs();
    } else {

        // Any CartographicElementGroups in the map will need to be ungrouped.
        int howManyGroups = openMaps.get(selectedMapAndLayer[0]).numberOfElementGroups();
        if (howManyGroups > 0) {
            showFeedback(messages.getString("NoGroupsInSavedMap"));
            openMaps.get(selectedMapAndLayer[0]).ungroupAllElements();
        }

        File file = new File(openMaps.get(selectedMapAndLayer[0]).getFileName());

        if (file.exists()) {
            file.delete();
        }

        recentMapsMenu.addMenuItem(openMaps.get(selectedMapAndLayer[0]).getFileName());

        FileWriter fw = null;
        BufferedWriter bw = null;
        PrintWriter out = null;
        try {
            fw = new FileWriter(file, false);
            bw = new BufferedWriter(fw);
            out = new PrintWriter(bw, true);

            GsonBuilder gsonBuilder = new GsonBuilder();
            gsonBuilder.setPrettyPrinting();
            gsonBuilder.registerTypeAdapter(MapInfo.class, new MapInfoSerializer());
            Gson gson = gsonBuilder.create();

            String json = gson.toJson(openMaps.get(selectedMapAndLayer[0]));
            out.println(json);
        } catch (java.io.IOException e) {
            logger.log(Level.SEVERE, "WhiteboxGui.saveMap", e);
            //System.err.println("Error: " + e.getMessage());
        } catch (Exception e) { //Catch exception if any
            logger.log(Level.SEVERE, "WhiteboxGui.saveMap", e);
            //System.err.println("Error: " + e.getMessage());
        } finally {
            if (out != null || bw != null) {
                out.flush();
                out.close();
            }

        }
    }

    selectedMapAndLayer[0] = -1;
    selectedMapAndLayer[1] = -1;
    selectedMapAndLayer[2] = -1;
}

From source file:loadmaprenderer.WhiteboxGuiClone.java

License:Open Source License

private void openMap() {
    // set the filter.
    ArrayList<ExtensionFileFilter> filters = new ArrayList<>();
    String filterDescription = "Whitebox Map Files (*.wmap)";
    String[] extensions = { "WMAP" }; //, "XML"};
    ExtensionFileFilter eff = new ExtensionFileFilter(filterDescription, extensions);

    filters.add(eff);// w w  w. j ava2s.  c o m

    JFileChooser fc = new JFileChooser();
    fc.setCurrentDirectory(new File(workingDirectory));
    fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    fc.setMultiSelectionEnabled(true);
    fc.setAcceptAllFileFilterUsed(false);

    for (int i = 0; i < filters.size(); i++) {
        fc.setFileFilter(filters.get(i));
    }

    int result = fc.showOpenDialog(this);
    File[] files = null;
    if (result == JFileChooser.APPROVE_OPTION) {
        files = fc.getSelectedFiles();
        String fileDirectory = files[0].getParentFile() + pathSep;
        if (!fileDirectory.equals(workingDirectory)) {
            setWorkingDirectory(fileDirectory);
        }
        for (int i = 0; i < files.length; i++) {

            try {
                // first read the text from the file into a string
                String mapTextData = whitebox.utilities.FileUtilities.readFileAsString(files[i].toString());

                // now use gson to create a new MapInfo object by deserialization
                GsonBuilder gsonBuilder = new GsonBuilder();
                gsonBuilder.setPrettyPrinting();
                gsonBuilder.registerTypeAdapter(MapInfo.class,
                        new MapInfoDeserializer(workingDirectory, paletteDirectory));
                Gson gson = gsonBuilder.create();

                MapInfo map = gson.fromJson(mapTextData, MapInfo.class);

                openMaps.add(map);
            } catch (Exception e) {
                showFeedback(messages.getString("MapFile") + files[i].toString() + " "
                        + messages.getString("NotReadProperly"));
                logger.log(Level.SEVERE, "WhiteboxGui.openMap", e);
                return;
            }

            recentMapsMenu.addMenuItem(files[i].toString());
        }

        activeMap = openMaps.size() - 1;
        drawingArea.setMapInfo(openMaps.get(activeMap));
        drawingArea.repaint();
        updateLayersTab();
        selectedMapAndLayer[0] = -1;
        selectedMapAndLayer[1] = -1;
        selectedMapAndLayer[2] = -1;
        numOpenMaps++;
    }
}

From source file:loadmaprenderer.WhiteboxGuiClone.java

License:Open Source License

private void openMap(String fileName) {
    File file = new File(fileName);
    if (!file.exists()) {
        showFeedback(messages.getString("NoMapFile"));
        return;//from  w  ww.j a v  a2 s  . c  o  m
    }

    String fileDirectory = file.getParentFile() + pathSep;
    if (!fileDirectory.equals(workingDirectory)) {
        setWorkingDirectory(fileDirectory);
    }

    try {
        // first read the text from the file into a string
        String mapTextData = whitebox.utilities.FileUtilities.readFileAsString(fileName);

        // now use gson to create a new MapInfo object by deserialization
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setPrettyPrinting();
        gsonBuilder.registerTypeAdapter(MapInfo.class,
                new MapInfoDeserializer(workingDirectory, paletteDirectory));
        Gson gson = gsonBuilder.create();

        MapInfo map = gson.fromJson(mapTextData, MapInfo.class);

        openMaps.add(map);
    } catch (IOException | JsonSyntaxException e) {
        showFeedback(
                messages.getString("MapFile") + " " + fileName + " " + messages.getString("NotReadProperly"));
        logger.log(Level.SEVERE, "WhiteboxGui.openMap", e);
        return;
    }

    recentMapsMenu.addMenuItem(fileName);

    activeMap = openMaps.size() - 1;
    drawingArea.setMapInfo(openMaps.get(activeMap));
    drawingArea.repaint();
    updateLayersTab();
    selectedMapAndLayer[0] = -1;
    selectedMapAndLayer[1] = -1;
    selectedMapAndLayer[2] = -1;
    numOpenMaps++;
}

From source file:main.java.miro.browser.util.DownloadHandler.java

License:Open Source License

private String objectToJson(RepositoryObject obj, boolean subtree) {
    GsonBuilder builder = new GsonBuilder();
    builder.setPrettyPrinting();
    builder.registerTypeAdapter(RepositoryObject.class, new RepositoryObjectSerializer());
    builder.registerTypeAdapter(ResourceHoldingObject.class, new ResourceHoldingObjectSerializer());
    builder.registerTypeAdapter(CertificateObject.class, new CertificateObjectJsonSerializer(subtree));
    builder.registerTypeAdapter(ManifestObject.class, new ManifestSerializer());
    builder.registerTypeAdapter(CRLObject.class, new CRLSerializer());
    builder.registerTypeAdapter(RoaObject.class, new RoaSerializer());
    builder.registerTypeAdapter(ValidationResults.class, new ValidationResultsSerializer());
    builder.registerTypeAdapter(IpResourceSet.class, new IpResourceSetSerializer());
    builder.registerTypeAdapter(byte[].class, new ByteArrayToHexSerializer());
    Gson gson = builder.create();//w w  w  .  j  av  a  2 s . c om
    return gson.toJson(obj, RepositoryObject.class);
}

From source file:main.java.miro.validator.export.json.JsonExporter.java

License:Open Source License

public void export(ResourceCertificateTree tree) {
    GsonBuilder builder = new GsonBuilder().disableHtmlEscaping();
    builder.registerTypeAdapter(ResourceCertificateTree.class, new ResourceCertificateTreeSerializer());
    builder.registerTypeAdapter(CertificateObject.class, new CertificateObjectJsonSerializer(true));
    builder.registerTypeAdapter(IpResourceSet.class, new IpResourceSetSerializer());
    builder.registerTypeAdapter(byte[].class, new ByteArrayToHexSerializer());
    builder.registerTypeAdapter(ManifestObject.class, new ManifestSerializer());
    builder.registerTypeAdapter(RoaObject.class, new RoaSerializer());
    builder.registerTypeAdapter(CRLObject.class, new CRLSerializer());
    builder.registerTypeAdapter(ResourceHoldingObject.class, new ResourceHoldingObjectSerializer());
    builder.registerTypeAdapter(ValidationCheck.class, new ValidationCheckSerializer());
    builder.registerTypeAdapter(ValidationResults.class, new ValidationResultsSerializer());

    Gson gson = builder.setPrettyPrinting().create();
    File f = new File(EXPORT_FILE);
    f.getParentFile().mkdirs();// www. j  a va  2s  . co  m

    try {
        FileWriter fw = new FileWriter(f);
        gson.toJson(tree, fw);
        fw.flush();
    } catch (IOException e) {
        e.printStackTrace();
        logger.log(Level.SEVERE, "Error: Exporting to file " + EXPORT_FILE + " failed. Exiting");
    }
}

From source file:mobi.jenkinsci.model.ItemNode.java

License:Apache License

private Gson getGson() {
    final GsonBuilder gbuilder = new GsonBuilder();
    gbuilder.disableHtmlEscaping();/*from   w ww.  jav a2s .c  om*/
    if (JSON_PRETTY_PRINTING) {
        gbuilder.setPrettyPrinting();
    }
    final Gson gson = gbuilder.create();
    return gson;
}