<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-cbor</artifactId>
<version>2.12.4</version>
</dependency>
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
public class CBORSerializationExample {
public static void main(String[] args) {
CBORFactory cborFactory = new CBORFactory();
ObjectMapper objectMapper = new ObjectMapper(cborFactory);
MyObject myObject = new MyObject("John Doe", 25);
try {
byte[] cborBytes = objectMapper.writeValueAsBytes(myObject);
System.out.println("Serialized CBOR bytes: " + Arrays.toString(cborBytes));
MyObject deserializedObject = objectMapper.readValue(cborBytes, MyObject.class);
System.out.println("Deserialized object: " + deserializedObject);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
static class MyObject {
private String name;
private int age;
public MyObject(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "MyObject{name='" + name + "', age=" + age + "}";
}
}
}