Class JSONFormatter

java.lang.Object
de.ralleytn.simple.json.JSONFormatter

public class JSONFormatter extends Object
Can format and minimize JSON data.
Since:
1.0.0
Version:
2.1.0
Author:
Ralph Niemitz/RalleYTN(ralph.niemitz@gmx.de)
  • Constructor Details

    • JSONFormatter

      public JSONFormatter()
      Constructs a new JSONFormatter. Default indent is 1 and tabulators will be used for the indent.
      Since:
      2.0.0
    • JSONFormatter

      public JSONFormatter(int indent, boolean useCRLF, boolean useTabs)
      Constructs a new JSONFormatter.
      Parameters:
      indent - the character count of spaces/tabs used for the indent
      useCRLF - true = CRLF (Windows linebreak), false = LF (Unix linebreak)
      useTabs - true = tabulator, false = space
      Since:
      2.1.0
  • Method Details

    • setUseCRLF

      public void setUseCRLF(boolean crlf)
      Sets whether a CRLF or a LF line break should be used.
      Parameters:
      crlf - true = CRLF (Windows linebreak), false = LF (Unix linebreak)
      Since:
      2.0.0
    • setUseTabs

      public void setUseTabs(boolean tabs)
      Sets whether a tabulator or space should be used for the indent.
      Parameters:
      tabs - true = tabulator, false = space
      Since:
      2.0.0
    • setIndent

      public void setIndent(int indent)
      Sets the indent.
      Parameters:
      indent - the character count of spaces/tabs used for the indent
      Since:
      2.0.0
    • format

      public void format(Reader reader, Writer writer) throws IOException
      Formats minimized JSON data. Do not try to format already formatted JSON. The result does not look good.
      Parameters:
      reader - the Reader with the JSON data
      writer - the Writer on which the formatted JSON data should be written
      Throws:
      IOException - if an I/O error occurs
      Since:
      1.0.0
    • format

      public String format(String json)
      Formats minimized JSON data. Do not try to format already formatted JSON. The result does not look good.
      Parameters:
      json - the JSON data that should be formatted
      Returns:
      the formatted JSON data
      Since:
      1.0.0
    • format

      public void format(JSONObject object, Writer writer) throws IOException
      Formats minimized JSON data. Do not try to format already formatted JSON. The result does not look good.
      Parameters:
      object - The JSON object that is meant to be formatted
      writer - The writer on which the formatted JSON string will be written
      Throws:
      IOException - if an I/O error occurs
      Since:
      2.1.0
    • format

      public void format(JSONArray array, Writer writer) throws IOException
      Formats minimized JSON data. Do not try to format already formatted JSON. The result does not look good.
      Parameters:
      array - The JSON array that is meant to be formatted
      writer - The writer on which the formatted JSON string will be written
      Throws:
      IOException - if an I/O error occurs
      Since:
      2.1.0
    • minimize

      public void minimize(Reader reader, Writer writer) throws IOException
      Minimizes formatted JSON data.
      Parameters:
      reader - the Reader with the formatted JSON data
      writer - the Writer on which the minimized JSON data should be written
      Throws:
      IOException - if an I/O error occurs
      Since:
      1.0.0
    • minimize

      public String minimize(String json)
      Minimizes formatted JSON data.
      Parameters:
      json - the formatted JSON data
      Returns:
      the minimized JSON data
      Since:
      1.0.0
    • usesCRLF

      public boolean usesCRLF()
      Returns:
      true when CRLF line breaks are used for formatting, else false
      Since:
      2.0.0
    • usesTabs

      public boolean usesTabs()
      Returns:
      true if tabulators are used for the indent, else false
      Since:
      2.0.0
    • getIndent

      public int getIndent()
      Returns:
      the indent
      Since:
      2.0.0