Java Code Examples for org.msgpack.core.MessageBufferPacker#packMapHeader()

The following examples show how to use org.msgpack.core.MessageBufferPacker#packMapHeader() . 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: Message.java    From locust4j with MIT License 6 votes vote down vote up
public byte[] getBytes() throws IOException {
    MessageBufferPacker packer = MessagePack.newDefaultBufferPacker();
    Visitor visitor = new Visitor(packer);
    // a message contains three fields, (type & data & nodeID)
    packer.packArrayHeader(3);
    packer.packString(this.type);
    if (this.data != null) {
        packer.packMapHeader(this.data.size());
        for (Map.Entry<String, Object> entry : this.data.entrySet()) {
            packer.packString(entry.getKey());
            visitor.visit(entry.getValue());
        }
    } else {
        packer.packNil();
    }
    packer.packString(this.nodeID);
    byte[] bytes = packer.toByteArray();
    packer.close();
    return bytes;
}
 
Example 2
Source File: DataDstMsgPack.java    From xresloader with MIT License 6 votes vote down vote up
@Override
public final byte[] build(DataDstImpl compiler) throws ConvException {
    MessageBufferPacker packer = MessagePack.newDefaultBufferPacker();

    DataDstJava.DataDstObject data_obj = build_data(compiler);

    try {
        packer.packMapHeader(3);
        packer.packString("header");
        writeData(packer, data_obj.header);
        packer.packString("data_block");
        writeData(packer, data_obj.body);
        packer.packString("data_message_type");
        writeData(packer, data_obj.data_message_type);
    } catch (IOException e) {
        this.logErrorMessage("MessagePacker write failed.");
        e.printStackTrace();
    }

    return packer.toByteArray();
}
 
Example 3
Source File: Serializer.java    From tinkergraph-gremlin with Apache License 2.0 5 votes vote down vote up
/**
 * when deserializing, msgpack can't differentiate between e.g. int and long, so we need to encode the type as well - doing that with an array
 * i.e. format is: Map[PropertyName, Array(TypeId, PropertyValue)]
 */
private void packProperties(MessageBufferPacker packer, Map<String, Object> properties) throws IOException {
  packer.packMapHeader(properties.size());
  for (Map.Entry<String, Object> property : properties.entrySet()) {
    packer.packString(property.getKey());
    packPropertyValue(packer, property.getValue());
  }
}
 
Example 4
Source File: Serializer.java    From tinkergraph-gremlin with Apache License 2.0 5 votes vote down vote up
/**
 * format: two `Map<Label, Array<EdgeId>>`, i.e. one Map for `IN` and one for `OUT` edges
 */
private void packEdgeIds(final MessageBufferPacker packer,
                         final Map<String, TLongSet> edgeIdsByLabel) throws IOException {
  packer.packMapHeader(edgeIdsByLabel.size());
  for (Map.Entry<String, TLongSet> entry : edgeIdsByLabel.entrySet()) {
    final String label = entry.getKey();
    packer.packString(label);
    final TLongSet edgeIds = entry.getValue();
    packer.packArrayHeader(edgeIds.size());
    final TLongIterator edgeIdIter = edgeIds.iterator();
    while (edgeIdIter.hasNext()) {
      packer.packLong(edgeIdIter.next());
    }
  }
}
 
Example 5
Source File: DataDstMsgPack.java    From xresloader with MIT License 4 votes vote down vote up
@SuppressWarnings("unchecked")
private void writeData(MessageBufferPacker packer, Object data) throws IOException {
    // null
    if (null == data) {
        packer.packNil();
        return;
    }

    // 字符串&二进制
    if (data instanceof String) {
        packer.packString((String) data);
        return;
    }

    // 数字
    // 枚举值已被转为Java Long,会在这里执行
    if (data instanceof Integer) {
        packer.packInt((Integer) data);
        return;
    }
    if (data instanceof Long) {
        packer.packLong((long) data);
        return;
    }
    if (data instanceof Short) {
        packer.packLong((short) data);
        return;
    }
    if (data instanceof Float) {
        packer.packFloat((float) data);
        return;
    }
    if (data instanceof Double) {
        packer.packDouble((double) data);
        return;
    }
    if (data instanceof Byte) {
        packer.packByte((byte) data);
        return;
    }
    if (data instanceof java.math.BigInteger) {
        packer.packBigInteger((java.math.BigInteger) data);
        return;
    }

    // 布尔
    if (data instanceof Boolean) {
        packer.packBoolean((Boolean) data);
        return;
    }

    // 列表
    if (data instanceof List<?>) {
        packer.packArrayHeader(((List<?>) data).size());
        for (Object subobj : (List<?>) data) {
            writeData(packer, subobj);
        }
        return;
    }

    // Hashmap
    if (data instanceof Map<?, ?>) {
        Map<?, ?> mp = (Map<?, ?>) data;

        ArrayList<Map.Entry<?, ?>> sorted_array = new ArrayList<Map.Entry<?, ?>>();
        sorted_array.ensureCapacity(mp.size());
        sorted_array.addAll(mp.entrySet());
        sorted_array.sort((l, r) -> {
            if (l.getValue() instanceof Integer && r.getValue() instanceof Integer) {
                return ((Integer) l.getValue()).compareTo((Integer) r.getValue());
            }

            if (l.getKey() instanceof Integer && r.getKey() instanceof Integer) {
                return ((Integer) l.getKey()).compareTo((Integer) r.getKey());
            } else if (l.getKey() instanceof Long && r.getKey() instanceof Long) {
                return ((Long) l.getKey()).compareTo((Long) r.getKey());
            } else {
                return l.getKey().toString().compareTo(r.getKey().toString());
            }
        });

        packer.packMapHeader(sorted_array.size());
        for (Map.Entry<?, ?> item : sorted_array) {
            writeData(packer, item.getKey());
            writeData(packer, item.getValue());
        }

        return;
    }

    packer.packString(data.toString());
}