|
| 1 | +package cn.leancloud.gson; |
| 2 | + |
| 3 | +import cn.leancloud.annotation.JsonField; |
| 4 | +import com.google.gson.TypeAdapter; |
| 5 | +import com.google.gson.stream.JsonReader; |
| 6 | +import com.google.gson.stream.JsonToken; |
| 7 | +import com.google.gson.stream.JsonWriter; |
| 8 | + |
| 9 | +import java.io.IOException; |
| 10 | +import java.lang.reflect.Field; |
| 11 | +import java.lang.reflect.Type; |
| 12 | +import java.util.HashMap; |
| 13 | +import java.util.Map; |
| 14 | + |
| 15 | +public class GeneralSimpleObjectAdapter<T> extends TypeAdapter<T> { |
| 16 | + private Class targetClazz; |
| 17 | + private Map<String, Field> displayFields = new HashMap<>(); |
| 18 | + |
| 19 | + public GeneralSimpleObjectAdapter(Class clazz) { |
| 20 | + this.targetClazz = clazz; |
| 21 | + Field[] fields = clazz.getDeclaredFields(); |
| 22 | + for (Field field: fields) { |
| 23 | + String fieldName = field.getName(); |
| 24 | + JsonField annotation = field.getAnnotation(JsonField.class); |
| 25 | + if (null != annotation) { |
| 26 | + fieldName = annotation.value(); |
| 27 | + } |
| 28 | + displayFields.put(fieldName, field); |
| 29 | + } |
| 30 | + } |
| 31 | + @Override |
| 32 | + public void write(JsonWriter jsonWriter, T t) throws IOException { |
| 33 | + jsonWriter.beginObject(); |
| 34 | + Field[] fields = t.getClass().getDeclaredFields(); |
| 35 | + for(Field field : fields) { |
| 36 | + field.setAccessible(true); |
| 37 | + String canonicalName = field.getName(); |
| 38 | + String outputName = canonicalName; |
| 39 | + JsonField jsonField = field.getAnnotation(JsonField.class); |
| 40 | + if (null != jsonField) { |
| 41 | + outputName = jsonField.value(); |
| 42 | + } |
| 43 | + Type valueType = field.getType(); |
| 44 | + try { |
| 45 | + if (valueType.equals(Character.class) || valueType.equals(char.class)) { |
| 46 | + char value = field.getChar(t); |
| 47 | + jsonWriter.name(outputName).value(value); |
| 48 | + } else if (valueType.equals(Boolean.class) || valueType.equals(boolean.class)) { |
| 49 | + boolean value = field.getBoolean(t); |
| 50 | + jsonWriter.name(outputName).value(value); |
| 51 | + } else if (valueType.equals(String.class)) { |
| 52 | + String value = (String)field.get(t); |
| 53 | + jsonWriter.name(outputName).value(value); |
| 54 | + } else if (valueType.equals(Integer.class) || valueType.equals(int.class)) { |
| 55 | + Integer value = (Integer) field.get(t); |
| 56 | + jsonWriter.name(outputName).value(value); |
| 57 | + } else if (valueType.equals(Long.class) || valueType.equals(long.class)) { |
| 58 | + Long value = (Long) field.get(t); |
| 59 | + jsonWriter.name(outputName).value(value); |
| 60 | + } else if (valueType.equals(Float.class) || valueType.equals(float.class)) { |
| 61 | + Float value = (Float) field.get(t); |
| 62 | + jsonWriter.name(outputName).value(value); |
| 63 | + } else if (valueType.equals(Double.class) || valueType.equals(double.class)) { |
| 64 | + Double value = (Double) field.get(t); |
| 65 | + jsonWriter.name(outputName).value(value); |
| 66 | + } |
| 67 | + } catch (Exception ex) { |
| 68 | + ex.printStackTrace(); |
| 69 | + } |
| 70 | + } |
| 71 | + jsonWriter.endObject(); |
| 72 | + jsonWriter.flush(); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public T read(JsonReader jsonReader) throws IOException { |
| 77 | + try { |
| 78 | + T result = (T)this.targetClazz.newInstance(); |
| 79 | + jsonReader.beginObject(); |
| 80 | + String jsonFieldName = null; |
| 81 | + while(jsonReader.hasNext()) { |
| 82 | + JsonToken token = jsonReader.peek(); |
| 83 | + if (token.equals(JsonToken.NAME)) { |
| 84 | + // get current token. |
| 85 | + jsonFieldName = jsonReader.nextName(); |
| 86 | + } |
| 87 | + Field targetField = this.displayFields.get(jsonFieldName); |
| 88 | + // move to next token |
| 89 | + jsonReader.peek(); |
| 90 | + try { |
| 91 | + if (null == targetField) { |
| 92 | + jsonReader.skipValue(); |
| 93 | + continue; |
| 94 | + } |
| 95 | + targetField.setAccessible(true); |
| 96 | + Object value = null; |
| 97 | + Type valueType = targetField.getType(); |
| 98 | + if (valueType.equals(String.class)) { |
| 99 | + value = jsonReader.nextString(); |
| 100 | + } else if (valueType.equals(Integer.class) || valueType.equals(int.class)) { |
| 101 | + value = jsonReader.nextInt(); |
| 102 | + } else if (valueType.equals(Boolean.class) || valueType.equals(boolean.class)) { |
| 103 | + value = jsonReader.nextBoolean(); |
| 104 | + } else if (valueType.equals(Character.class) || valueType.equals(char.class)) { |
| 105 | + value = jsonReader.nextString(); |
| 106 | + } else if (valueType.equals(Long.class) || valueType.equals(long.class)) { |
| 107 | + value = jsonReader.nextLong(); |
| 108 | + } else if (valueType.equals(Float.class) || valueType.equals(float.class)) { |
| 109 | + value = jsonReader.nextDouble(); |
| 110 | + } else if (valueType.equals(Double.class) || valueType.equals(double.class)) { |
| 111 | + value = jsonReader.nextDouble(); |
| 112 | + } else { |
| 113 | + jsonReader.skipValue(); |
| 114 | + } |
| 115 | + if (null != value) { |
| 116 | + targetField.set(result, value); |
| 117 | + } |
| 118 | + } catch (Exception ex) { |
| 119 | + ex.printStackTrace(); |
| 120 | + } |
| 121 | + } |
| 122 | + jsonReader.endObject(); |
| 123 | + return result; |
| 124 | + } catch (InstantiationException e) { |
| 125 | + throw new RuntimeException(e); |
| 126 | + } catch (IllegalAccessException e) { |
| 127 | + throw new RuntimeException(e); |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments