Class StringIndenter
java.lang.Object
org.codehaus.jettison.util.StringIndenter
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
FieldsModifier and TypeFieldDescriptionprivate intcurrent number of indents (times to is the indentprivate intcurrent end tagprivate Stringchars to processprivate StringBuilderresultprivate intcurrent start tag -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate intafter the last start tag, find the next end start tagprivate static intfindNextEndTagIndex(String json, int startFrom) find the end tag from json and a start from indexprivate static intfindNextStartTagIndex(String json, int startFrom) find the start tag from json and a start from index either look for a quote, {, [ or scalar.private intafter the last end tag, find the next start tagprivate voidindent()indent the stringprivate static booleaninstantIndent(String json, int index) see if instant indentprivate static booleaninstantNewline(String json, int index) see if instant indentprivate static booleaninstantUnindent(String json, int index) see if instant unindentprivate static booleaninstantUnindentTwoChars(String json, int index) see if instant indentprivate voidput a newline and indentprivate booleansee if current pos is on newlineprivate voidprintNewlineIndent(int start, int end) put a newline and indentprivate static Stringrepeat a string a certain number of times.result()get the resultprivate voidunindent()unindent a previous indent if it is there
-
Field Details
-
json
chars to process -
startTagIndex
private int startTagIndexcurrent start tag -
endTagIndex
private int endTagIndexcurrent end tag -
currentNumberOfIndents
private int currentNumberOfIndentscurrent number of indents (times to is the indent -
result
result
-
-
Constructor Details
-
StringIndenter
- Parameters:
theJson- is the json to format indenter
-
-
Method Details
-
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
see if instant indent- Parameters:
json-index-- Returns:
- if it is an instant indent
-
instantNewline
see if instant indent- Parameters:
json-index-- Returns:
- if it is an instant indent
-
instantUnindent
see if instant unindent- Parameters:
json-index-- Returns:
- if it is an instant unindent
-
instantUnindentTwoChars
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
-
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
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
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
-