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 | |
Stuart McCulloch | a21b9e8 | 2012-08-02 13:26:25 +0000 | [diff] [blame] | 62 | @Override |
| 63 | Object decode(Decoder dec, String s) throws Exception { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 64 | return s; |
| 65 | } |
| 66 | |
Stuart McCulloch | a21b9e8 | 2012-08-02 13:26:25 +0000 | [diff] [blame] | 67 | @Override |
| 68 | Object decode(Decoder dec, Number s) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 69 | return s.toString(); |
| 70 | } |
| 71 | |
Stuart McCulloch | a21b9e8 | 2012-08-02 13:26:25 +0000 | [diff] [blame] | 72 | @Override |
| 73 | Object decode(Decoder dec, boolean s) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 74 | return Boolean.toString(s); |
| 75 | } |
| 76 | |
Stuart McCulloch | a21b9e8 | 2012-08-02 13:26:25 +0000 | [diff] [blame] | 77 | @Override |
| 78 | Object decode(Decoder dec ) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 79 | return null; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * An object can be assigned to a string. This means that the stream is |
| 84 | * interpreted as the object but stored in its complete in the string. |
| 85 | */ |
| 86 | Object decodeObject(Decoder r) throws Exception { |
| 87 | return collect(r, '}'); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * An array can be assigned to a string. This means that the stream is |
| 92 | * interpreted as the array but stored in its complete in the string. |
| 93 | */ |
Stuart McCulloch | a21b9e8 | 2012-08-02 13:26:25 +0000 | [diff] [blame] | 94 | @Override |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 95 | Object decodeArray(Decoder r) throws Exception { |
| 96 | return collect(r, ']'); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Gather the input until you find the the closing character making sure |
| 101 | * that new blocks are are take care of. |
| 102 | * <p> |
| 103 | * This method parses the input for a complete block so that it can be |
| 104 | * stored in a string. This allows envelopes. |
| 105 | * |
| 106 | * @param isr |
| 107 | * @param c |
| 108 | * @return |
| 109 | * @throws Exception |
| 110 | */ |
Stuart McCulloch | d482610 | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 111 | private Object collect(Decoder isr, @SuppressWarnings("unused") char close) throws Exception { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 112 | boolean instring = false; |
| 113 | int level = 1; |
| 114 | StringBuilder sb = new StringBuilder(); |
| 115 | |
| 116 | int c = isr.current(); |
| 117 | while (c > 0 && level > 0) { |
| 118 | sb.append((char) c); |
| 119 | if (instring) |
| 120 | switch (c) { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 121 | case '"' : |
| 122 | instring = true; |
| 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 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 130 | case ']' : |
| 131 | case '}' : |
| 132 | level--; |
| 133 | break; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 134 | } |
| 135 | else |
| 136 | switch (c) { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 137 | case '"' : |
| 138 | instring = false; |
| 139 | break; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 140 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 141 | case '\\' : |
| 142 | sb.append((char) isr.read()); |
| 143 | break; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | c = isr.read(); |
| 147 | } |
| 148 | return sb.toString(); |
| 149 | } |
| 150 | |
| 151 | } |