Skip to content

Instantly share code, notes, and snippets.

@sl2820
Last active July 12, 2019 08:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sl2820/72fe74af044e971eba819994e8b2de14 to your computer and use it in GitHub Desktop.
Save sl2820/72fe74af044e971eba819994e8b2de14 to your computer and use it in GitHub Desktop.
void ff(MessageUnpacker unpacker) {
try{
while (unpacker.hasNext()) {
// [Advanced] You can check the detailed data format with getNextFormat()
// Here is a list of message pack data format: https://github.com/msgpack/msgpack/blob/master/spec.md#overview
MessageFormat format = unpacker.getNextFormat();
// You can also use unpackValue to extract a value of any type
Value v = unpacker.unpackValue();
switch (v.getValueType()) {
case NIL:
v.isNilValue(); // true
println("read nil");
break;
case BOOLEAN:
boolean b = v.asBooleanValue().getBoolean();
println("read boolean: " + b);
break;
case INTEGER:
IntegerValue iv = v.asIntegerValue();
if (iv.isInIntRange()) {
int i = iv.toInt();
println("read int: " + i);
}
else if (iv.isInLongRange()) {
long l = iv.toLong();
println("read long: " + l);
}
else {
BigInteger i = iv.toBigInteger();
println("read long: " + i);
}
break;
case FLOAT:
FloatValue fv = v.asFloatValue();
float f = fv.toFloat(); // use as float
double d = fv.toDouble(); // use as double
println("read float: " + d);
break;
case STRING:
String s = v.asStringValue().asString();
println("read string: " + s);
break;
case BINARY:
byte[] mb = v.asBinaryValue().asByteArray();
println("read binary: size=" + mb.length);
break;
case ARRAY:
ArrayValue a = v.asArrayValue();
for (Value e : a) {
println("read array element: " + e);
}
break;
case MAP:
int length = unpacker.unpackMapHeader();
for (int i = 0; i < length; i++) {
unpacker.unpackString(); //key
unpacker.unpackString(); //value
}
break;
case EXTENSION:
ExtensionValue ev = v.asExtensionValue();
byte extType = ev.getType();
byte[] extValue = ev.getData();
break;
}
}
} catch(IOException e) {
println("ERROR");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment