Using AJAX technology in Java inevitably encounters the problem of serializing Java objects, and JSON is widely used as a lightweight and easy to understand data format. So, here's a way to serialize Java objects into JSON data, which is mainly about Google's Open-source tool Gson.
Gson inherited Google's fine tradition of simplicity and ease of use. Gson This Java class library converts Java objects to JSON or converts JSON strings into an equal Java object. Gson supports arbitrary complex Java objects including objects that have no source code.
Here we mainly talk about Gson in the serialization of some of the operations to be noted, the general application of the Internet has a lot of information, the best way to view the API
Let's write a generic tool class that converts to JSON data Converttojson.java
Package meetyou.util;
Import Java.lang.reflect.Type;
Import java.util.List;
Import Com.google.gson.Gson;
Import Com.google.gson.GsonBuilder;
public class Converttojson {public static String Tojsonwithgson (Object obj) {gson gson=new Gson ();
return Gson.tojson (obj);
public static String Tojsonwithgson (Object obj,type Type) {Gson gson=new Gson ();
Return Gson.tojson (Obj,type);
@SuppressWarnings ("unchecked") public static String Tojsonwithgson (List list) {Gson gson=new Gson ();
return Gson.tojson (list);
@SuppressWarnings ("unchecked") public static String Tojsonwithgson (List list,type Type) {Gson gson=new Gson ();
Return Gson.tojson (List,type); public static String Tojsonwithgsonbuilder (Object obj) {gson gson=new gsonbuilder (). setexclusionstrategies (New My
Exclusionstrategy ()). Serializenulls (). Create ();
return Gson.tojson (obj); public static String Tojsonwithgsonbuilder (Object obj,type Type) {Gson gson=new gsonbuilder (). setexclusionStrategies (New Myexclusionstrategy ()). Serializenulls (). Create ();
Return Gson.tojson (Obj,type); @SuppressWarnings ("unchecked") public static String Tojsonwithgsonbuilder (List list) {Gson gson=new gsonbuilder ().
Setexclusionstrategies (New Myexclusionstrategy ()). Serializenulls (). Create ();
return Gson.tojson (list); @SuppressWarnings ("unchecked") public static String Tojsonwithgsonbuilder (List list,type Type) {Gson gson=new Gson
Builder (). Setexclusionstrategies (New Myexclusionstrategy ()). Serializenulls (). Create ();
Return Gson.tojson (List,type);
}
}
From the code we start talking about
1, how to use Gson.
The Gson object consists of two methods Tojson () serialization, Fromjson () deserialization
2. How to Get Gson object
Two ways, 1, new Gson (). 2, New Gsonbuilder (). Create ();
3, two ways to get the difference between Gson objects
When you create a Gson object with Gsonbuilder, you can add some special handling, which should be more powerful than the first way.
4. How to create a type in a parameter
(1), ordinary object, such as user User=new User (), Get type method, User.class
(2), complex point object, such as List<user> users=new arraylist<user> (), Get type way,new Com.google.gson.reflect.TypeToken <List<User>> () {}.gettype ()
5. What are the advantages of Gsonbuilder
(1), you can use the annotation way, personalized customization needs serialization and square serialization of fields, such as in the field before adding @expose annotation, when using the Gsonbuilder.excludefieldswithoutexposeannotation () method, Fields that do not have the annotation are excluded. Other annotations include @serializename, @Since, and @until, specifically, see Gson API documentation
(2), customization of their own annotations and exclusion strategy. As the class myexclusionstrategy in the preceding code, the code is as follows
Package meetyou.util;
Import Com.google.gson.ExclusionStrategy;
Import com.google.gson.FieldAttributes;
public class Myexclusionstrategy implements Exclusionstrategy {
private final class<?> Typetoskip;
Public Myexclusionstrategy () {
this.typetoskip=null;
}
Public Myexclusionstrategy (class<?> typetoskip) {
this.typetoskip = Typetoskip;
}
public boolean shouldskipclass (class<?> clazz) {return
(clazz = = Typetoskip);
}
public boolean Shouldskipfield (FieldAttributes f) {return
f.getannotation (notserialize.class)!= null;
}
Implement Exclusionstrategy interface
Its own annotation interface notserialize, the code is as follows
Package meetyou.util;
Import Java.lang.annotation.ElementType;
Import java.lang.annotation.Retention;
Import Java.lang.annotation.RetentionPolicy;
Import Java.lang.annotation.Target;
@Retention (retentionpolicy.runtime)
@Target ({Elementtype.field}) public
@interface notserialize {
}
At this point, we use the time, as long as the @notserialize in front of the field with a note (such as password field, to ensure that its security is not necessary to serialize out), using the new Gsonbuilder (). When the Setexclusionstrategies () method sets the Gson object created by the exclusion policy, the field is no longer serialized
As for the deserialization of Gson, this is not to be said. Refer to the Gson API documentation.