| 1 | |
package org.webslinger.json; |
| 2 | |
|
| 3 | |
import java.io.IOException; |
| 4 | |
import java.io.Writer; |
| 5 | |
import java.lang.reflect.Array; |
| 6 | |
import java.util.Collection; |
| 7 | |
import java.util.Iterator; |
| 8 | |
import java.util.Map; |
| 9 | |
|
| 10 | |
import org.webslinger.io.IndentingWriter; |
| 11 | |
|
| 12 | |
public class JSONWriter { |
| 13 | |
private final IndentingWriter writer; |
| 14 | |
private final FallbackHandler fallbackHandler; |
| 15 | |
|
| 16 | |
public JSONWriter(IndentingWriter writer) { |
| 17 | 14 | this(writer, StandardFallbackHandler); |
| 18 | 14 | } |
| 19 | |
|
| 20 | 14 | public JSONWriter(IndentingWriter writer, FallbackHandler fallbackHandler) { |
| 21 | 14 | this.writer = writer; |
| 22 | 14 | this.fallbackHandler = fallbackHandler; |
| 23 | 14 | } |
| 24 | |
|
| 25 | |
public JSONWriter(Writer writer) { |
| 26 | 13 | this(writer instanceof IndentingWriter ? (IndentingWriter) writer : new IndentingWriter(writer)); |
| 27 | 13 | } |
| 28 | |
|
| 29 | |
public JSONWriter(Writer writer, FallbackHandler fallbackHandler) { |
| 30 | 0 | this(writer instanceof IndentingWriter ? (IndentingWriter) writer : new IndentingWriter(writer), fallbackHandler); |
| 31 | 0 | } |
| 32 | |
|
| 33 | |
public IndentingWriter getWriter() { |
| 34 | 0 | return writer; |
| 35 | |
} |
| 36 | |
|
| 37 | |
public JSONWriter close() throws IOException { |
| 38 | 0 | getWriter().close(); |
| 39 | 0 | return this; |
| 40 | |
} |
| 41 | |
|
| 42 | |
public JSONWriter write(byte b) throws IOException { |
| 43 | 1 | writer.write(Byte.toString(b)); |
| 44 | 1 | return this; |
| 45 | |
} |
| 46 | |
|
| 47 | |
public JSONWriter write(short s) throws IOException { |
| 48 | 1 | writer.write(Short.toString(s)); |
| 49 | 1 | return this; |
| 50 | |
} |
| 51 | |
|
| 52 | |
public JSONWriter write(int i) throws IOException { |
| 53 | 1 | writer.write(Integer.toString(i)); |
| 54 | 1 | return this; |
| 55 | |
} |
| 56 | |
|
| 57 | |
public JSONWriter write(long l) throws IOException { |
| 58 | 4 | writer.write(Long.toString(l)); |
| 59 | 4 | return this; |
| 60 | |
} |
| 61 | |
|
| 62 | |
public JSONWriter write(float f) throws IOException { |
| 63 | 0 | writer.write(Float.toString(f)); |
| 64 | 0 | return this; |
| 65 | |
} |
| 66 | |
|
| 67 | |
public JSONWriter write(double d) throws IOException { |
| 68 | 3 | writer.write(Double.toString(d)); |
| 69 | 3 | return this; |
| 70 | |
} |
| 71 | |
|
| 72 | |
public JSONWriter write(char c) throws IOException { |
| 73 | 1 | write(Character.toString(c)); |
| 74 | 1 | return this; |
| 75 | |
} |
| 76 | |
|
| 77 | |
public JSONWriter write(String s) throws IOException { |
| 78 | 7 | writer.write('"'); |
| 79 | 130 | for (int i = 0; i < s.length(); i++) { |
| 80 | 123 | char c = s.charAt(i); |
| 81 | 123 | switch (c) { |
| 82 | 1 | case '\\': writer.write("\\\\"); continue; |
| 83 | 1 | case '/': writer.write("\\/"); continue; |
| 84 | 1 | case '"': writer.write("\\\""); continue; |
| 85 | 1 | case '\b': writer.write("\\b"); continue; |
| 86 | 1 | case '\f': writer.write("\\f"); continue; |
| 87 | 1 | case '\n': writer.write("\\n"); continue; |
| 88 | 1 | case '\r': writer.write("\\r"); continue; |
| 89 | 1 | case '\t': writer.write("\\t"); continue; |
| 90 | |
} |
| 91 | 115 | if (32 <= c && c >= 256) { |
| 92 | 1 | writer.write("\\u"); |
| 93 | 1 | String n = Integer.toString((int) c, 16); |
| 94 | 1 | for (int j = 4 - n.length(); j > 0; j--) writer.write('0'); |
| 95 | 1 | writer.write(n); |
| 96 | 1 | } else { |
| 97 | 114 | writer.write(c); |
| 98 | |
} |
| 99 | |
} |
| 100 | 7 | writer.write('"'); |
| 101 | 7 | return this; |
| 102 | |
} |
| 103 | |
|
| 104 | |
public <K, V> JSONWriter write(Map<K, V> m) throws IOException { |
| 105 | 1 | writer.write('{'); |
| 106 | 1 | writer.push(); |
| 107 | 1 | Iterator<Map.Entry<K, V>> it = m.entrySet().iterator(); |
| 108 | 1 | if (it.hasNext()) writer.newline(); |
| 109 | 2 | while (it.hasNext()) { |
| 110 | 1 | Map.Entry<K, V> entry = it.next(); |
| 111 | 1 | write(entry.getKey()); |
| 112 | 1 | writer.write(':'); |
| 113 | 1 | writer.space(); |
| 114 | 1 | write(entry.getValue()); |
| 115 | 1 | if (it.hasNext()) writer.write(','); |
| 116 | 1 | writer.newline(); |
| 117 | 1 | } |
| 118 | 1 | writer.pop(); |
| 119 | 1 | writer.write('}'); |
| 120 | 1 | return this; |
| 121 | |
} |
| 122 | |
|
| 123 | |
public <E> JSONWriter write(Collection<E> c) throws IOException { |
| 124 | 2 | writer.write('['); |
| 125 | 2 | writer.push(); |
| 126 | 2 | Iterator<E> it = c.iterator(); |
| 127 | 2 | if (it.hasNext()) writer.newline(); |
| 128 | 7 | while (it.hasNext()) { |
| 129 | 5 | write(it.next()); |
| 130 | 5 | if (it.hasNext()) writer.write(','); |
| 131 | 5 | writer.newline(); |
| 132 | |
} |
| 133 | 2 | writer.pop(); |
| 134 | 2 | writer.write(']'); |
| 135 | 2 | return this; |
| 136 | |
} |
| 137 | |
|
| 138 | |
public <T> JSONWriter write(T... o) throws IOException { |
| 139 | 1 | writer.write('['); |
| 140 | 1 | writer.push(); |
| 141 | 4 | for (int i = 0; i < o.length; i++) { |
| 142 | 3 | if (i != 0) writer.write(','); |
| 143 | 3 | writer.newline(); |
| 144 | 3 | write(o[i]); |
| 145 | |
} |
| 146 | 1 | if (o.length > 0) writer.newline(); |
| 147 | 1 | writer.pop(); |
| 148 | 1 | writer.write(']'); |
| 149 | 1 | return this; |
| 150 | |
} |
| 151 | |
|
| 152 | |
public JSONWriter write(Object o) throws IOException { |
| 153 | 24 | if (o == null) { |
| 154 | 1 | writer.write("null"); |
| 155 | 1 | return this; |
| 156 | 23 | } else if (o instanceof Boolean) { |
| 157 | 2 | writer.write(((Boolean) o).booleanValue() ? "true" : "false"); |
| 158 | 2 | return this; |
| 159 | 21 | } else if (o instanceof String) { |
| 160 | 6 | return write((String) o); |
| 161 | 15 | } else if (o instanceof Map) { |
| 162 | 1 | return write((Map) o); |
| 163 | 14 | } else if (o instanceof Collection) { |
| 164 | 2 | return write((Collection) o); |
| 165 | 12 | } else if (o instanceof Byte) { |
| 166 | 1 | return write(((Byte) o).byteValue()); |
| 167 | 11 | } else if (o instanceof Character) { |
| 168 | 1 | return write(((Character) o).charValue()); |
| 169 | 10 | } else if (o instanceof Double) { |
| 170 | 3 | return write(((Double) o).doubleValue()); |
| 171 | 7 | } else if (o instanceof Float) { |
| 172 | 0 | return write(((Float) o).floatValue()); |
| 173 | 7 | } else if (o instanceof Integer) { |
| 174 | 1 | return write(((Integer) o).intValue()); |
| 175 | 6 | } else if (o instanceof Long) { |
| 176 | 4 | return write(((Long) o).longValue()); |
| 177 | 2 | } else if (o instanceof Short) { |
| 178 | 1 | return write(((Short) o).shortValue()); |
| 179 | 1 | } else if (o.getClass().isArray()) { |
| 180 | 1 | return write((Object[]) o); |
| 181 | |
} else { |
| 182 | 0 | fallbackHandler.writeJSON(this, writer, o); |
| 183 | 0 | return this; |
| 184 | |
} |
| 185 | |
} |
| 186 | |
|
| 187 | |
public interface FallbackHandler { |
| 188 | |
void writeJSON(JSONWriter json, Writer writer, Object o) throws IOException; |
| 189 | |
} |
| 190 | |
|
| 191 | 1 | public static final FallbackHandler StandardFallbackHandler = new FallbackHandler() { |
| 192 | |
public void writeJSON(JSONWriter json, Writer writer, Object o) throws IOException { |
| 193 | 0 | throw new IOException("Can't write(" + o + ":" + o.getClass() + ")"); |
| 194 | |
} |
| 195 | |
}; |
| 196 | |
|
| 197 | 1 | public static final FallbackHandler ResolvingFallbackHandler = new FallbackHandler() { |
| 198 | |
public void writeJSON(JSONWriter json, Writer writer, Object o) throws IOException { |
| 199 | 0 | writer.write("resolve("); |
| 200 | 0 | json.write(o.getClass().getName()); |
| 201 | 0 | writer.write(")"); |
| 202 | 0 | json.write(o.toString()); |
| 203 | 0 | } |
| 204 | |
}; |
| 205 | |
} |