Mercurial Hosting > luan
annotate src/goodjava/json/JsonToString.java @ 2018:5203eb9b0e02
http utf-8
| author | Franklin Schmidt <fschmidt@gmail.com> | 
|---|---|
| date | Fri, 03 Oct 2025 16:45:33 -0600 | 
| parents | e4440c286af6 | 
| children | 
| rev | line source | 
|---|---|
| 1402 
27efb1fcbcb5
move luan.lib to goodjava
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1307diff
changeset | 1 package goodjava.json; | 
| 720 | 2 | 
| 3 import java.util.List; | |
| 4 import java.util.Map; | |
| 758 
c29d11d675fd
added Json.toString() and rpc now sends tables as json
 Franklin Schmidt <fschmidt@gmail.com> parents: 
720diff
changeset | 5 import java.util.Iterator; | 
| 720 | 6 | 
| 7 | |
| 1419 
59fd2e8b1b9d
stringify and json_string
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1402diff
changeset | 8 public final class JsonToString { | 
| 
59fd2e8b1b9d
stringify and json_string
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1402diff
changeset | 9 public boolean compressed = false; | 
| 
59fd2e8b1b9d
stringify and json_string
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1402diff
changeset | 10 | 
| 1113 | 11 public static final class JsonException extends RuntimeException { | 
| 12 private JsonException(String msg) { | |
| 13 super(msg); | |
| 14 } | |
| 15 } | |
| 16 | |
| 1419 
59fd2e8b1b9d
stringify and json_string
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1402diff
changeset | 17 public String toString(Object obj) throws JsonException { | 
| 758 
c29d11d675fd
added Json.toString() and rpc now sends tables as json
 Franklin Schmidt <fschmidt@gmail.com> parents: 
720diff
changeset | 18 StringBuilder sb = new StringBuilder(); | 
| 1419 
59fd2e8b1b9d
stringify and json_string
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1402diff
changeset | 19 toString(obj,sb,0); | 
| 1132 
b70102bab110
add json_compressed_string()
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1117diff
changeset | 20 return sb.toString(); | 
| 
b70102bab110
add json_compressed_string()
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1117diff
changeset | 21 } | 
| 
b70102bab110
add json_compressed_string()
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1117diff
changeset | 22 | 
| 
b70102bab110
add json_compressed_string()
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1117diff
changeset | 23 private void toString(Object obj,StringBuilder sb,int indented) throws JsonException { | 
| 758 
c29d11d675fd
added Json.toString() and rpc now sends tables as json
 Franklin Schmidt <fschmidt@gmail.com> parents: 
720diff
changeset | 24 if( obj == null || obj instanceof Boolean || obj instanceof Number ) { | 
| 
c29d11d675fd
added Json.toString() and rpc now sends tables as json
 Franklin Schmidt <fschmidt@gmail.com> parents: 
720diff
changeset | 25 sb.append(obj); | 
| 
c29d11d675fd
added Json.toString() and rpc now sends tables as json
 Franklin Schmidt <fschmidt@gmail.com> parents: 
720diff
changeset | 26 return; | 
| 
c29d11d675fd
added Json.toString() and rpc now sends tables as json
 Franklin Schmidt <fschmidt@gmail.com> parents: 
720diff
changeset | 27 } | 
| 
c29d11d675fd
added Json.toString() and rpc now sends tables as json
 Franklin Schmidt <fschmidt@gmail.com> parents: 
720diff
changeset | 28 if( obj instanceof String ) { | 
| 
c29d11d675fd
added Json.toString() and rpc now sends tables as json
 Franklin Schmidt <fschmidt@gmail.com> parents: 
720diff
changeset | 29 toString((String)obj,sb); | 
| 
c29d11d675fd
added Json.toString() and rpc now sends tables as json
 Franklin Schmidt <fschmidt@gmail.com> parents: 
720diff
changeset | 30 return; | 
| 
c29d11d675fd
added Json.toString() and rpc now sends tables as json
 Franklin Schmidt <fschmidt@gmail.com> parents: 
720diff
changeset | 31 } | 
| 1113 | 32 if( obj instanceof List ) { | 
| 1117 
9a1aa6fc0b4e
remove json_pretty_string and make json_string somewhat pretty
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1116diff
changeset | 33 toString((List)obj,sb,indented); | 
| 758 
c29d11d675fd
added Json.toString() and rpc now sends tables as json
 Franklin Schmidt <fschmidt@gmail.com> parents: 
720diff
changeset | 34 return; | 
| 
c29d11d675fd
added Json.toString() and rpc now sends tables as json
 Franklin Schmidt <fschmidt@gmail.com> parents: 
720diff
changeset | 35 } | 
| 1113 | 36 if( obj instanceof Map ) { | 
| 1117 
9a1aa6fc0b4e
remove json_pretty_string and make json_string somewhat pretty
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1116diff
changeset | 37 toString((Map)obj,sb,indented); | 
| 1113 | 38 return; | 
| 39 } | |
| 40 throw new JsonException("can't handle type "+obj.getClass().getName()); | |
| 758 
c29d11d675fd
added Json.toString() and rpc now sends tables as json
 Franklin Schmidt <fschmidt@gmail.com> parents: 
720diff
changeset | 41 } | 
| 
c29d11d675fd
added Json.toString() and rpc now sends tables as json
 Franklin Schmidt <fschmidt@gmail.com> parents: 
720diff
changeset | 42 | 
| 1295 | 43 private static void toString(final String s,StringBuilder sb) { | 
| 44 sb.append('"'); | |
| 758 
c29d11d675fd
added Json.toString() and rpc now sends tables as json
 Franklin Schmidt <fschmidt@gmail.com> parents: 
720diff
changeset | 45 for( int i=0; i<s.length(); i++ ) { | 
| 
c29d11d675fd
added Json.toString() and rpc now sends tables as json
 Franklin Schmidt <fschmidt@gmail.com> parents: 
720diff
changeset | 46 char c = s.charAt(i); | 
| 
c29d11d675fd
added Json.toString() and rpc now sends tables as json
 Franklin Schmidt <fschmidt@gmail.com> parents: 
720diff
changeset | 47 switch(c) { | 
| 
c29d11d675fd
added Json.toString() and rpc now sends tables as json
 Franklin Schmidt <fschmidt@gmail.com> parents: 
720diff
changeset | 48 case '"': | 
| 
c29d11d675fd
added Json.toString() and rpc now sends tables as json
 Franklin Schmidt <fschmidt@gmail.com> parents: 
720diff
changeset | 49 sb.append("\\\""); | 
| 
c29d11d675fd
added Json.toString() and rpc now sends tables as json
 Franklin Schmidt <fschmidt@gmail.com> parents: 
720diff
changeset | 50 break; | 
| 1307 | 51 case '\\': | 
| 52 sb.append("\\\\"); | |
| 53 break; | |
| 54 case '\b': | |
| 55 sb.append("\\b"); | |
| 56 break; | |
| 57 case '\f': | |
| 58 sb.append("\\f"); | |
| 59 break; | |
| 60 case '\n': | |
| 61 sb.append("\\n"); | |
| 62 break; | |
| 63 case '\r': | |
| 64 sb.append("\\r"); | |
| 65 break; | |
| 66 case '\t': | |
| 67 sb.append("\\t"); | |
| 68 break; | |
| 1836 | 69 case 0: | 
| 70 sb.append("\\u0000"); | |
| 71 break; | |
| 1307 | 72 default: | 
| 73 sb.append(c); | |
| 74 } | |
| 75 } | |
| 76 sb.append('"'); | |
| 77 } | |
| 78 | |
| 79 public static String javascriptEncode(String s) { | |
| 80 StringBuilder sb = new StringBuilder(); | |
| 81 for( int i=0; i<s.length(); i++ ) { | |
| 82 char c = s.charAt(i); | |
| 83 switch(c) { | |
| 84 case '"': | |
| 85 sb.append("\\\""); | |
| 86 break; | |
| 87 case '\'': // added for javascript | |
| 1295 | 88 sb.append("\\'"); | 
| 89 break; | |
| 758 
c29d11d675fd
added Json.toString() and rpc now sends tables as json
 Franklin Schmidt <fschmidt@gmail.com> parents: 
720diff
changeset | 90 case '\\': | 
| 
c29d11d675fd
added Json.toString() and rpc now sends tables as json
 Franklin Schmidt <fschmidt@gmail.com> parents: 
720diff
changeset | 91 sb.append("\\\\"); | 
| 
c29d11d675fd
added Json.toString() and rpc now sends tables as json
 Franklin Schmidt <fschmidt@gmail.com> parents: 
720diff
changeset | 92 break; | 
| 
c29d11d675fd
added Json.toString() and rpc now sends tables as json
 Franklin Schmidt <fschmidt@gmail.com> parents: 
720diff
changeset | 93 case '\b': | 
| 
c29d11d675fd
added Json.toString() and rpc now sends tables as json
 Franklin Schmidt <fschmidt@gmail.com> parents: 
720diff
changeset | 94 sb.append("\\b"); | 
| 
c29d11d675fd
added Json.toString() and rpc now sends tables as json
 Franklin Schmidt <fschmidt@gmail.com> parents: 
720diff
changeset | 95 break; | 
| 
c29d11d675fd
added Json.toString() and rpc now sends tables as json
 Franklin Schmidt <fschmidt@gmail.com> parents: 
720diff
changeset | 96 case '\f': | 
| 
c29d11d675fd
added Json.toString() and rpc now sends tables as json
 Franklin Schmidt <fschmidt@gmail.com> parents: 
720diff
changeset | 97 sb.append("\\f"); | 
| 
c29d11d675fd
added Json.toString() and rpc now sends tables as json
 Franklin Schmidt <fschmidt@gmail.com> parents: 
720diff
changeset | 98 break; | 
| 
c29d11d675fd
added Json.toString() and rpc now sends tables as json
 Franklin Schmidt <fschmidt@gmail.com> parents: 
720diff
changeset | 99 case '\n': | 
| 
c29d11d675fd
added Json.toString() and rpc now sends tables as json
 Franklin Schmidt <fschmidt@gmail.com> parents: 
720diff
changeset | 100 sb.append("\\n"); | 
| 
c29d11d675fd
added Json.toString() and rpc now sends tables as json
 Franklin Schmidt <fschmidt@gmail.com> parents: 
720diff
changeset | 101 break; | 
| 
c29d11d675fd
added Json.toString() and rpc now sends tables as json
 Franklin Schmidt <fschmidt@gmail.com> parents: 
720diff
changeset | 102 case '\r': | 
| 
c29d11d675fd
added Json.toString() and rpc now sends tables as json
 Franklin Schmidt <fschmidt@gmail.com> parents: 
720diff
changeset | 103 sb.append("\\r"); | 
| 
c29d11d675fd
added Json.toString() and rpc now sends tables as json
 Franklin Schmidt <fschmidt@gmail.com> parents: 
720diff
changeset | 104 break; | 
| 
c29d11d675fd
added Json.toString() and rpc now sends tables as json
 Franklin Schmidt <fschmidt@gmail.com> parents: 
720diff
changeset | 105 case '\t': | 
| 
c29d11d675fd
added Json.toString() and rpc now sends tables as json
 Franklin Schmidt <fschmidt@gmail.com> parents: 
720diff
changeset | 106 sb.append("\\t"); | 
| 
c29d11d675fd
added Json.toString() and rpc now sends tables as json
 Franklin Schmidt <fschmidt@gmail.com> parents: 
720diff
changeset | 107 break; | 
| 
c29d11d675fd
added Json.toString() and rpc now sends tables as json
 Franklin Schmidt <fschmidt@gmail.com> parents: 
720diff
changeset | 108 default: | 
| 
c29d11d675fd
added Json.toString() and rpc now sends tables as json
 Franklin Schmidt <fschmidt@gmail.com> parents: 
720diff
changeset | 109 sb.append(c); | 
| 
c29d11d675fd
added Json.toString() and rpc now sends tables as json
 Franklin Schmidt <fschmidt@gmail.com> parents: 
720diff
changeset | 110 } | 
| 
c29d11d675fd
added Json.toString() and rpc now sends tables as json
 Franklin Schmidt <fschmidt@gmail.com> parents: 
720diff
changeset | 111 } | 
| 1307 | 112 return sb.toString(); | 
| 758 
c29d11d675fd
added Json.toString() and rpc now sends tables as json
 Franklin Schmidt <fschmidt@gmail.com> parents: 
720diff
changeset | 113 } | 
| 
c29d11d675fd
added Json.toString() and rpc now sends tables as json
 Franklin Schmidt <fschmidt@gmail.com> parents: 
720diff
changeset | 114 | 
| 1132 
b70102bab110
add json_compressed_string()
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1117diff
changeset | 115 private void toString(List list,StringBuilder sb,int indented) { | 
| 1116 | 116 sb.append('['); | 
| 117 if( !list.isEmpty() ) { | |
| 118 indent(sb,indented+1); | |
| 119 toString(list.get(0),sb,indented+1); | |
| 1485 | 120 final int n = list.size(); | 
| 121 for( int i=1; i<n; i++ ) { | |
| 1518 
f989778ca9e1
stringify, json_string
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1485diff
changeset | 122 sb.append(','); | 
| 
f989778ca9e1
stringify, json_string
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1485diff
changeset | 123 indent(sb,indented+1); | 
| 1116 | 124 toString(list.get(i),sb,indented+1); | 
| 125 } | |
| 126 indent(sb,indented); | |
| 1115 
809d74db1415
add json_pretty_string
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1113diff
changeset | 127 } | 
| 
809d74db1415
add json_pretty_string
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1113diff
changeset | 128 sb.append(']'); | 
| 
809d74db1415
add json_pretty_string
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1113diff
changeset | 129 return; | 
| 
809d74db1415
add json_pretty_string
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1113diff
changeset | 130 } | 
| 
809d74db1415
add json_pretty_string
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1113diff
changeset | 131 | 
| 1132 
b70102bab110
add json_compressed_string()
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1117diff
changeset | 132 private void toString(Map map,StringBuilder sb,int indented) throws JsonException { | 
| 1115 
809d74db1415
add json_pretty_string
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1113diff
changeset | 133 sb.append('{'); | 
| 1116 | 134 if( !map.isEmpty() ) { | 
| 135 Iterator<Map.Entry> i = map.entrySet().iterator(); | |
| 1115 
809d74db1415
add json_pretty_string
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1113diff
changeset | 136 indent(sb,indented+1); | 
| 
809d74db1415
add json_pretty_string
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1113diff
changeset | 137 toString(i.next(),sb,indented+1); | 
| 1116 | 138 while( i.hasNext() ) { | 
| 139 sb.append(','); | |
| 140 indent(sb,indented+1); | |
| 141 toString(i.next(),sb,indented+1); | |
| 142 } | |
| 143 indent(sb,indented); | |
| 1115 
809d74db1415
add json_pretty_string
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1113diff
changeset | 144 } | 
| 
809d74db1415
add json_pretty_string
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1113diff
changeset | 145 sb.append('}'); | 
| 
809d74db1415
add json_pretty_string
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1113diff
changeset | 146 } | 
| 
809d74db1415
add json_pretty_string
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1113diff
changeset | 147 | 
| 1132 
b70102bab110
add json_compressed_string()
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1117diff
changeset | 148 private void toString(Map.Entry entry,StringBuilder sb,int indented) throws JsonException { | 
| 1115 
809d74db1415
add json_pretty_string
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1113diff
changeset | 149 Object key = entry.getKey(); | 
| 
809d74db1415
add json_pretty_string
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1113diff
changeset | 150 if( !(key instanceof String) ) | 
| 1477 | 151 throw new JsonException("table keys must be strings but got "+key.getClass().getSimpleName()+" ("+key+"="+entry.getValue()+")"); | 
| 1115 
809d74db1415
add json_pretty_string
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1113diff
changeset | 152 toString((String)key,sb); | 
| 1518 
f989778ca9e1
stringify, json_string
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1485diff
changeset | 153 sb.append( compressed ? ":" : ": " ); | 
| 1115 
809d74db1415
add json_pretty_string
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1113diff
changeset | 154 toString(entry.getValue(),sb,indented); | 
| 
809d74db1415
add json_pretty_string
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1113diff
changeset | 155 } | 
| 
809d74db1415
add json_pretty_string
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1113diff
changeset | 156 | 
| 1419 
59fd2e8b1b9d
stringify and json_string
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1402diff
changeset | 157 private void indent(StringBuilder sb,int indented) { | 
| 
59fd2e8b1b9d
stringify and json_string
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1402diff
changeset | 158 if( compressed ) | 
| 
59fd2e8b1b9d
stringify and json_string
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1402diff
changeset | 159 return; | 
| 1117 
9a1aa6fc0b4e
remove json_pretty_string and make json_string somewhat pretty
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1116diff
changeset | 160 sb.append('\n'); | 
| 
9a1aa6fc0b4e
remove json_pretty_string and make json_string somewhat pretty
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1116diff
changeset | 161 for( int i=0; i<indented; i++ ) { | 
| 
9a1aa6fc0b4e
remove json_pretty_string and make json_string somewhat pretty
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1116diff
changeset | 162 sb.append('\t'); | 
| 
9a1aa6fc0b4e
remove json_pretty_string and make json_string somewhat pretty
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1116diff
changeset | 163 } | 
| 
9a1aa6fc0b4e
remove json_pretty_string and make json_string somewhat pretty
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1116diff
changeset | 164 } | 
| 1115 
809d74db1415
add json_pretty_string
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1113diff
changeset | 165 | 
| 720 | 166 } | 
