Java tutorial
/* * 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/>. */ /** * JsonToString.java * Copyright (C) 2013 University of Waikato, Hamilton, New Zealand */ package adams.data.conversion; import adams.core.io.PrettyPrintingSupporter; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; import com.google.gson.JsonParser; import net.minidev.json.JSONAware; /** <!-- globalinfo-start --> * Turns a JSON object/array into a string. * <br><br> <!-- globalinfo-end --> * <!-- options-start --> * Valid options are: <br><br> * * <pre>-D <int> (property: debugLevel) * The greater the number the more additional info the scheme may output to * the console (0 = off). * default: 0 * minimum: 0 * </pre> * <!-- options-end --> * * @author fracpete (fracpete at waikato dot ac dot nz) * @version $Revision$ */ public class JsonToString extends AbstractConversionToString implements PrettyPrintingSupporter { /** for serialization. */ private static final long serialVersionUID = -468714756281370533L; /** 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 JSON object/array into a string."; } /** * 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 the class that is accepted as input. * * @return the class */ @Override public Class accepts() { return JSONAware.class; } /** * Performs the actual conversion. * * @return the converted data * @throws Exception if something goes wrong with the conversion */ @Override protected Object doConvert() throws Exception { String result; Gson gson; JsonParser jp; JsonElement je; result = ((JSONAware) m_Input).toJSONString(); if (m_PrettyPrinting) { gson = new GsonBuilder().setPrettyPrinting().create(); jp = new JsonParser(); je = jp.parse(result); result = gson.toJson(je); } return result; } }