Class StringIndenter


  • public class StringIndenter
    extends java.lang.Object
     indent json, assumes the input is not yet indented.  here is an example:
     {
     "FastResponseJs":{
       "ajaxElements":{
         "AjaxMapEntry":[
           {
             "theKey":{
               "@class":"string",
               "$":"ajax1a"
             },
             "theValue":{
               "@class":"AjaxEvent",
               "ajaxSendFormNames":{
                 "string":"ajax1"
               },
               "ajaxEventType":"NORMAL",
               "eventName":"onblur",
               "ajaxId":"PRLZY5QZ",
               "screenElementId":"ajax1a",
               "asynchronous":true,
               "disableValidation":true
             }
             
           },
           {
             "theKey":{
            ...
            
            
       Usage: String formatted = new StringIndenter(jsonString).result();
     
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private int currentNumberOfIndents
      current number of indents (times to is the indent
      private int endTagIndex
      current end tag
      private java.lang.String json
      chars to process
      private java.lang.StringBuilder result
      result
      private int startTagIndex
      current start tag
    • Constructor Summary

      Constructors 
      Constructor Description
      StringIndenter​(java.lang.String theJson)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private int findEndTagIndex()
      after the last start tag, find the next end start tag
      private static int findNextEndTagIndex​(java.lang.String json, int startFrom)
      find the end tag from json and a start from index
      private static int findNextStartTagIndex​(java.lang.String json, int startFrom)
      find the start tag from json and a start from index either look for a quote, {, [ or scalar.
      private int findStartTagIndex()
      after the last end tag, find the next start tag
      private void indent()
      indent the string
      private static boolean instantIndent​(java.lang.String json, int index)
      see if instant indent
      private static boolean instantNewline​(java.lang.String json, int index)
      see if instant indent
      private static boolean instantUnindent​(java.lang.String json, int index)
      see if instant unindent
      private static boolean instantUnindentTwoChars​(java.lang.String json, int index)
      see if instant indent
      private void newlineIndent()
      put a newline and indent
      private boolean onNewline()
      see if current pos is on newline
      private void printNewlineIndent​(int start, int end)
      put a newline and indent
      private static java.lang.String repeat​(java.lang.String theString, int times)
      repeat a string a certain number of times.
      java.lang.String result()
      get the result
      private void unindent()
      unindent a previous indent if it is there
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • json

        private java.lang.String json
        chars to process
      • startTagIndex

        private int startTagIndex
        current start tag
      • endTagIndex

        private int endTagIndex
        current end tag
      • currentNumberOfIndents

        private int currentNumberOfIndents
        current number of indents (times to is the indent
      • result

        private java.lang.StringBuilder result
        result
    • Constructor Detail

      • StringIndenter

        public StringIndenter​(java.lang.String theJson)
        Parameters:
        theJson - is the json to format indenter
    • Method Detail

      • result

        public java.lang.String result()
        get the result
        Returns:
        the result
      • indent

        private void indent()
        indent the string
      • onNewline

        private boolean onNewline()
        see if current pos is on newline
        Returns:
        true if on new line
      • instantIndent

        private static boolean instantIndent​(java.lang.String json,
                                             int index)
        see if instant indent
        Parameters:
        json -
        index -
        Returns:
        if it is an instant indent
      • instantNewline

        private static boolean instantNewline​(java.lang.String json,
                                              int index)
        see if instant indent
        Parameters:
        json -
        index -
        Returns:
        if it is an instant indent
      • instantUnindent

        private static boolean instantUnindent​(java.lang.String json,
                                               int index)
        see if instant unindent
        Parameters:
        json -
        index -
        Returns:
        if it is an instant unindent
      • instantUnindentTwoChars

        private static boolean instantUnindentTwoChars​(java.lang.String json,
                                                       int index)
        see if instant indent
        Parameters:
        json -
        index -
        Returns:
        if it is an instant indent
      • printNewlineIndent

        private void printNewlineIndent​(int start,
                                        int end)
        put a newline and indent
        Parameters:
        start -
        end -
      • newlineIndent

        private void newlineIndent()
        put a newline and indent
      • repeat

        private static java.lang.String repeat​(java.lang.String theString,
                                               int times)
        repeat a string a certain number of times.
        Parameters:
        theString -
        times -
        Returns:
        the string
      • unindent

        private void unindent()
        unindent a previous indent if it is there
      • findStartTagIndex

        private int findStartTagIndex()
        after the last end tag, find the next start tag
        Returns:
        the next start tag
      • findEndTagIndex

        private int findEndTagIndex()
        after the last start tag, find the next end start tag
        Returns:
        the next start tag
      • findNextStartTagIndex

        private static int findNextStartTagIndex​(java.lang.String json,
                                                 int startFrom)
        find the start tag from json and a start from index either look for a quote, {, [ or scalar. generally not whitespace
        Parameters:
        json -
        startFrom -
        Returns:
        the start tag index of -1 if not found another
      • findNextEndTagIndex

        private static int findNextEndTagIndex​(java.lang.String json,
                                               int startFrom)
        find the end tag from json and a start from index
        Parameters:
        json -
        startFrom - is the char after the start of tag
        Returns:
        the start tag index of -1 if not found another