Mercurial Hosting > luan
comparison src/luan/lib/json/JsonToString.java @ 1132:b70102bab110
add json_compressed_string()
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 02 Jan 2018 21:30:41 -0700 |
parents | 9a1aa6fc0b4e |
children | 9dca1e912658 |
comparison
equal
deleted
inserted
replaced
1131:46732cc0ab87 | 1132:b70102bab110 |
---|---|
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 final class JsonToString { | 8 public class JsonToString { |
9 | 9 |
10 public static final class JsonException extends RuntimeException { | 10 public static final class JsonException extends RuntimeException { |
11 private JsonException(String msg) { | 11 private JsonException(String msg) { |
12 super(msg); | 12 super(msg); |
13 } | 13 } |
14 } | 14 } |
15 | 15 |
16 public static String toString(Object obj) throws JsonException { | 16 public static String toString(Object obj) throws JsonException { |
17 StringBuilder sb = new StringBuilder(); | 17 StringBuilder sb = new StringBuilder(); |
18 toString(obj,sb,0); | 18 new JsonToString().toString(obj,sb,0); |
19 sb.append('\n'); | 19 sb.append('\n'); |
20 return sb.toString(); | 20 return sb.toString(); |
21 } | 21 } |
22 | 22 |
23 private static void toString(Object obj,StringBuilder sb,int indented) throws JsonException { | 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(); | |
30 } | |
31 | |
32 private void toString(Object obj,StringBuilder sb,int indented) throws JsonException { | |
24 if( obj == null || obj instanceof Boolean || obj instanceof Number ) { | 33 if( obj == null || obj instanceof Boolean || obj instanceof Number ) { |
25 sb.append(obj); | 34 sb.append(obj); |
26 return; | 35 return; |
27 } | 36 } |
28 if( obj instanceof String ) { | 37 if( obj instanceof String ) { |
38 return; | 47 return; |
39 } | 48 } |
40 throw new JsonException("can't handle type "+obj.getClass().getName()); | 49 throw new JsonException("can't handle type "+obj.getClass().getName()); |
41 } | 50 } |
42 | 51 |
43 private static void toString(final String s,StringBuilder sb) { | 52 private void toString(final String s,StringBuilder sb) { |
44 sb.append('"'); | 53 sb.append('"'); |
45 for( int i=0; i<s.length(); i++ ) { | 54 for( int i=0; i<s.length(); i++ ) { |
46 char c = s.charAt(i); | 55 char c = s.charAt(i); |
47 switch(c) { | 56 switch(c) { |
48 case '"': | 57 case '"': |
71 } | 80 } |
72 } | 81 } |
73 sb.append('"'); | 82 sb.append('"'); |
74 } | 83 } |
75 | 84 |
76 private static void toString(List list,StringBuilder sb,int indented) { | 85 private void toString(List list,StringBuilder sb,int indented) { |
77 sb.append('['); | 86 sb.append('['); |
78 if( !list.isEmpty() ) { | 87 if( !list.isEmpty() ) { |
79 indent(sb,indented+1); | 88 indent(sb,indented+1); |
80 toString(list.get(0),sb,indented+1); | 89 toString(list.get(0),sb,indented+1); |
81 for( int i=1; i<list.size(); i++ ) { | 90 for( int i=1; i<list.size(); i++ ) { |
86 } | 95 } |
87 sb.append(']'); | 96 sb.append(']'); |
88 return; | 97 return; |
89 } | 98 } |
90 | 99 |
91 private static void toString(Map map,StringBuilder sb,int indented) throws JsonException { | 100 private void toString(Map map,StringBuilder sb,int indented) throws JsonException { |
92 sb.append('{'); | 101 sb.append('{'); |
93 if( !map.isEmpty() ) { | 102 if( !map.isEmpty() ) { |
94 Iterator<Map.Entry> i = map.entrySet().iterator(); | 103 Iterator<Map.Entry> i = map.entrySet().iterator(); |
95 indent(sb,indented+1); | 104 indent(sb,indented+1); |
96 toString(i.next(),sb,indented+1); | 105 toString(i.next(),sb,indented+1); |
102 indent(sb,indented); | 111 indent(sb,indented); |
103 } | 112 } |
104 sb.append('}'); | 113 sb.append('}'); |
105 } | 114 } |
106 | 115 |
107 private static void toString(Map.Entry entry,StringBuilder sb,int indented) throws JsonException { | 116 private void toString(Map.Entry entry,StringBuilder sb,int indented) throws JsonException { |
108 Object key = entry.getKey(); | 117 Object key = entry.getKey(); |
109 if( !(key instanceof String) ) | 118 if( !(key instanceof String) ) |
110 throw new JsonException("table keys must be strings"); | 119 throw new JsonException("table keys must be strings"); |
111 toString((String)key,sb); | 120 toString((String)key,sb); |
112 sb.append(": "); | 121 sb.append(": "); |
113 toString(entry.getValue(),sb,indented); | 122 toString(entry.getValue(),sb,indented); |
114 } | 123 } |
115 | 124 |
116 private static void indent(StringBuilder sb,int indented) { | 125 void indent(StringBuilder sb,int indented) { |
117 sb.append('\n'); | 126 sb.append('\n'); |
118 for( int i=0; i<indented; i++ ) { | 127 for( int i=0; i<indented; i++ ) { |
119 sb.append('\t'); | 128 sb.append('\t'); |
120 } | 129 } |
121 } | 130 } |
122 | 131 |
123 private JsonToString() {} // never | 132 private JsonToString() {} |
124 } | 133 } |