In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.