<dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-cbor</artifactId> <version>2.13.0</version> </dependency> public class Person { private String name; private int age; public byte[] toCbor() throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(new CBORFactory()); return mapper.writeValueAsBytes(this); } public static Person fromCbor(byte[] cbor) throws IOException { ObjectMapper mapper = new ObjectMapper(new CBORFactory()); return mapper.readValue(cbor, Person.class); } } public class Main { public static void main(String[] args) throws IOException { byte[] cbor = person.toCbor(); System.out.println(Arrays.toString(cbor)); Person deserializedPerson = Person.fromCbor(cbor); System.out.println(deserializedPerson); } }


上一篇:
下一篇:
切换中文