org.msgpack.value.ValueType Java Examples

The following examples show how to use org.msgpack.value.ValueType. 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: MsgPack.java    From mercury with Apache License 2.0 4 votes vote down vote up
private Object unpackValue(MessageUnpacker unpacker, MessageFormat mf) throws IOException {
    ValueType type = mf.getValueType();
    switch (type) {
        case STRING:
            return unpacker.unpackString();

        case INTEGER:
            /*
             * msgPack compresses long value into integer or short value to save space
             * MessageFormat.POSFIXINT for value 0 - 255 --> Short
             * MessageFormat.UINT16 for value 0 - 2^16-1 --> Integer
             * MessageFormat.UINT32 for value 0 - 2^32-1 --> Integer
             * MessageFormat.UINT64 for value 0 - 2^64-1 --> Long
             *
             * For simplicity, restore it to long or integer
             */
            if (mf == MessageFormat.UINT64) {
                return unpacker.unpackLong();
            } else {
                return unpacker.unpackInt();
            }

        case FLOAT:
            /*
             * Similarly, restore the value to double or float
             */
            if (mf == MessageFormat.FLOAT64) {
                return unpacker.unpackDouble();
            } else {
                return unpacker.unpackFloat();
            }

        case BINARY:
            int bytesLen = unpacker.unpackBinaryHeader();
            byte[] bytesValue = new byte[bytesLen];
            unpacker.readPayload(bytesValue);
            return bytesValue;

        case BOOLEAN:
            return unpacker.unpackBoolean();

        case NIL:
            unpacker.unpackNil();
            return null;

        default:
            // for simplicity, custom types are not supported
            unpacker.skipValue();
            return null;
    }
}
 
Example #5
Source File: MsgpackGenerator.java    From protostuff with Apache License 2.0 4 votes vote down vote up
@Override
public ValueType getValueType()
{
    return ValueType.ARRAY;
}
 
Example #6
Source File: MsgpackGenerator.java    From protostuff with Apache License 2.0 4 votes vote down vote up
@Override
public ValueType getValueType()
{
    return ValueType.FLOAT;
}