关于源码,已经迁移到GitHub上,地址https://github.com/google/gson
也可以直接在这个地址下载http://download.csdn.net/detail/atm008/9556429,已集成下面文章中的方法
-------------------------------------------------------------------------------------以下正文
看文章前请先下载最新Gson代码,地址: https://code.google.com/p/google-gson/downloads/list
这里面包含了jar包和源码,因为这里需要改变一点点源码,所以想要实验的同学还是把源码直接导入工程方便看效果!public interface IEnum {
int getValue();
void setValue(int value);
}
public enum Sex implements IEnum{
Man(1),
Women(2);
private int value;
private Sex(int value){
this.value = value;
}
public int getValue(){
return value;
}
@Override
public void setValue(int value) {
this.value = value;
}
}
public enum Degree implements IEnum{
High(1),
Middle(2),
Low(3);
private int value;
private Degree(int value){
this.value = value;
}
public int getValue(){
return value;
}
@Override
public void setValue(int value) {
this.value = value;
}
}
public class MyEnumAdapterFactory implements TypeAdapterFactory {
@Override
public TypeAdapter create(Gson gson, TypeToken type) {
Class super T> rawType = type.getRawType();
if(rawType.isEnum()){
Type[] types = rawType.getGenericInterfaces();
for(Type item:types){
if(item == IEnum.class){
return new EnumTypeAdapter();
}
}
}
return null;
}
}
public class EnumTypeAdapter extends TypeAdapter{
@Override
public T read(JsonReader in) throws IOException {
if(in.peek() == JsonToken.NULL){
in.nextNull();
return null;
}
return null;
}
public Object read(JsonReader in,TypeToken type) throws IOException{
boolean isEnum = type.getRawType().isEnum();
if(isEnum){
int value = in.nextInt();
try {
Method valuesMethod = type.getRawType().getMethod("values", null);
IEnum[] enumArr = (IEnum[])valuesMethod.invoke(type.getClass(), null);
for (IEnum iEnum : enumArr) {
// System.out.println( iEnum );
if(iEnum.getValue() == value){
Log.d("This is a enum ", "value is=====>"+value);
return iEnum;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
@Override
public void write(JsonWriter out, T value) throws IOException {
if(value == null){
out.nullValue();
return;
}
if(value instanceof IEnum){
out.value(((IEnum)value).getValue());
}
}
}
Object fieldValue = null;
if (typeAdapter instanceof EnumTypeAdapter) {
fieldValue = ((EnumTypeAdapter) typeAdapter)
.read(reader, fieldType);
} else {
fieldValue = typeAdapter.read(reader);
}
if (fieldValue != null || !isPrimitive) {
field.set(value, fieldValue);
}
boolean isEnum = type.getRawType().isEnum();
if(isEnum){
int value = in.nextInt();
try {
Method valuesMethod = type.getRawType().getMethod("values", null);
IEnum[] enumArr = (IEnum[])valuesMethod.invoke(type.getClass(), null);
for (IEnum iEnum : enumArr) {
// System.out.println( iEnum );
if(iEnum.getValue() == value){
Log.d("This is a enum ", "value is=====>"+value);
return iEnum;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
public class Student {
private String stuName;
private int stuId;
private Sex mSex;
private Degree mDegree;
//...set 和 get方法.....
}
public class Teacher {
private List students;
private int studentCount;
//...set和get方法
}
Teacher mTeacher = new Teacher();
for (int i = 0; i < 5; i++) {
Student stu = new Student();
stu.setStuId(i);
stu.setStuName("stu" + i);
stu.setmDegree(Degree.Low);
stu.setmSex(Sex.Women);
mTeacher.getStudents().add(stu);
}
mTeacher.setStudentCount(5);
GsonBuilder gb = new GsonBuilder();
gb.registerTypeAdapterFactory(new MyEnumAdapterFactory());
Gson gson = gb.create();
String mJsonStr = gson.toJson(mTeacher);
Log.d("json is====>", mJsonStr); //打印出转换后的Json值
Teacher teacher = gson.fromJson(mJsonStr, Teacher.class)
Log.d("DeSerilize ok===>", "Sex is:"+ teacher.getStudents().get(0).getmSex()+ " Degree is:"+ teacher.getStudents().get(0).getmDegree());