msgpack-java
msgpack-java copied to clipboard
How to implement nested objects and parsing?Examples are as follows
public class MessagePackTest {
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@ToString(callSuper = true)
public static class HistoryValueView {
private Long time;
private Integer qos;
private Object value;
private transient Long diff;
}
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@ToString(callSuper = true)
public static class HistoryView {
private String namespace;
private String tag;
private long total;
private List<HistoryValueView> data;
}
public static void main(String[] args) throws IOException {
List<HistoryView> list = Lists.newArrayList();
HistoryView object = new HistoryView();
object.setNamespace("unit05");
object.setTag("A1.AV");
object.setTotal(1);
object.setData(Collections.singletonList(new HistoryValueView(Instant.now().toEpochMilli(), 1, 250.0, 0L)));
MessageBufferPacker packer = MessagePack.newDefaultBufferPacker();
packer.packArrayHeader(list.size());
.......
packer.close();
MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(packer.toByteArray());
ImmutableArrayValue array = unpacker.unpackValue().asArrayValue();
.......
}
}