comparison src/goodjava/json/JsonToString.java @ 1419:59fd2e8b1b9d

stringify and json_string
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 25 Oct 2019 22:12:06 -0600
parents 27efb1fcbcb5
children 509736ad42e6
comparison
equal deleted inserted replaced
1418:732b5de211fc 1419:59fd2e8b1b9d
3 import java.util.List; 3 import java.util.List;
4 import java.util.Map; 4 import java.util.Map;
5 import java.util.Iterator; 5 import java.util.Iterator;
6 6
7 7
8 public class JsonToString { 8 public final class JsonToString {
9 public boolean compressed = false;
10
11 private String colon;
12 private String comma;
9 13
10 public static final class JsonException extends RuntimeException { 14 public static final class JsonException extends RuntimeException {
11 private JsonException(String msg) { 15 private JsonException(String msg) {
12 super(msg); 16 super(msg);
13 } 17 }
14 } 18 }
15 19
16 public static String toString(Object obj) throws JsonException { 20 public String toString(Object obj) throws JsonException {
21 colon = compressed ? ":" : ": ";
22 comma = compressed ? "," : ", ";
17 StringBuilder sb = new StringBuilder(); 23 StringBuilder sb = new StringBuilder();
18 new JsonToString().toString(obj,sb,0); 24 toString(obj,sb,0);
19 sb.append('\n'); 25 if( !compressed )
20 return sb.toString(); 26 sb.append('\n');
21 }
22
23 public static String toCompressedString(Object obj) throws JsonException {
24 StringBuilder sb = new StringBuilder();
25 JsonToString jts = new JsonToString() {
26 void indent(StringBuilder sb,int indented) {}
27 };
28 jts.toString(obj,sb,0);
29 return sb.toString(); 27 return sb.toString();
30 } 28 }
31 29
32 private void toString(Object obj,StringBuilder sb,int indented) throws JsonException { 30 private void toString(Object obj,StringBuilder sb,int indented) throws JsonException {
33 if( obj == null || obj instanceof Boolean || obj instanceof Number ) { 31 if( obj == null || obj instanceof Boolean || obj instanceof Number ) {
122 sb.append('['); 120 sb.append('[');
123 if( !list.isEmpty() ) { 121 if( !list.isEmpty() ) {
124 indent(sb,indented+1); 122 indent(sb,indented+1);
125 toString(list.get(0),sb,indented+1); 123 toString(list.get(0),sb,indented+1);
126 for( int i=1; i<list.size(); i++ ) { 124 for( int i=1; i<list.size(); i++ ) {
127 sb.append(", "); 125 sb.append(comma);
128 toString(list.get(i),sb,indented+1); 126 toString(list.get(i),sb,indented+1);
129 } 127 }
130 indent(sb,indented); 128 indent(sb,indented);
131 } 129 }
132 sb.append(']'); 130 sb.append(']');
152 private void toString(Map.Entry entry,StringBuilder sb,int indented) throws JsonException { 150 private void toString(Map.Entry entry,StringBuilder sb,int indented) throws JsonException {
153 Object key = entry.getKey(); 151 Object key = entry.getKey();
154 if( !(key instanceof String) ) 152 if( !(key instanceof String) )
155 throw new JsonException("table keys must be strings"); 153 throw new JsonException("table keys must be strings");
156 toString((String)key,sb); 154 toString((String)key,sb);
157 sb.append(": "); 155 sb.append(colon);
158 toString(entry.getValue(),sb,indented); 156 toString(entry.getValue(),sb,indented);
159 } 157 }
160 158
161 void indent(StringBuilder sb,int indented) { 159 private void indent(StringBuilder sb,int indented) {
160 if( compressed )
161 return;
162 sb.append('\n'); 162 sb.append('\n');
163 for( int i=0; i<indented; i++ ) { 163 for( int i=0; i<indented; i++ ) {
164 sb.append('\t'); 164 sb.append('\t');
165 } 165 }
166 } 166 }
167 167
168 private JsonToString() {}
169 } 168 }