Add jitpack repo:
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
Include with:
<dependency>
<groupId>com.github.peergos</groupId>
<artifactId>dag-cbor</artifactId>
<version>0.1.2</version>
</dependency>
public record CustomType(String name, long time, Multihash ref) implements Cborable {
@Override
public CborObject toCbor() {
SortedMap<String, Cborable> state = new TreeMap<>();
state.put("n", new CborObject.CborString(name));
state.put("t", new CborObject.CborLong(time));
state.put("r", new CborObject.CborMerkleLink(ref));
return CborObject.CborMap.build(state);
}
public static CustomType fromCbor(Cborable cbor) {
if (! (cbor instanceof CborObject.CborMap))
throw new IllegalStateException("Invalid cbor for CustomType! " + cbor);
CborObject.CborMap m = (CborObject.CborMap) cbor;
return new CustomType(m.getString("n"), m.getLong("t"), m.getMerkleLink("r"));
}
}
CustomType example = new CustomType("Hey", 12345L, new Cid(1, Cid.Codec.DagCbor, Multihash.Type.sha2_256, new byte[32]));
byte[] raw = example.serialize();
CustomType deserialized = CustomType.fromCbor(CborObject.fromByteArray(raw));
There is a benchmark giving results in the hundreds of MB/s!
Decode rate 265 MB/s for canada.json.dagcbor
Decode rate 123 MB/s for citm_catalog.json.dagcbor
Decode rate 204 MB/s for twitter.json.dagcbor
Encode rate 157 MB/s for canada.json.dagcbor
Encode rate 228 MB/s for citm_catalog.json.dagcbor
Encode rate 503 MB/s for twitter.json.dagcbor
Benchmark data was taken from https://github.com/DavidBuchanan314/dag-cbor-benchmark