blob: 7622ae713832841acb67fa73ffb01640b5c44690 [file] [log] [blame]
Stuart McCullochf3173222012-06-07 21:57:32 +00001package aQute.lib.json;
2
3import java.io.*;
4import java.lang.reflect.*;
5import java.util.*;
6
Stuart McCulloch55fbda52012-08-02 13:26:25 +00007import aQute.lib.hex.*;
Stuart McCullochf3173222012-06-07 21:57:32 +00008
Stuart McCulloch2a0afd62012-09-06 18:28:06 +00009/**
10 *
11 * Will now use hex for encoding byte arrays
12 *
13 */
Stuart McCullochf3173222012-06-07 21:57:32 +000014public class ByteArrayHandler extends Handler {
Stuart McCulloch4482c702012-06-15 13:27:53 +000015 @Override
16 void encode(Encoder app, Object object, Map<Object,Type> visited) throws IOException, Exception {
Stuart McCulloch2a0afd62012-09-06 18:28:06 +000017 StringHandler.string(app, Hex.toHexString((byte[]) object));
Stuart McCullochf3173222012-06-07 21:57:32 +000018 }
19
Stuart McCulloch4482c702012-06-15 13:27:53 +000020 @Override
21 Object decodeArray(Decoder r) throws Exception {
Stuart McCullochf3173222012-06-07 21:57:32 +000022 ByteArrayOutputStream out = new ByteArrayOutputStream();
23
24 ArrayList<Object> list = new ArrayList<Object>();
25 r.codec.parseArray(list, Byte.class, r);
26 for (Object b : list) {
27 out.write(((Byte) b).byteValue());
28 }
29 return out.toByteArray();
30 }
31
Stuart McCulloch4482c702012-06-15 13:27:53 +000032 @Override
Stuart McCulloch55fbda52012-08-02 13:26:25 +000033 Object decode(Decoder dec, String s) throws Exception {
Stuart McCulloch2a0afd62012-09-06 18:28:06 +000034 return Hex.toByteArray(s);
Stuart McCullochf3173222012-06-07 21:57:32 +000035 }
36}