Skip to main content

MessagePack Parsers

The MsgPackParser class is used to parse MessagePack into POJOs.

When to use it​

Use MsgPackParser to read MessagePack bytes written by MsgPackSerializer, or by any MessagePack library, into POJOs. The spaced-hex and Base64 instances accept the text forms of the same bytes.

Example​

import org.apache.juneau.marshall.msgpack.*;

public class MsgPackParserExample {

public static class Person {
public String name = "John Smith";
public int age = 45;
}

public static void main(String[] args) throws Exception {
// Serialize a bean to bytes, then parse the bytes back.
byte[] bytes = (byte[]) MsgPackSerializer.DEFAULT.write(new Person());
Person person = MsgPackParser.DEFAULT.read(bytes, Person.class);

System.out.println(person.name + " " + person.age);
}
}

Options​

The following pre-configured parsers are provided for convenience:

InstanceAccepts
MsgPackParser.DEFAULTBinary MessagePack.
MsgPackParser.DEFAULT_NATIVEBinary MessagePack, returning native types such as byte[] for binary values.
MsgPackParser.DEFAULT_SPACED_HEXMessagePack as a hex string.
MsgPackParser.DEFAULT_BASE64MessagePack as a Base64 string.

For anything else, start from the builder's create() method. The settings readers reach for first:

Builder methodEffect
nativeMode()Return native Java types for MessagePack values.
maxLength(int)Reject binary, string or collection values longer than this.
validateEnd()Fail if anything follows the parsed value.
beanDictionary(Class...)Classes the parser may instantiate from _type properties.
swaps(Class...)Object swaps that convert parsed values into other types.

The class hierarchy for the builder of this parser is below. Refer to the builder javadocs for the full set of configurable settings.

Context.BuilderMarshallingContextable.BuilderParser.BuilderInputStreamParser.BuilderMsgPackParser.Builder

See also​