How to write a TypeAdapter and register TypeAdapter and handle Enum types yourself.
This article will explain in detail for you how to write a TypeAdapter and register TypeAdapter and deal with the Enum type. The content of the article is of high quality, so the editor shares it for you to do a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
Enumerated types bring benefits to our program, how to use Gson to achieve interconversion with Json? Please read this article.
How to write a TypeAdapter and register TypeAdapter and handle Enum types yourself.
Entity class:
one
two
three
Public enum PackageState {
PLAY, UPDATE, UPDATING, DOWNLOAD, DOWNLOADING
}
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
fifteen
sixteen
seventeen
eighteen
nineteen
twenty
twenty-one
twenty-two
twenty-three
twenty-four
twenty-five
twenty-six
twenty-seven
twenty-eight
twenty-nine
thirty
thirty-one
thirty-two
thirty-three
thirty-four
thirty-five
Public class PackageItem {
Private String name
Private PackageState state
Private String size
Public String getName () {
Return name
}
Public void setName (String name) {
This.name = name
}
Public PackageState getState () {
Return state
}
Public void setState (PackageState state) {
This.state = state
}
Public String getSize () {
Return size
}
Public void setSize (String size) {
This.size = size
}
@ Override
Public String toString () {
Return "PackageItem [name=" + name + ", size=" + size + ", state="
+ state + "]"
}
}
Write a converter to implement JsonSerializer interface and jsonDeserializer interface:
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
fifteen
sixteen
seventeen
eighteen
nineteen
twenty
twenty-one
twenty-two
twenty-three
twenty-four
twenty-five
twenty-six
twenty-seven
twenty-eight
twenty-nine
thirty
Mport java.lang.reflect.Type
Import com.google.gson.JsonDeserializationContext
Import com.google.gson.JsonDeserializer
Import com.google.gson.JsonElement
Import com.google.gson.JsonParseException
Import com.google.gson.JsonPrimitive
Import com.google.gson.JsonSerializationContext
Import com.google.gson.JsonSerializer
Public class EnumSerializer implements JsonSerializer
JsonDeserializer {
/ / it is called when the object is converted to Json to implement the JsonSerializer interface
@ Override
Public JsonElement serialize (PackageState state, Type arg1
JsonSerializationContext arg2) {
Return new JsonPrimitive (state.ordinal ())
}
/ / json is called when it is converted to an object to implement the JsonDeserializer interface
@ Override
Public PackageState deserialize (JsonElement json, Type typeOfT
JsonDeserializationContext context) throws JsonParseException {
If (json.getAsInt () < PackageState.values () .length)
Return PackageState.values () [json.getAsInt ()]
Return null
}
}
Test class:
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
fifteen
sixteen
seventeen
eighteen
nineteen
twenty
twenty-one
twenty-two
twenty-three
twenty-four
Import com.google.gson.Gson
Import com.google.gson.GsonBuilder
Public class GsonTest6 {
Public static void main (String [] args) {
GsonBuilder gsonBuilder = new GsonBuilder ()
GsonBuilder.registerTypeAdapter (PackageState.class
New EnumSerializer ()
Gson gson = gsonBuilder.create ()
PackageItem item = new PackageItem ()
Item.setName ("item_name")
Item.setSize ("500m")
Item.setState (PackageState.UPDATING); / / this state is an enumerated value
String s = gson.toJson (item)
System.out.println (s)
System.out.println ("-")
PackageItem retItem = gson.fromJson (s, PackageItem.class)
System.out.println (retItem)
}
}
Output result (the corresponding enumeration type of state has been changed to int type in the result):
one
two
three
{"name": "item_name", "state": 2, "size": "500m"}
-
PackageItem [name=item_name, size=500M, state=UPDATING]
Classification: JSP
Tags: Enum, Gson, json, TypeAdapter, instance
About how to write a TypeAdapter and register TypeAdapter and handle Enum types. So much for sharing here. I hope the above content can help you to some extent and learn more knowledge. If you think the article is good, you can share it for more people to see.