Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Custom enumeration gson conversion

Shulou Source: shulou.com Published: 2022-06-02 04:04:54 09月23日 Update

This article introduces the relevant knowledge of "the conversion of custom enumeration gson". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

CodedTypeTypeAdapterimport com.google.gson.*;import com.utils.mybatis.CodedEnum;import java.lang.reflect.Type / * * CodedEnum conversion rules in GSON, using code instead of the characters * * @ param * @ author tenmao * / public class CodedTypeTypeAdapter implements JsonSerializer, JsonDeserializer {@ Override public E deserialize (JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {if (type instanceof Class) {@ SuppressWarnings ("unchecked") Class klass = (Class) type; return CodedEnum.codeOf (klass, jsonElement.getAsInt ()) .orElse (null) } else {throw new RuntimeException (String.format ("json% s cannot convert to type s", jsonElement, type));} @ Override public JsonElement serialize (E, Type type, JsonSerializationContext jsonSerializationContext) {return new JsonPrimitive (e.getCode ());}} HttpMessageConverterimport com.google.gson.GsonBuilder;import com.tenmao.utils.mybatis.CodedEnum;import com.tenmao.utils.mybatis.converter.CodedTypeTypeAdapter;import lombok.extern.slf4j.Slf4j Import org.reflections.Reflections;import org.reflections.scanners.SubTypesScanner;import org.springframework.http.converter.json.GsonHttpMessageConverter;import java.util.Set;/** * @ author tenmao * @ since 2017-12-1 * / @ Slf4jpublic class HttpMessageConverter extends GsonHttpMessageConverter {public HttpMessageConverter () {final GsonBuilder builder = new GsonBuilder (); final Reflections reflections = new Reflections ("com.tenmao", new SubTypesScanner (true)) Final Set allClasses = reflections.getStore (). GetSubTypesOf (CodedEnum.class.getName ()); for (String klass: allClasses) {try {final Class aClass = Class.forName (klass); builder.registerTypeAdapter (aClass, new CodedTypeTypeAdapter ());} catch (ClassNotFoundException e) {log.error ("fail to register for gson", e) }} setGson (builder.create ());}} spring-mvc.xml complete

After implementing the above steps, as long as you implement the custom enumeration of interface CodedEnum, you can automatically convert to its code value.

This is the end of the conversion of Custom enumeration gson. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Content more knowledge practicality achievement next dilemma character reality situation interface article case step ribbon website industry rule process high quality Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Apple Huawei Docker OPPO Reno