preface

A gson to JSON type conversion exception encountered while writing to the data cache in a project is documented here

Get into the business

We request data from the background or network in this form. List

Take the background data of our company as an example

Data is an object:

{"code":"0","message":"success","data":{goodsName=' 1 ', goodsOldPrice=198.0, goodsRealPrice=168.0, GoodsPostage = 0.0,... }}Copy the code

Data is an array:

{"code":"0","message":"success","data":[sortNo=1, isDiplay='0', catalogDesc=' customize each conference room for customer ', isSubcribe='0', = 'null' isCollect minConsume = 2000.0]}Copy the code

This is the requested data corresponding to a homeModel, so if you have a lot of data types you have a lot of data models, and it’s really not a good idea to write duplicate code for each one. So we can consider using generics, and Gson provides TypeToken support for generics.

The simplest way to do this is to provide two methods that correspond to data as Array and Object and receive a parameter that indicates the type of XXX. New TypeToken(){} and new TypeToken<Result<List>>(){} are automatically completed.

The TypeToken function is used to obtain a generic class. The Type returned is Type. The real generic information is stored in this Type.

Type is the parent interface for all types in Java. Before 1.8, it was an empty interface. Since 1.8, getTypeName() has been added. ParameterizedType, GenericArrayType, WildcardType, TypeVariable, Class. Only ParameterizedType will be used for these interfaces during this encapsulation, so let’s be brief:

ParameterizedType is simply a type <>, such as List

.
,model>

public class ListOfSomething<X> implements ParameterizedType { private Class<? > wrapped; public ListOfSomething(Class<X> wrapped) { this.wrapped = wrapped; } // Return Key and Model from List<Key,Model> Class, model.clas] public Type[] getActualTypeArguments() {return new Type[] {wrapped}; List.class public Type getRawType() {return list.class; Public Type getOwnerType() {return null; getOwnerType() {return null; }}Copy the code

Encapsulation method

Public static <T>void saveList(String key, String key, String key, String key); List<T> list) { Gson gson = new Gson(); String saveListStr = gson.toJson(list, new TypeToken<List<T>>(){}.getType()); Timber.d("saved list:%s", saveListStr); putString(key, saveListStr); } public static <T>List<T> getList(String key, String key, String key, String key) Class<T> tClass) { Gson gson = new Gson(); String json = getString(key, "[]"); return gson.fromJson(json, new ListOfSomething<>(tClass)); }Copy the code

This solves the type conversion exception encountered in gson to JSON

Conclusion:

ParameterizedType is an excellent solution to ParameterizedType. It took a long time to solve this problem.

Share with you

I want to work my way up

Fly forward on the blades at the highest point

Let the wind blow dry tears and sweat

I want to work my way up

Waiting for the sun to watch its face

Little days have big dreams

I have my day

Let the wind blow dry tears and sweat

One day I will have my day