Java Code Examples for org.msgpack.value.ValueType#ARRAY

The following examples show how to use org.msgpack.value.ValueType#ARRAY . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MsgPack.java    From mercury with Apache License 2.0 6 votes vote down vote up
/**
 * Optimized unpack method for generic map or list object
 *
 * @param bytes - packed structure
 * @param offset of array
 * @param length from offset
 * @return result - Map or List
 * @throws IOException for mapping exception
 */
public Object unpack(byte[] bytes, int offset, int length) throws IOException  {
    MessageUnpacker unpacker = null;
    try {
        unpacker = MessagePack.newDefaultUnpacker(bytes, offset, length);
        if (unpacker.hasNext()) {
            MessageFormat mf = unpacker.getNextFormat();
            ValueType type = mf.getValueType();
            if (type == ValueType.MAP) {
                return unpack(unpacker, new HashMap<>());
            } else if (type == ValueType.ARRAY) {
                return unpack(unpacker, new ArrayList<>());
            } else {
                throw new MessageFormatException("Packed input should be Map or List, Actual: "+type);
            }
        }
        unpacker.close();
        unpacker = null;
    } finally {
        if (unpacker != null) {
            unpacker.close();
        }
    }
    // this should not occur
    return new HashMap<String, Object>();
}
 
Example 2
Source File: MsgPack.java    From mercury with Apache License 2.0 6 votes vote down vote up
private Map<String, Object> unpack(MessageUnpacker unpacker, Map<String, Object> map) throws IOException {
    int n = unpacker.unpackMapHeader();
    for (int i=0; i < n; i++) {
        String key = unpacker.unpackString();
        MessageFormat mf = unpacker.getNextFormat();
        ValueType type = mf.getValueType();
        if (type == ValueType.MAP) {
            Map<String, Object> submap = new HashMap<>();
            map.put(key, submap);
            unpack(unpacker, submap);
        } else if (type == ValueType.ARRAY) {
            List<Object> array = new ArrayList<>();
            map.put(key, array);
            unpack(unpacker, array);
        } else {
            // skip null value
            Object value = unpackValue(unpacker, mf);
            if (value != null) {
                map.put(key, value);
            }
        }
    }
    return map;
}
 
Example 3
Source File: MsgPack.java    From mercury with Apache License 2.0 6 votes vote down vote up
private List<Object> unpack(MessageUnpacker unpacker, List<Object> list) throws IOException {
    int len = unpacker.unpackArrayHeader();
    for (int i=0; i < len; i++) {
        MessageFormat mf = unpacker.getNextFormat();
        ValueType type = mf.getValueType();
        if (type == ValueType.MAP) {
            Map<String, Object> submap = new HashMap<>();
            list.add(submap);
            unpack(unpacker, submap);
        } else if (type == ValueType.ARRAY) {
            List<Object> array = new ArrayList<>();
            list.add(array);
            unpack(unpacker, array);
        } else {
            // null value is allowed to preserve the original sequence of the list
            list.add(unpackValue(unpacker, mf));
        }
    }
    return list;
}
 
Example 4
Source File: MsgpackGenerator.java    From protostuff with Apache License 2.0 4 votes vote down vote up
@Override
public ValueType getValueType()
{
    return ValueType.ARRAY;
}