adams.data.conversion.ReportToJson.java Source code

Java tutorial

Introduction

Here is the source code for adams.data.conversion.ReportToJson.java

Source

/*
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * ReportToJson.java
 * Copyright (C) 2018 University of Waikato, Hamilton, NZ
 */

package adams.data.conversion;

import adams.core.QuickInfoHelper;
import adams.core.io.PrettyPrintingSupporter;
import adams.data.report.Report;
import adams.data.report.ReportJsonUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;

/**
 <!-- globalinfo-start -->
 * Turns a report into a JSON string.<br>
 * Output format:<br>
 * {<br>
 *   "Sample ID": "someid",<br>
 *   "GLV2": 1.123,<br>
 *   "valid": true<br>
 * }<br>
 * <br><br>
 <!-- globalinfo-end -->
 *
 <!-- options-start -->
 * <pre>-logging-level &lt;OFF|SEVERE|WARNING|INFO|CONFIG|FINE|FINER|FINEST&gt; (property: loggingLevel)
 * &nbsp;&nbsp;&nbsp;The logging level for outputting errors and debugging output.
 * &nbsp;&nbsp;&nbsp;default: WARNING
 * </pre>
 *
 * <pre>-pretty-printing &lt;boolean&gt; (property: prettyPrinting)
 * &nbsp;&nbsp;&nbsp;If enabled, the output is printed in a 'pretty' format.
 * &nbsp;&nbsp;&nbsp;default: false
 * </pre>
 *
 <!-- options-end -->
 *
 * @author FracPete (fracpete at waikato dot ac dot nz)
 */
public class ReportToJson extends AbstractConversion implements PrettyPrintingSupporter {

    private static final long serialVersionUID = 2957342595369694174L;

    /** whether to use pretty-printing. */
    protected boolean m_PrettyPrinting;

    /**
     * Returns a string describing the object.
     *
     * @return          a description suitable for displaying in the gui
     */
    @Override
    public String globalInfo() {
        return "Turns a report into a JSON string.\n" + "Output format:\n" + ReportJsonUtils.example();
    }

    /**
     * Adds options to the internal list of options.
     */
    public void defineOptions() {
        super.defineOptions();

        m_OptionManager.add("pretty-printing", "prettyPrinting", false);
    }

    /**
     * Sets whether to use pretty-printing or not.
     *
     * @param value   true if to use pretty-printing
     */
    public void setPrettyPrinting(boolean value) {
        m_PrettyPrinting = value;
        reset();
    }

    /**
     * Returns whether pretty-printing is used or not.
     *
     * @return      true if to use pretty-printing
     */
    public boolean getPrettyPrinting() {
        return m_PrettyPrinting;
    }

    /**
     * Returns the tip text for this property.
     *
     * @return       tip text for this property suitable for
     *          displaying in the GUI or for listing the options.
     */
    public String prettyPrintingTipText() {
        return "If enabled, the output is printed in a 'pretty' format.";
    }

    /**
     * Returns a quick info about the object, which can be displayed in the GUI.
     *
     * @return      null if no info available, otherwise short string
     */
    @Override
    public String getQuickInfo() {
        return QuickInfoHelper.toString(this, "prettyPrinting", m_PrettyPrinting, "pretty-printing");
    }

    /**
     * Returns the class that is accepted as input.
     *
     * @return      the class
     */
    @Override
    public Class accepts() {
        return Report.class;
    }

    /**
     * Returns the class that is generated as output.
     *
     * @return      the class
     */
    @Override
    public Class generates() {
        return String.class;
    }

    /**
     * Performs the actual conversion.
     *
     * @return      the converted data
     * @throws Exception   if something goes wrong with the conversion
     */
    @Override
    protected Object doConvert() throws Exception {
        GsonBuilder builder;
        Gson gson;
        JsonObject jobj;

        jobj = ReportJsonUtils.toJson((Report) m_Input);
        builder = new GsonBuilder();
        if (m_PrettyPrinting)
            builder.setPrettyPrinting();
        gson = builder.create();
        return gson.toJson(jobj);
    }
}