-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathMsgpackParser.java
More file actions
140 lines (116 loc) · 5.29 KB
/
MsgpackParser.java
File metadata and controls
140 lines (116 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright (c) Cognitect, Inc.
// All rights reserved.
package com.cognitect.transit.impl;
import com.cognitect.transit.*;
import org.msgpack.core.MessageUnpacker;
import org.msgpack.value.ValueType;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
public class MsgpackParser extends AbstractParser {
private final MessageUnpacker mp;
public MsgpackParser(MessageUnpacker mp,
Map<String, ReadHandler<?,?>> handlers,
DefaultReadHandler defaultHandler,
MapReader<?, Map<Object, Object>, Object, Object> mapBuilder,
ArrayReader<?, List<Object>, Object> listBuilder) {
super(handlers, defaultHandler, mapBuilder, listBuilder);
this.mp = mp;
}
private Object parseLong() throws IOException {
return mp.unpackLong();
}
@Override
public Object parse(ReadCache cache) throws IOException {
return parseVal(false, cache);
}
@Override
public Object parseVal(boolean asMapKey, ReadCache cache) throws IOException {
switch (mp.getNextFormat().getValueType()) {
case MAP:
return parseMap(asMapKey, cache, null);
case ARRAY:
return parseArray(asMapKey, cache, null);
case STRING:
return cache.cacheRead(mp.unpackString(), asMapKey, this);
case INTEGER:
return parseLong();
case FLOAT:
return mp.unpackDouble();
case BOOLEAN:
return mp.unpackBoolean();
case NIL:
mp.unpackNil();
}
return null;
}
@Override
public Object parseMap(boolean ignored, ReadCache cache, MapReadHandler<Object, ?, Object, Object, ?> handler) throws IOException {
int sz = this.mp.unpackMapHeader();
MapReader<Object, ?, Object, Object> mr = (handler != null) ? handler.mapReader() : mapBuilder;
Object mb = mr.init(sz);
for (int remainder = sz; remainder > 0; remainder--) {
Object key = parseVal(true, cache);
if (key instanceof Tag) {
String tag = ((Tag)key).getValue();
ReadHandler<Object, Object> val_handler = getHandler(tag);
Object val;
if (val_handler != null) {
if (this.mp.getNextFormat().getValueType() == ValueType.MAP && val_handler instanceof MapReadHandler) {
// use map reader to decode value
val = parseMap(false, cache, (MapReadHandler<Object, ?, Object, Object, ?>) val_handler);
} else if (this.mp.getNextFormat().getValueType() == ValueType.ARRAY && val_handler instanceof ArrayReadHandler) {
// use array reader to decode value
val = parseArray(false, cache, (ArrayReadHandler<Object, ?, Object, ?>) val_handler);
} else {
// read value and decode normally
val = val_handler.fromRep(parseVal(false, cache));
}
} else {
// default decode
val = this.decode(tag, parseVal(false, cache));
}
return val;
} else {
mb = mr.add(mb, key, parseVal(false, cache));
}
}
return mr.complete(mb);
}
@Override
public Object parseArray(boolean ignored, ReadCache cache, ArrayReadHandler<Object, ?, Object, ?> handler) throws IOException {
int sz = this.mp.unpackArrayHeader();
ArrayReader<Object, ?, Object> ar = (handler != null) ? handler.arrayReader() : listBuilder;
Object ab = ar.init(sz);
for (int remainder = sz; remainder > 0; remainder--) {
Object val = parseVal(false, cache);
if ((val != null) && (val instanceof Tag)) {
// it's a tagged value
String tag = ((Tag) val).getValue();
ReadHandler<Object, Object> val_handler = getHandler(tag);
if (val_handler != null) {
if (this.mp.getNextFormat().getValueType() == ValueType.MAP && val_handler instanceof MapReadHandler) {
// use map reader to decode value
val = parseMap(false, cache, (MapReadHandler<Object, ?, Object, Object, ?>) val_handler);
} else if (this.mp.getNextFormat().getValueType() == ValueType.ARRAY && val_handler instanceof ArrayReadHandler) {
// use array reader to decode value
val = parseArray(false, cache, (ArrayReadHandler<Object, ?, Object, ?>) val_handler);
} else {
// read value and decode normally
val = val_handler.fromRep(parseVal(false, cache));
}
} else {
// default decode
val = this.decode(tag, parseVal(false, cache));
}
return val;
} else {
// fall through to regular parse
ab = ar.add(ab, val);
}
}
return ar.complete(ab);
}
}