티스토리 뷰

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package mltm.com.customize.configure;
 
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
 
import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
 
public class HashMapDeserializer implements JsonDeserializer> {
 
    @Override
    public HashMap deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext ctx) throws JsonParseException {
        JsonObject obj = json.getAsJsonObject();
        Entry entry = obj.entrySet().iterator().next();
        HashMap resultMap = new HashMap();
        resultMap.put(entry.getKey(), ParseObjectFromElement.SINGLETON.apply(entry.getValue()));
        return resultMap;
    }
 
    public enum ParseObjectFromElement implements Function {
        SINGLETON;
        public Object apply(JsonElement input) {
            Object value = null;
            if (input == null || input.isJsonNull()) {
                value = null;
            } else if (input.isJsonPrimitive()) {
                JsonPrimitive primitive = input.getAsJsonPrimitive();
                if (primitive.isNumber()) {
                    value = primitive.getAsInt(); // Number 값은 무조건 integer로 처리
                } else if (primitive.isBoolean()) {
                    value = primitive.getAsBoolean();
                } else {
                    value = primitive.getAsString();
                }
            } else if (input.isJsonArray()) {
                value = Lists.newArrayList(Iterables.transform(input.getAsJsonArray(), this));
            } else if (input.isJsonObject()) {
                value = Maps. newLinkedHashMap(
                        Maps.transformValues(JsonObjectAsMap.INSTANCE.apply(input.getAsJsonObject()), this));
            }
            return value;
        }
    }
 
    public enum JsonObjectAsMap implements Function> {
        INSTANCE;
 
        private final Field members;
 
        JsonObjectAsMap() {
            try {
                members = JsonObject.class.getDeclaredField("members");
                members.setAccessible(true);
            } catch (NoSuchFieldException e) {
                throw new UnsupportedOperationException("cannot access gson internals", e);
            }
        }
 
        @SuppressWarnings("unchecked")
        @Override
        public Map apply(JsonObject in) {
            try {
                return (Map) members.get(in);
            } catch (IllegalArgumentException e) {
                throw new UnsupportedOperationException("cannot access gson internals", e);
            } catch (IllegalAccessException e) {
                throw new UnsupportedOperationException("cannot access gson internals", e);
            }
        }
    }
}

custom Deserializer를 이용하여 gson 객체 생성


1
Gson gson = new GsonBuilder().registerTypeAdapter(HashMap.class, new HashMapDeserializer()).create();

json convert to tree linked map


1
Map<String, Object> retMap = gson.fromJson(jsonData, new TypeToken<HashMap<String, Object>>() {}.getType());
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함