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.security.*; |
| 6 | import java.util.*; |
| 7 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 8 | import aQute.lib.converter.*; |
| 9 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 10 | public class Decoder implements Closeable { |
| 11 | final JSONCodec codec; |
| 12 | Reader reader; |
| 13 | int current; |
| 14 | MessageDigest digest; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 15 | Map<String,Object> extra; |
| 16 | String encoding = "UTF-8"; |
| 17 | |
| 18 | boolean strict; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 19 | |
| 20 | Decoder(JSONCodec codec) { |
| 21 | this.codec = codec; |
| 22 | } |
| 23 | |
| 24 | public Decoder from(File file) throws Exception { |
| 25 | return from(new FileInputStream(file)); |
| 26 | } |
| 27 | |
| 28 | public Decoder from(InputStream in) throws Exception { |
| 29 | return from(new InputStreamReader(in, encoding)); |
| 30 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 31 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 32 | public Decoder charset(String encoding) { |
| 33 | this.encoding = encoding; |
| 34 | return this; |
| 35 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 36 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 37 | public Decoder strict() { |
| 38 | this.strict = true; |
| 39 | return this; |
| 40 | } |
| 41 | |
| 42 | public Decoder from(Reader in) throws Exception { |
| 43 | reader = in; |
| 44 | read(); |
| 45 | return this; |
| 46 | } |
| 47 | |
| 48 | public Decoder faq(String in) throws Exception { |
| 49 | return from(in.replace('\'', '"')); |
| 50 | } |
| 51 | |
| 52 | public Decoder from(String in) throws Exception { |
| 53 | return from(new StringReader(in)); |
| 54 | } |
| 55 | |
| 56 | public Decoder mark() throws NoSuchAlgorithmException { |
| 57 | if (digest == null) |
| 58 | digest = MessageDigest.getInstance("SHA1"); |
| 59 | digest.reset(); |
| 60 | return this; |
| 61 | } |
| 62 | |
| 63 | public byte[] digest() { |
| 64 | if (digest == null) |
| 65 | return null; |
| 66 | |
| 67 | return digest.digest(); |
| 68 | } |
| 69 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 70 | public <T> T get(Class<T> clazz) throws Exception { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 71 | return (T) codec.decode(clazz, this); |
| 72 | } |
| 73 | |
| 74 | public Object get(Type type) throws Exception { |
| 75 | return codec.decode(type, this); |
| 76 | } |
| 77 | |
| 78 | public Object get() throws Exception { |
| 79 | return codec.decode(null, this); |
| 80 | } |
| 81 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 82 | public <T> T get(TypeReference<T> ref) throws Exception { |
| 83 | return (T) codec.decode(ref.getType(), this); |
| 84 | } |
| 85 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 86 | int read() throws Exception { |
| 87 | current = reader.read(); |
| 88 | if (digest != null) { |
| 89 | digest.update((byte) (current / 256)); |
| 90 | digest.update((byte) (current % 256)); |
| 91 | } |
| 92 | return current; |
| 93 | } |
| 94 | |
| 95 | int current() { |
| 96 | return current; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Skip any whitespace. |
| 101 | * |
| 102 | * @return |
| 103 | * @throws Exception |
| 104 | */ |
| 105 | int skipWs() throws Exception { |
| 106 | while (Character.isWhitespace(current())) |
| 107 | read(); |
| 108 | return current(); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Skip any whitespace. |
| 113 | * |
| 114 | * @return |
| 115 | * @throws Exception |
| 116 | */ |
| 117 | int next() throws Exception { |
| 118 | read(); |
| 119 | return skipWs(); |
| 120 | } |
| 121 | |
| 122 | void expect(String s) throws Exception { |
| 123 | for (int i = 0; i < s.length(); i++) |
| 124 | if (!(s.charAt(i) == read())) |
| 125 | throw new IllegalArgumentException("Expected " + s + " but got something different"); |
| 126 | read(); |
| 127 | } |
| 128 | |
| 129 | public boolean isEof() throws Exception { |
| 130 | int c = skipWs(); |
| 131 | return c < 0; |
| 132 | } |
| 133 | |
| 134 | public void close() throws IOException { |
| 135 | reader.close(); |
| 136 | } |
| 137 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 138 | public Map<String,Object> getExtra() { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 139 | if (extra == null) |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 140 | extra = new HashMap<String,Object>(); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 141 | return extra; |
| 142 | } |
| 143 | } |