EasyGSON: Detailed explanation of data type conversion and mapping mechanism in the Java class library
EasyGSON: Detailed explanation of data type conversion and mapping mechanism in the Java class library
Overview:
EasyGson is a popular Java library that provides a simple and flexible way to convert Java objects into JSON format, and can also convert JSON format back to the Java object.One of the important features of EasyGson is its data type conversion and mapping mechanism, allowing developers to easily transform data between Java objects and JSON.
1. Map the basic data type:
Easygson can automatically convert the basic data types of Java (such as int, float, etc.) and their packaging classes (such as Integer, Float, etc.) into the corresponding JSON format.On the contrary, it can convert the original type in JSON back to the Java object.For example, the following code example demonstrates how to use EasyGSon to convert a Java object to a JSON string, and then analyze it back to the Java object:
import com.google.gson.Gson;
public class EasyGsonExample {
public static void main(String[] args) {
// Create a Java object
Person person = new Person("John", 25, true);
// Use Easygson to convert Java objects into json string
Gson gson = new Gson();
String json = gson.toJson(person);
System.out.println ("JSON string:" + json);
// Use EasyGSon to convert JSON string back to the Java object
Person newPerson = gson.fromJson(json, Person.class);
System.out.println ("Java object:" + newperson);
}
}
class Person {
private String name;
private int age;
private boolean isStudent;
public Person(String name, int age, boolean isStudent) {
this.name = name;
this.age = age;
this.isStudent = isStudent;
}
// Getter and Setter method omitted
}
Output results:
Json string: {"name": "John", "Age": 25, "isstudent": true}
Java object: Person {name = 'John', Age = 25, Isstudent = TRUE}
2. Custom type conversion:
In addition to the automatic conversion of basic data types, EasyGson also supports custom type conversion.Developers can achieve customized type conversion logic by implementing the `jsonserializer` and` jsondeserializer` interfaces.For example, if you want to convert a Java's `date` object into a string in a specified format, you can customize the converting example as follows:
import com.google.gson.*;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.Date;
public class EasyGsonExample {
public static void main(String[] args) {
// Create a Java object
Order order = new Order("12345", new Date());
// Use Easygson to convert Java objects into json string
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Date.class, new DateSerializer());
Gson gson = gsonBuilder.create();
String json = gson.toJson(order);
System.out.println ("JSON string:" + json);
// Use EasyGSon to convert JSON string back to the Java object
Order newOrder = gson.fromJson(json, Order.class);
System.out.println ("Java object:" + newer);
}
}
class Order {
private String orderId;
private Date orderDate;
public Order(String orderId, Date orderDate) {
this.orderId = orderId;
this.orderDate = orderDate;
}
// Getter and Setter method omitted
}
class DateSerializer implements JsonSerializer<Date> {
@Override
public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = formatter.format(src);
return new JsonPrimitive(formattedDate);
}
}
Output results:
Json string: {"orderid": "12345", "OrderDate": "2022-03-01 10:30:15"}
Java 对象 : order {orderid = '12345', orderdate = tue mar 01 10:30:15 cst 2022}
In the above examples, a string of converting the `date` object into a specified format is implemented by customizing the` DateSerializer` class.The `DateSerializer` class implements the` jsonserializer` interface, and defines the conversion logic in the `Serialize` method.Then, the custom converter was registered by using the `registerTypeadapter` method, thereby realizing the conversion between the` date` object and the JSON string.
Summarize:
Easygson, as an excellent Java class library, provides powerful and flexible data type conversion and mapping mechanisms.Through the automatic conversion and custom type conversion of basic data types, we can easily transform data between Java objects and JSON, thereby simplifying the development process and improving development efficiency.In actual projects, we can reasonably use EasyGSon's data type conversion and mapping mechanism to improve the readability and maintenance of the code.