Maven: <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-cbor</artifactId> <version>2.13.0</version> </dependency> Gradle: groovy implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-cbor:2.13.0' import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.dataformat.cbor.CBORFactory; ObjectMapper objectMapper = new ObjectMapper(new CBORFactory()); JsonGenerator jsonGenerator = objectMapper.getFactory().createGenerator(System.out); JsonParser jsonParser = objectMapper.getFactory().createParser(new File("data.cbor")); MyObject myObject = new MyObject(); objectMapper.writeValue(jsonGenerator, myObject); MyObject deserializedObject = objectMapper.readValue(jsonParser, MyObject.class); import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonParser; 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) throws IOException { ObjectMapper objectMapper = new ObjectMapper(new CBORFactory()); JsonGenerator jsonGenerator = objectMapper.getFactory().createGenerator(System.out); JsonParser jsonParser = objectMapper.getFactory().createParser(new File("data.cbor")); MyObject myObject = new MyObject(); objectMapper.writeValue(jsonGenerator, myObject); MyObject deserializedObject = objectMapper.readValue(jsonParser, MyObject.class); } static class MyObject { private String name; private int age; public MyObject() { this.name = "John Doe"; this.age = 30; } } }


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