Working with CBOR Dataformat in Jackson JSON Java Library
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-cbor</artifactId>
<version>2.12.5</version>
</dependency>
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
import java.io.File;
import java.io.IOException;
public class CBORExample {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper(new CBORFactory());
try {
byte[] cborData = mapper.writeValueAsBytes(new MyData("Hello", 42));
File file = new File("data.cbor");
mapper.writeValue(file, cborData);
System.out.println("CBOR data is written to file successfully!");
} catch (JsonProcessingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
static class MyData {
private String message;
private int value;
public MyData(String message, int value) {
this.message = message;
this.value = value;
}
}
}