Understand Camel :: CBOR framework in the Java library data serialization technology
Camel :: CBOR is a framework for data serialization in the Java class library.CBOR represented Concise Binary Object Repositionation (Representative of Simple Binary objects), which is a format for data transmission and storage on the network.It is a lightweight, interoperable and easy -to -resolution format. Compared with JSON, it has higher efficiency and smaller size.
Camel :: CBOR provides the function of converting the Java object to CBOR format and reaction to the Java object.It uses annotation -based methods to add annotations to the Java class to define serialization and derivativeization.
First, let's see a basic Java class as an example:
public class Person {
private String name;
private int age;
// getters and setters
}
In order to use CAMEL :: CBOR serialization and counter -sequence, we need to do the following steps:
1. Add the annotation of `@cberSerializable`, indicating that this class can perform CBOR serialization and derivativeization.
2. Use the annotation of the fields that need to be serialized and dependentized by using `@cborProperty`.
The modified sample code is as follows:
import com.fasterxml.jackson.dataformat.cbor.annotation.CborProperty;
import com.fasterxml.jackson.dataformat.cbor.annotation.CborSerializable;
@CborSerializable
public class Person {
@CborProperty("n")
private String name;
@CborProperty("a")
private int age;
// getters and setters
// Note: In order to enable CBOR Serializer to correctly sequenize the data back to the Java object, we need to provide a parameter -free constructor.
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
Now, we can sequence the `Person` object to the data in the CBOR format, as shown below:
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
import java.io.IOException;
public class CBORSerializationExample {
public static void main(String[] args) throws IOException {
// Create a CBORFACTORY object
CBORFactory cborFactory = new CBORFactory();
// Create an ObjectMapper object and configure the use of CBORFACTORY
ObjectMapper objectMapper = new ObjectMapper(cborFactory);
// Create Person objects
Person Person = New Person ("Zhang San", 25);
// The sequence of the Person object is the byte array in the CBOR format
byte[] cborData = objectMapper.writeValueAsBytes(person);
// Print CBOR data
System.out.println(new String(cborData));
}
}
The above code seizures the CBOR data.The result is similar to the three au` in the n autumn part.
Then, we can return the CBOR data to the Java object, as shown below:
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
import java.io.IOException;
public class CBORDeserializationExample {
public static void main(String[] args) throws IOException {
// Create a CBORFACTORY object
CBORFactory cborFactory = new CBORFactory();
// Create an ObjectMapper object and configure the use of CBORFACTORY
ObjectMapper objectMapper = new ObjectMapper(cborFactory);
// CBOR data
byte [] cbordata = "H autumn part n" .Getbytes ();
// Cable to the CBOR data back to Person object
Person person = objectMapper.readValue(cborData, Person.class);
// Print the attribute value of the Person object
System.out.println("Name: " + person.getName());
System.out.println("Age: " + person.getAge());
}
}
The above code will output the attribute value of the Person object after the output.The results are as follows:
Name: Zhang San
Age: 25
This is the basic process of serialization of data serialization in the Java library using Camel :: CBOR.By using annotations, we can flexibly control the use of CBOR formats for serialization and derivativeization.This allows us to transmit and store data efficiently between different systems.