Jon framework application instance: realize data serialization and counter -serialization in the Java class library
The Jon framework is an open source Java object navigation framework, which provides a lightweight method to achieve the serialization and derivativeization of the object.Through the JON framework, we can convert the Java object into a serialized format in a easy -to -read and easy -to -understand way, so that it is more convenient to store, transmit and reuse the object.
In the Java library, it is a common demand to achieve data serialization and dependentization.Let's explain how to use the JON framework to implement these functions through an example.
Suppose we have a Person class that contains some basic personal information, such as names, age, and gender.We hope to sequence the Person object into a JSON format and save it into the file.At the same time, we also hope to read JSON data from the file and turn its back -sequence into Person objects.
First, we need to introduce the Jon framework in the project.You can manage dependence through Maven, you only need to add the following code to the pom.xml file:
<dependency>
<groupId>com.github.jonframework</groupId>
<artifactId>jon</artifactId>
<version>1.0.0</version>
</dependency>
Next, we create Person classes and define names, age and gender attributes in the class, as well as corresponding Getter and Setter methods:
public class Person {
private String name;
private int age;
private String gender;
// The constructor is omitted with the Getter and Setter methods
}
Now, let's see how to sequence the Person object to JSON, and then save it into the file:
import com.github.jonframework.JsonSerializer;
public class SerializationExample {
public static void main(String[] args) {
Person Person = New Person ("Zhang San", 25, "Male");
String jsonString = JsonSerializer.serialize(person);
try (FileWriter fileWriter = new FileWriter("person.json")) {
fileWriter.write(jsonString);
} catch (IOException e) {
e.printStackTrace();
}
}
}
In the above code, we use the Serialize method of the JSONSERIZER class to convert the Person object to a JSON string and write the JSON string into the file with FileWriter.
Next, let's see how to read JSON data from the file and turn its back -sequentially sequencing to Person object:
import com.github.jonframework.JsonDeserializer;
public class DeserializationExample {
public static void main(String[] args) {
try (FileReader fileReader = new FileReader("person.json")) {
StringBuilder jsonString = new StringBuilder();
int data;
while ((data = fileReader.read()) != -1) {
jsonString.append((char) data);
}
Person person = JsonDeserializer.deserialize(jsonString.toString(), Person.class);
// You can continue to operate the Person object, such as output name, age, and gender
System.out.println(person.getName());
System.out.println(person.getAge());
System.out.println(person.getGender());
} catch (IOException e) {
e.printStackTrace();
}
}
}
In the above code, we read files that saved JSON data through FileReader and gradually read the data into a StringBuilder.Then, we use the Deseerialize method of the JSONDESERIZER class to turn the JSON string back -sequencing into the Person object.
Through the above examples, we can see that it is very simple to achieve data serialization and back -serialization through the JON framework in the Java class library.We only need to introduce the Jon framework dependencies, and then use the JSONSERIALIZER class to sequence the object to the JSON string, or use the JSONDESERIZER class to turn the JSON string derivative into the object.This enables us to easily handle the storage, transmission and reuse of data, while improving the readability and maintenance of the code.