|
| 1 | +// |
| 2 | +// MessagePack for Java |
| 3 | +// |
| 4 | +// Copyright (C) 2009-2011 FURUHASHI Sadayuki |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +package org.msgpack.unpacker; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.io.EOFException; |
| 22 | +import java.io.InputStream; |
| 23 | +import java.io.Reader; |
| 24 | +import java.io.InputStreamReader; |
| 25 | +import java.io.StringReader; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.Iterator; |
| 29 | +import java.math.BigInteger; |
| 30 | +import org.json.simple.parser.JSONParser; |
| 31 | +import org.json.simple.parser.ParseException; |
| 32 | +//import org.msgpack.io.Input; |
| 33 | +//import org.msgpack.io.StreamInput; |
| 34 | +import org.msgpack.MessagePack; |
| 35 | +import org.msgpack.MessageTypeException; |
| 36 | +import org.msgpack.type.Value; |
| 37 | +import org.msgpack.type.ValueFactory; |
| 38 | + |
| 39 | + |
| 40 | +public class JSONUnpacker extends Converter { |
| 41 | + protected Reader in; |
| 42 | + protected JSONParser parser; |
| 43 | + |
| 44 | + public JSONUnpacker(InputStream in) { |
| 45 | + this(new MessagePack(), in); |
| 46 | + } |
| 47 | + |
| 48 | + public JSONUnpacker(MessagePack msgpack, InputStream in) { |
| 49 | + this(msgpack, new InputStreamReader(in)); |
| 50 | + } |
| 51 | + |
| 52 | + JSONUnpacker(MessagePack msgpack, Reader in) { |
| 53 | + super(msgpack, null); |
| 54 | + this.in = in; |
| 55 | + this.parser = new JSONParser(); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + // FIXME throws IOException? |
| 60 | + protected Value nextValue() { |
| 61 | + try { |
| 62 | + Object obj = parser.parse(in); |
| 63 | + return objectToValue(obj); |
| 64 | + } catch (ParseException e) { // FIXME exception |
| 65 | + throw new MessageTypeException(e); |
| 66 | + } catch (IOException e) { // FIXME exception |
| 67 | + throw new MessageTypeException(e); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private Value objectToValue(Object obj) { |
| 72 | + if(obj instanceof String) { |
| 73 | + return ValueFactory.rawValue((String)obj); |
| 74 | + } else if(obj instanceof Integer) { |
| 75 | + return ValueFactory.integerValue((Integer)obj); |
| 76 | + } else if(obj instanceof Long) { |
| 77 | + return ValueFactory.integerValue((Long)obj); |
| 78 | + } else if(obj instanceof Map) { |
| 79 | + return mapToValue((Map)obj); |
| 80 | + } else if(obj instanceof List) { |
| 81 | + return listToValue((List)obj); |
| 82 | + } else if(obj instanceof Boolean) { |
| 83 | + return ValueFactory.booleanValue((Boolean)obj); |
| 84 | + } else if(obj instanceof Double) { |
| 85 | + return ValueFactory.floatValue((Double)obj); |
| 86 | + } else { |
| 87 | + return ValueFactory.nilValue(); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private Value listToValue(List list) { |
| 92 | + Value[] array = new Value[list.size()]; |
| 93 | + for(int i=0; i < array.length; i++) { |
| 94 | + array[i] = objectToValue(list.get(i)); |
| 95 | + } |
| 96 | + return ValueFactory.arrayValue(array, true); |
| 97 | + } |
| 98 | + |
| 99 | + private Value mapToValue(Map map) { |
| 100 | + Value[] kvs = new Value[map.size()*2]; |
| 101 | + Iterator<Map.Entry> it = map.entrySet().iterator(); |
| 102 | + for(int i=0; i < kvs.length; i+=2) { |
| 103 | + Map.Entry pair = it.next(); |
| 104 | + kvs[i] = objectToValue(pair.getKey()); |
| 105 | + kvs[i+1] = objectToValue(pair.getValue()); |
| 106 | + } |
| 107 | + return ValueFactory.mapValue(kvs, true); |
| 108 | + } |
| 109 | + |
| 110 | + public void close() throws IOException { |
| 111 | + in.close(); |
| 112 | + } |
| 113 | +} |
| 114 | + |
0 commit comments