Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.lib.json; |
| 2 | |
| 3 | import java.io.*; |
| 4 | import java.lang.reflect.*; |
| 5 | import java.util.*; |
| 6 | |
| 7 | public class StringHandler extends Handler { |
| 8 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 9 | @Override |
| 10 | void encode(Encoder app, Object object, Map<Object,Type> visited) throws IOException { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 11 | string(app, object.toString()); |
| 12 | } |
| 13 | |
| 14 | static void string(Appendable app, String s) throws IOException { |
| 15 | |
| 16 | app.append('"'); |
| 17 | for (int i = 0; i < s.length(); i++) { |
| 18 | char c = s.charAt(i); |
| 19 | switch (c) { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 20 | case '"' : |
| 21 | app.append("\\\""); |
| 22 | break; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 23 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 24 | case '\\' : |
| 25 | app.append("\\\\"); |
| 26 | break; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 27 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 28 | case '\b' : |
| 29 | app.append("\\b"); |
| 30 | break; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 31 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 32 | case '\f' : |
| 33 | app.append("\\f"); |
| 34 | break; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 35 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 36 | case '\n' : |
| 37 | app.append("\\n"); |
| 38 | break; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 39 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 40 | case '\r' : |
| 41 | app.append("\\r"); |
| 42 | break; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 43 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 44 | case '\t' : |
| 45 | app.append("\\t"); |
| 46 | break; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 47 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 48 | default : |
| 49 | if (Character.isISOControl(c)) { |
| 50 | app.append("\\u"); |
| 51 | app.append("0123456789ABCDEF".charAt(0xF & (c >> 12))); |
| 52 | app.append("0123456789ABCDEF".charAt(0xF & (c >> 8))); |
| 53 | app.append("0123456789ABCDEF".charAt(0xF & (c >> 4))); |
| 54 | app.append("0123456789ABCDEF".charAt(0xF & (c >> 0))); |
| 55 | } else |
| 56 | app.append(c); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | app.append('"'); |
| 60 | } |
| 61 | |
| 62 | Object decode(String s) throws Exception { |
| 63 | return s; |
| 64 | } |
| 65 | |
| 66 | Object decode(Number s) { |
| 67 | return s.toString(); |
| 68 | } |
| 69 | |
| 70 | Object decode(boolean s) { |
| 71 | return Boolean.toString(s); |
| 72 | } |
| 73 | |
| 74 | Object decode() { |
| 75 | return null; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * An object can be assigned to a string. This means that the stream is |
| 80 | * interpreted as the object but stored in its complete in the string. |
| 81 | */ |
| 82 | Object decodeObject(Decoder r) throws Exception { |
| 83 | return collect(r, '}'); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * An array can be assigned to a string. This means that the stream is |
| 88 | * interpreted as the array but stored in its complete in the string. |
| 89 | */ |
| 90 | Object decodeArray(Decoder r) throws Exception { |
| 91 | return collect(r, ']'); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Gather the input until you find the the closing character making sure |
| 96 | * that new blocks are are take care of. |
| 97 | * <p> |
| 98 | * This method parses the input for a complete block so that it can be |
| 99 | * stored in a string. This allows envelopes. |
| 100 | * |
| 101 | * @param isr |
| 102 | * @param c |
| 103 | * @return |
| 104 | * @throws Exception |
| 105 | */ |
Stuart McCulloch | d482610 | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 106 | private Object collect(Decoder isr, @SuppressWarnings("unused") char close) throws Exception { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 107 | boolean instring = false; |
| 108 | int level = 1; |
| 109 | StringBuilder sb = new StringBuilder(); |
| 110 | |
| 111 | int c = isr.current(); |
| 112 | while (c > 0 && level > 0) { |
| 113 | sb.append((char) c); |
| 114 | if (instring) |
| 115 | switch (c) { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 116 | case '"' : |
| 117 | instring = true; |
| 118 | break; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 119 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 120 | case '[' : |
| 121 | case '{' : |
| 122 | level++; |
| 123 | break; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 124 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 125 | case ']' : |
| 126 | case '}' : |
| 127 | level--; |
| 128 | break; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 129 | } |
| 130 | else |
| 131 | switch (c) { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 132 | case '"' : |
| 133 | instring = false; |
| 134 | break; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 135 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 136 | case '\\' : |
| 137 | sb.append((char) isr.read()); |
| 138 | break; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | c = isr.read(); |
| 142 | } |
| 143 | return sb.toString(); |
| 144 | } |
| 145 | |
| 146 | } |