44package com .cognitect .transit .impl ;
55
66import com .cognitect .transit .*;
7- import org .msgpack .type . Value ;
8- import org . msgpack . type . ValueType ;
9- import org .msgpack .unpacker . Unpacker ;
7+ import org .msgpack .core . MessageUnpacker ;
8+
9+ import org .msgpack .value . ValueType ;
1010
1111import java .io .IOException ;
12- import java .math .BigInteger ;
12+ import java .nio .ByteBuffer ;
13+ import java .nio .charset .StandardCharsets ;
1314import java .util .List ;
1415import java .util .Map ;
1516
1617
1718public class MsgpackParser extends AbstractParser {
18- private final Unpacker mp ;
19+ private final MessageUnpacker mp ;
1920
20- public MsgpackParser (Unpacker mp ,
21+ public MsgpackParser (MessageUnpacker mp ,
2122 Map <String , ReadHandler <?,?>> handlers ,
2223 DefaultReadHandler defaultHandler ,
2324 MapReader <?, Map <Object , Object >, Object , Object > mapBuilder ,
@@ -27,16 +28,7 @@ public MsgpackParser(Unpacker mp,
2728 }
2829
2930 private Object parseLong () throws IOException {
30- Value val = mp .readValue ();
31-
32- try {
33- return val .asIntegerValue ().getLong ();
34- }
35- catch (Exception ex ) {
36- BigInteger bi = new BigInteger (val .asRawValue ().getString ());
37- }
38-
39- return val ;
31+ return mp .unpackLong ();
4032 }
4133
4234 @ Override
@@ -46,21 +38,21 @@ public Object parse(ReadCache cache) throws IOException {
4638
4739 @ Override
4840 public Object parseVal (boolean asMapKey , ReadCache cache ) throws IOException {
49- switch (mp .getNextType ()) {
41+ switch (mp .getNextFormat (). getValueType ()) {
5042 case MAP :
5143 return parseMap (asMapKey , cache , null );
5244 case ARRAY :
5345 return parseArray (asMapKey , cache , null );
54- case RAW :
55- return cache .cacheRead (mp .readValue (). asRawValue (). getString (), asMapKey , this );
46+ case STRING :
47+ return cache .cacheRead (mp .unpackString (), asMapKey , this );
5648 case INTEGER :
5749 return parseLong ();
5850 case FLOAT :
59- return mp .readValue (). asFloatValue (). getDouble ();
51+ return mp .unpackDouble ();
6052 case BOOLEAN :
61- return mp .readValue (). asBooleanValue (). getBoolean ();
53+ return mp .unpackBoolean ();
6254 case NIL :
63- mp .readNil ();
55+ mp .unpackNil ();
6456 }
6557
6658 return null ;
@@ -69,7 +61,7 @@ public Object parseVal(boolean asMapKey, ReadCache cache) throws IOException {
6961 @ Override
7062 public Object parseMap (boolean ignored , ReadCache cache , MapReadHandler <Object , ?, Object , Object , ?> handler ) throws IOException {
7163
72- int sz = this .mp .readMapBegin ();
64+ int sz = this .mp .unpackMapHeader ();
7365
7466 MapReader <Object , ?, Object , Object > mr = (handler != null ) ? handler .mapReader () : mapBuilder ;
7567
@@ -82,10 +74,10 @@ public Object parseMap(boolean ignored, ReadCache cache, MapReadHandler<Object,
8274 ReadHandler <Object , Object > val_handler = getHandler (tag );
8375 Object val ;
8476 if (val_handler != null ) {
85- if (this .mp .getNextType () == ValueType .MAP && val_handler instanceof MapReadHandler ) {
77+ if (this .mp .getNextFormat (). getValueType () == ValueType .MAP && val_handler instanceof MapReadHandler ) {
8678 // use map reader to decode value
8779 val = parseMap (false , cache , (MapReadHandler <Object , ?, Object , Object , ?>) val_handler );
88- } else if (this .mp .getNextType () == ValueType .ARRAY && val_handler instanceof ArrayReadHandler ) {
80+ } else if (this .mp .getNextFormat (). getValueType () == ValueType .ARRAY && val_handler instanceof ArrayReadHandler ) {
8981 // use array reader to decode value
9082 val = parseArray (false , cache , (ArrayReadHandler <Object , ?, Object , ?>) val_handler );
9183 } else {
@@ -97,21 +89,19 @@ public Object parseMap(boolean ignored, ReadCache cache, MapReadHandler<Object,
9789 val = this .decode (tag , parseVal (false , cache ));
9890 }
9991
100- this .mp .readMapEnd (true );
10192 return val ;
10293 } else {
10394 mb = mr .add (mb , key , parseVal (false , cache ));
10495 }
10596 }
10697
107- this .mp .readMapEnd (true );
10898 return mr .complete (mb );
10999 }
110100
111101 @ Override
112102 public Object parseArray (boolean ignored , ReadCache cache , ArrayReadHandler <Object , ?, Object , ?> handler ) throws IOException {
113103
114- int sz = this .mp .readArrayBegin ();
104+ int sz = this .mp .unpackArrayHeader ();
115105
116106 ArrayReader <Object , ?, Object > ar = (handler != null ) ? handler .arrayReader () : listBuilder ;
117107
@@ -124,10 +114,10 @@ public Object parseArray(boolean ignored, ReadCache cache, ArrayReadHandler<Obje
124114 String tag = ((Tag ) val ).getValue ();
125115 ReadHandler <Object , Object > val_handler = getHandler (tag );
126116 if (val_handler != null ) {
127- if (this .mp .getNextType () == ValueType .MAP && val_handler instanceof MapReadHandler ) {
117+ if (this .mp .getNextFormat (). getValueType () == ValueType .MAP && val_handler instanceof MapReadHandler ) {
128118 // use map reader to decode value
129119 val = parseMap (false , cache , (MapReadHandler <Object , ?, Object , Object , ?>) val_handler );
130- } else if (this .mp .getNextType () == ValueType .ARRAY && val_handler instanceof ArrayReadHandler ) {
120+ } else if (this .mp .getNextFormat (). getValueType () == ValueType .ARRAY && val_handler instanceof ArrayReadHandler ) {
131121 // use array reader to decode value
132122 val = parseArray (false , cache , (ArrayReadHandler <Object , ?, Object , ?>) val_handler );
133123 } else {
@@ -138,15 +128,13 @@ public Object parseArray(boolean ignored, ReadCache cache, ArrayReadHandler<Obje
138128 // default decode
139129 val = this .decode (tag , parseVal (false , cache ));
140130 }
141- this .mp .readArrayEnd ();
142131 return val ;
143132 } else {
144133 // fall through to regular parse
145134 ab = ar .add (ab , val );
146135 }
147136 }
148137
149- this .mp .readArrayEnd ();
150138 return ar .complete (ab );
151139 }
152140}
0 commit comments