| 1 |
package org.astrogrid.samp; |
| 2 |
|
| 3 |
import java.util.Iterator; |
| 4 |
import java.util.List; |
| 5 |
import java.util.Map; |
| 6 |
|
| 7 |
/** |
| 8 |
* Outputs a SAMP object as JSON. |
| 9 |
* Can do it formatted and reasonably compact. |
| 10 |
* |
| 11 |
* @author Mark Taylor |
| 12 |
* @since 25 Jul 2011 |
| 13 |
*/ |
| 14 |
class JsonWriter { |
| 15 |
private final int indent_; |
| 16 |
private final String spc_; |
| 17 |
|
| 18 |
/** |
| 19 |
* Constructor with default properties. |
| 20 |
*/ |
| 21 |
public JsonWriter() { |
| 22 |
this( 2, true ); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Custom constructor. |
| 27 |
* |
| 28 |
* @param indent number of characters indent per level |
| 29 |
* @param spacer whether to put spaces inside brackets |
| 30 |
*/ |
| 31 |
public JsonWriter( int indent, boolean spacer ) { |
| 32 |
indent_ = indent; |
| 33 |
spc_ = spacer ? " " : ""; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Converts a SAMP data item to JSON. |
| 38 |
* |
| 39 |
* @param item SAMP-friendly object |
| 40 |
* @return JSON representation |
| 41 |
*/ |
| 42 |
public String toJson( Object item ) { |
| 43 |
StringBuffer sbuf = new StringBuffer(); |
| 44 |
toJson( sbuf, item, 0, false ); |
| 45 |
if ( indent_ >= 0 ) { |
| 46 |
assert sbuf.charAt( 0 ) == '\n'; |
| 47 |
return sbuf.substring( 1, sbuf.length() ); |
| 48 |
} |
| 49 |
else { |
| 50 |
return sbuf.toString(); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Recursive method which does the work for conversion. |
| 56 |
* If possible, call this method with <code>isPositioned=false</code>. |
| 57 |
* |
| 58 |
* @param sbuf string buffer to append result to |
| 59 |
* @param item object to convert |
| 60 |
* @param level current indentation level |
| 61 |
* @param isPositioned true if output should be direct to sbuf, |
| 62 |
* false if it needs a newline plus indentation first |
| 63 |
*/ |
| 64 |
private void toJson( StringBuffer sbuf, Object item, int level, |
| 65 |
boolean isPositioned ) { |
| 66 |
if ( item instanceof String ) { |
| 67 |
if ( ! isPositioned ) { |
| 68 |
sbuf.append( getIndent( level ) ); |
| 69 |
} |
| 70 |
sbuf.append( '"' ) |
| 71 |
.append( (String) item ) |
| 72 |
.append( '"' ); |
| 73 |
} |
| 74 |
else if ( item instanceof List ) { |
| 75 |
List list = (List) item; |
| 76 |
if ( list.isEmpty() ) { |
| 77 |
if ( ! isPositioned ) { |
| 78 |
sbuf.append( getIndent( level ) ); |
| 79 |
} |
| 80 |
sbuf.append( "[]" ); |
| 81 |
} |
| 82 |
else { |
| 83 |
sbuf.append( getIntroIndent( level, '[', isPositioned ) ); |
| 84 |
boolean isPos = ! isPositioned; |
| 85 |
for ( Iterator it = list.iterator(); it.hasNext(); ) { |
| 86 |
toJson( sbuf, it.next(), level + 1, isPos ); |
| 87 |
if ( it.hasNext() ) { |
| 88 |
sbuf.append( "," ); |
| 89 |
} |
| 90 |
isPos = false; |
| 91 |
} |
| 92 |
sbuf.append( spc_ + "]" ); |
| 93 |
} |
| 94 |
} |
| 95 |
else if ( item instanceof Map ) { |
| 96 |
Map map = (Map) item; |
| 97 |
if ( map.isEmpty() ) { |
| 98 |
if ( ! isPositioned ) { |
| 99 |
sbuf.append( getIndent( level ) ); |
| 100 |
} |
| 101 |
sbuf.append( "{}" ); |
| 102 |
} |
| 103 |
else { |
| 104 |
sbuf.append( getIntroIndent( level, '{', isPositioned ) ); |
| 105 |
boolean isPos = ! isPositioned; |
| 106 |
for ( Iterator it = map.entrySet().iterator(); it.hasNext(); ) { |
| 107 |
Map.Entry entry = (Map.Entry) it.next(); |
| 108 |
Object key = entry.getKey(); |
| 109 |
if ( ! ( key instanceof String ) ) { |
| 110 |
throw new DataException( "Non-string key in map:" |
| 111 |
+ key ); |
| 112 |
} |
| 113 |
toJson( sbuf, key, level + 1, isPos ); |
| 114 |
sbuf.append( ":" + spc_ ); |
| 115 |
toJson( sbuf, entry.getValue(), level + 1, true ); |
| 116 |
if ( it.hasNext() ) { |
| 117 |
sbuf.append( "," ); |
| 118 |
} |
| 119 |
isPos = false; |
| 120 |
} |
| 121 |
sbuf.append( spc_ + "}" ); |
| 122 |
} |
| 123 |
} |
| 124 |
else { |
| 125 |
throw new DataException( "Illegal data type " + item ); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Returns prepended whitespace containing an opener character. |
| 131 |
* |
| 132 |
* @param level indentation level |
| 133 |
* @param chr opener character |
| 134 |
* @param isPositioned true if output should be direct to sbuf, |
| 135 |
* false if it needs a newline plus indentation first |
| 136 |
* @return string to prepend |
| 137 |
*/ |
| 138 |
private String getIntroIndent( int level, char chr, boolean isPositioned ) { |
| 139 |
if ( isPositioned ) { |
| 140 |
return new StringBuffer().append( chr ).toString(); |
| 141 |
} |
| 142 |
else { |
| 143 |
StringBuffer sbuf = new StringBuffer(); |
| 144 |
sbuf.append( getIndent( level ) ); |
| 145 |
sbuf.append( chr ); |
| 146 |
for ( int ic = 0; ic < indent_ - 1; ic++ ) { |
| 147 |
sbuf.append( ' ' ); |
| 148 |
} |
| 149 |
return sbuf.toString(); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Returns prepended whitespace. |
| 155 |
* |
| 156 |
* @param level indentation level |
| 157 |
* @return string to prepend |
| 158 |
*/ |
| 159 |
private String getIndent( int level ) { |
| 160 |
if ( indent_ >= 0 ) { |
| 161 |
int nc = level * indent_; |
| 162 |
StringBuffer sbuf = new StringBuffer( nc + 1 ); |
| 163 |
sbuf.append( '\n' ); |
| 164 |
for ( int ic = 0; ic < nc; ic++ ) { |
| 165 |
sbuf.append( ' ' ); |
| 166 |
} |
| 167 |
return sbuf.toString(); |
| 168 |
} |
| 169 |
else { |
| 170 |
return ""; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
public static void main( String[] args ) { |
| 175 |
String txt = args[ 0 ]; |
| 176 |
Object item = new JsonReader().read( txt ); |
| 177 |
System.out.println( new JsonWriter().toJson( item ) ); |
| 178 |
} |
| 179 |
} |