|
| 1 | +package com.genexus.gam.utils; |
| 2 | + |
| 3 | +import com.genexus.GxUserType; |
| 4 | +import com.google.gson.Gson; |
| 5 | +import com.google.gson.GsonBuilder; |
| 6 | +import com.google.gson.JsonParser; |
| 7 | +import com.google.gson.JsonElement; |
| 8 | +import com.google.gson.JsonPrimitive; |
| 9 | +import com.google.gson.JsonSyntaxException; |
| 10 | +import com.google.gson.reflect.TypeToken; |
| 11 | + |
| 12 | +import java.lang.reflect.Type; |
| 13 | +import java.util.LinkedHashMap; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Map; |
| 16 | + |
| 17 | +public class Dictionary { |
| 18 | + |
| 19 | + private final Map<String, Object> userMap; |
| 20 | + private final Gson gson; |
| 21 | + |
| 22 | + /********EXTERNAL OBJECT PUBLIC METHODS - BEGIN ********/ |
| 23 | + |
| 24 | + public Dictionary() { |
| 25 | + this.userMap = new LinkedHashMap<>(); |
| 26 | + this.gson = new GsonBuilder().serializeNulls().create(); |
| 27 | + } |
| 28 | + |
| 29 | + public Object get(String key) { |
| 30 | + return this.userMap.get(key); |
| 31 | + } |
| 32 | + |
| 33 | + public void set(String key, Object value) { |
| 34 | + objectToMap(key, value); |
| 35 | + } |
| 36 | + |
| 37 | + public void remove(String key) { |
| 38 | + this.userMap.remove(key); |
| 39 | + } |
| 40 | + |
| 41 | + public void clear() { |
| 42 | + this.userMap.clear(); |
| 43 | + } |
| 44 | + |
| 45 | + public String toJsonString() { |
| 46 | + return this.gson.toJson(this.userMap); |
| 47 | + } |
| 48 | + |
| 49 | + /********EXTERNAL OBJECT PUBLIC METHODS - END ********/ |
| 50 | + |
| 51 | + // Convert a JSON String to Map<String, Object> |
| 52 | + private Map<String, Object> jsonStringToMap(String jsonString) { |
| 53 | + Type type = new TypeToken<Map<String, Object>>() {}.getType(); |
| 54 | + return this.gson.fromJson(jsonString, type); |
| 55 | + } |
| 56 | + |
| 57 | + private void objectToMap(String key, Object value) { |
| 58 | + if (value == null) { |
| 59 | + this.userMap.put(key, null); |
| 60 | + } else if (value instanceof Number || value instanceof Boolean || value instanceof Map || value instanceof List) { |
| 61 | + this.userMap.put(key, value); |
| 62 | + } else if (value instanceof GxUserType) { |
| 63 | + this.userMap.put(key, jsonStringToMap(((GxUserType) value).toJSonString())); |
| 64 | + } else if (value instanceof String) { |
| 65 | + String str = (String) value; |
| 66 | + |
| 67 | + // Try to parse as JSON |
| 68 | + try { |
| 69 | + JsonElement parsed = JsonParser.parseString(str); |
| 70 | + if (parsed.isJsonObject()) { |
| 71 | + this.userMap.put(key, this.gson.fromJson(parsed, Map.class)); |
| 72 | + } else if (parsed.isJsonArray()) { |
| 73 | + this.userMap.put(key, this.gson.fromJson(parsed, List.class)); |
| 74 | + } else if (parsed.isJsonPrimitive()) { |
| 75 | + JsonPrimitive primitive = parsed.getAsJsonPrimitive(); |
| 76 | + if (primitive.isBoolean()) { |
| 77 | + this.userMap.put(key, primitive.getAsBoolean()); |
| 78 | + } else if (primitive.isNumber()) { |
| 79 | + this.userMap.put(key, primitive.getAsNumber()); |
| 80 | + } else if (primitive.isString()) { |
| 81 | + this.userMap.put(key, primitive.getAsString()); |
| 82 | + } |
| 83 | + } |
| 84 | + } catch (JsonSyntaxException e) { |
| 85 | + // Invalid JSON: it is left as string |
| 86 | + this.userMap.put(key, str); |
| 87 | + } |
| 88 | + } else { |
| 89 | + // Any other object: it is converted to string |
| 90 | + this.userMap.put(key, value.toString()); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments