Skip to content

Commit fcc5ac4

Browse files
mrodbratschiBeta Bot
authored andcommitted
Cherry pick branch 'genexuslabs:fix/duplicated-fields-log' into beta
1 parent 918fe8d commit fcc5ac4

File tree

1 file changed

+40
-7
lines changed

1 file changed

+40
-7
lines changed

gamutils/src/main/java/com/genexus/gam/utils/Dictionary.java

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package com.genexus.gam.utils;
22

33
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;
4+
import com.google.gson.*;
105
import com.google.gson.reflect.TypeToken;
6+
import com.google.gson.stream.JsonReader;
7+
import com.google.gson.stream.JsonWriter;
118

9+
import java.io.IOException;
1210
import java.lang.reflect.Type;
1311
import java.util.LinkedHashMap;
1412
import java.util.List;
@@ -23,7 +21,42 @@ public class Dictionary {
2321

2422
public Dictionary() {
2523
this.userMap = new LinkedHashMap<>();
26-
this.gson = new GsonBuilder().serializeNulls().create();
24+
this.gson = new GsonBuilder().serializeNulls()
25+
.addSerializationExclusionStrategy(new ExclusionStrategy() {
26+
@Override
27+
public boolean shouldSkipField(FieldAttributes f) {
28+
// exclude only the field 'map' inherited from org.json.JSONObject
29+
return f.getName().equals("map") &&
30+
f.getDeclaringClass() == org.json.JSONObject.class;
31+
}
32+
33+
@Override
34+
public boolean shouldSkipClass(Class<?> clazz) {
35+
return false;
36+
}
37+
})
38+
.registerTypeHierarchyAdapter(Number.class, new TypeAdapter<Number>() {
39+
@Override
40+
public void write(JsonWriter out, Number value) throws IOException {
41+
if (value == null) {
42+
out.nullValue();
43+
return;
44+
}
45+
46+
double d = value.doubleValue();
47+
if (d == Math.rint(d)) {
48+
out.value(value.longValue()); // 1.0 → 1
49+
} else {
50+
out.value(d); // 1.5 → 1.5
51+
}
52+
}
53+
54+
@Override
55+
public Number read(JsonReader in) throws IOException {
56+
return in.nextDouble();
57+
}
58+
})
59+
.create();
2760
}
2861

2962
public Object get(String key) {

0 commit comments

Comments
 (0)