Skip to content

Commit d88ed52

Browse files
committed
added JSONUnpacker
1 parent a590028 commit d88ed52

File tree

7 files changed

+290
-30
lines changed

7 files changed

+290
-30
lines changed

src/main/java/org/msgpack/type/ValueFactory.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
import java.math.BigInteger;
2121
import java.nio.ByteBuffer;
22+
import java.util.List;
23+
import java.util.Map;
2224

2325
public final class ValueFactory {
2426
public static NilValue nilValue() {

src/main/java/org/msgpack/unpacker/BufferUnpacker.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ public interface BufferUnpacker extends Unpacker {
3030

3131
public BufferUnpacker wrap(ByteBuffer buf);
3232

33-
// TODO feed
33+
public BufferUnpacker feed(byte[] b);
34+
35+
public BufferUnpacker feed(byte[] b, boolean nocopy);
36+
37+
public BufferUnpacker feed(byte[] b, int off, int len);
38+
39+
public BufferUnpacker feed(byte[] b, int off, int len, boolean nocopy);
40+
41+
public BufferUnpacker feed(ByteBuffer b);
42+
43+
public BufferUnpacker feed(ByteBuffer buf, boolean nocopy);
3444
}
3545

src/main/java/org/msgpack/unpacker/Converter.java

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,28 @@ public class Converter extends AbstractUnpacker {
3434
protected Value value;
3535

3636
public Converter(Value value) {
37-
this(new MessagePack(), value);
37+
this(new MessagePack(), value);
3838
}
3939

4040
public Converter(MessagePack msgpack, Value value) {
41-
super(msgpack);
41+
super(msgpack);
4242
this.stack = new UnpackerStack();
4343
this.values = new Object[UnpackerStack.MAX_STACK_SIZE];
44-
this.values[0] = value;
4544
this.value = value;
4645
}
4746

47+
// FIXME throws IOException?
48+
protected Value nextValue() {
49+
// FIXME EOFError?
50+
throw new NullPointerException("Value is not set");
51+
}
52+
53+
private void ensureValue() {
54+
if(value == null) {
55+
value = nextValue();
56+
}
57+
}
58+
4859
@Override
4960
public boolean tryReadNil() {
5061
stack.checkCount();
@@ -60,9 +71,7 @@ public boolean tryReadNil() {
6071

6172
@Override
6273
public boolean trySkipNil() {
63-
if(value == null) {
64-
value = nextValue();
65-
}
74+
ensureValue();
6675

6776
if(stack.getDepth() > 0 && stack.getTopCount() <= 0) {
6877
// end of array or map
@@ -257,24 +266,16 @@ public void readMapEnd(boolean check) {
257266
}
258267
}
259268

260-
// FIXME throws IOException?
261-
protected Value nextValue() {
262-
// FIXME EOFError?
263-
throw new NullPointerException("Value is not set");
264-
}
265-
266269
private Value getTop() {
267-
if(value == null) {
268-
this.value = nextValue();
269-
}
270+
ensureValue();
270271

271272
stack.checkCount();
272273
if(stack.getDepth() == 0) {
273-
if(stack.getTopCount() < 0) {
274-
//throw new EOFException(); // TODO
275-
throw new RuntimeException(new EOFException());
276-
}
277-
return (Value) values[0];
274+
//if(stack.getTopCount() < 0) {
275+
// //throw new EOFException(); // TODO
276+
// throw new RuntimeException(new EOFException());
277+
//}
278+
return value;
278279
}
279280
Value[] array = (Value[]) values[stack.getDepth()];
280281
return array[array.length - stack.getTopCount()];
@@ -289,6 +290,7 @@ protected void readValue(Unconverter uc) {
289290
stack.checkCount();
290291
Value v = getTop();
291292
if(!v.isArray() && !v.isMap()) {
293+
uc.write(v);
292294
stack.reduceCount();
293295
if(stack.getDepth() == 0) {
294296
value = null;
@@ -299,7 +301,7 @@ protected void readValue(Unconverter uc) {
299301
}
300302

301303
while(true) {
302-
while(stack.getTopCount() == 0) {
304+
while(stack.getDepth() != 0 && stack.getTopCount() == 0) {
303305
if(stack.topIsArray()) {
304306
uc.writeArrayEnd(true);
305307
stack.pop();
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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.ByteArrayInputStream;
23+
import java.io.Reader;
24+
import java.io.InputStreamReader;
25+
import java.nio.ByteBuffer;
26+
import org.msgpack.MessagePack;
27+
import org.msgpack.MessageTypeException;
28+
import org.msgpack.type.Value;
29+
30+
31+
public class JSONBufferUnpacker extends JSONUnpacker implements BufferUnpacker {
32+
private static final int DEFAULT_BUFFER_SIZE = 512; // TODO default buffer size
33+
34+
public JSONBufferUnpacker() {
35+
this(DEFAULT_BUFFER_SIZE);
36+
}
37+
38+
public JSONBufferUnpacker(int bufferSize) {
39+
this(new MessagePack(), bufferSize);
40+
}
41+
42+
public JSONBufferUnpacker(MessagePack msgpack) {
43+
this(msgpack, DEFAULT_BUFFER_SIZE);
44+
}
45+
46+
public JSONBufferUnpacker(MessagePack msgpack, int bufferSize) {
47+
super(msgpack, (Reader)null);
48+
}
49+
50+
@Override
51+
protected Value nextValue() {
52+
if(in == null) {
53+
// FIXME exception
54+
throw new MessageTypeException(new EOFException());
55+
}
56+
return super.nextValue();
57+
}
58+
59+
public JSONBufferUnpacker wrap(byte[] b) {
60+
return wrap(b, 0, b.length);
61+
}
62+
63+
public JSONBufferUnpacker wrap(byte[] b, int off, int len) {
64+
ByteArrayInputStream in = new ByteArrayInputStream(b, off, len);
65+
this.in = new InputStreamReader(in);
66+
return this;
67+
}
68+
69+
public JSONBufferUnpacker wrap(ByteBuffer buf) {
70+
throw new UnsupportedOperationException("JSONBufferUnpacker doesn't support wrap(ByteBuffer buf)");
71+
}
72+
73+
public JSONBufferUnpacker feed(byte[] b) {
74+
throw new UnsupportedOperationException("JSONBufferUnpacker doesn't support feed()");
75+
}
76+
77+
public JSONBufferUnpacker feed(byte[] b, boolean nocopy) {
78+
throw new UnsupportedOperationException("JSONBufferUnpacker doesn't support feed()");
79+
}
80+
81+
public JSONBufferUnpacker feed(byte[] b, int off, int len) {
82+
throw new UnsupportedOperationException("JSONBufferUnpacker doesn't support feed()");
83+
}
84+
85+
public JSONBufferUnpacker feed(byte[] b, int off, int len, boolean nocopy) {
86+
throw new UnsupportedOperationException("JSONBufferUnpacker doesn't support feed()");
87+
}
88+
89+
public JSONBufferUnpacker feed(ByteBuffer buf) {
90+
throw new UnsupportedOperationException("JSONBufferUnpacker doesn't support feed()");
91+
}
92+
93+
public JSONBufferUnpacker feed(ByteBuffer buf, boolean nocopy) {
94+
throw new UnsupportedOperationException("JSONBufferUnpacker doesn't support feed()");
95+
}
96+
}
97+
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+

src/main/java/org/msgpack/unpacker/MessagePackBufferUnpacker.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,35 @@ public MessagePackBufferUnpacker wrap(ByteBuffer buf) {
5757
((LinkedBufferInput) in).feed(buf, true);
5858
return this;
5959
}
60+
61+
public MessagePackBufferUnpacker feed(byte[] b) {
62+
((LinkedBufferInput) in).feed(b);
63+
return this;
64+
}
65+
66+
public MessagePackBufferUnpacker feed(byte[] b, boolean nocopy) {
67+
((LinkedBufferInput) in).feed(b, nocopy);
68+
return this;
69+
}
70+
71+
public MessagePackBufferUnpacker feed(byte[] b, int off, int len) {
72+
((LinkedBufferInput) in).feed(b, off, len);
73+
return this;
74+
}
75+
76+
public MessagePackBufferUnpacker feed(byte[] b, int off, int len, boolean nocopy) {
77+
((LinkedBufferInput) in).feed(b, off, len, nocopy);
78+
return this;
79+
}
80+
81+
public MessagePackBufferUnpacker feed(ByteBuffer b) {
82+
((LinkedBufferInput) in).feed(b);
83+
return this;
84+
}
85+
86+
public MessagePackBufferUnpacker feed(ByteBuffer buf, boolean nocopy) {
87+
((LinkedBufferInput) in).feed(buf, nocopy);
88+
return this;
89+
}
6090
}
6191

0 commit comments

Comments
 (0)