Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.ToNumberPolicy;
import com.optimizely.ab.config.DatafileProjectConfig;
import com.optimizely.ab.config.Experiment;
import com.optimizely.ab.config.FeatureFlag;
Expand All @@ -37,6 +38,7 @@ final public class GsonConfigParser implements ConfigParser {

public GsonConfigParser() {
this(new GsonBuilder()
.setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE)
.registerTypeAdapter(Audience.class, new AudienceGsonDeserializer())
.registerTypeAdapter(TypedAudience.class, new AudienceGsonDeserializer())
.registerTypeAdapter(Experiment.class, new ExperimentGsonDeserializer())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,19 +404,20 @@ public void testToJson() {

@Test
public void testFromJson() {
String json = "{\"k1\":\"v1\",\"k2\":3.5,\"k3\":true}";
String json = "{\"k1\":\"v1\",\"k2\":3.5,\"k3\":true,\"k4\":12345}";

Map<String, Object> expectedMap = new HashMap<>();
expectedMap.put("k1", "v1");
expectedMap.put("k2", 3.5);
expectedMap.put("k3", true);
expectedMap.put("k4", 12345L);

GsonConfigParser parser = new GsonConfigParser();

Map map = null;
Map<String, Object> map = null;
try {
map = parser.fromJson(json, Map.class);
assertEquals(map, expectedMap);
assertEquals(expectedMap, map);
} catch (JsonParseException e) {
fail("Parse to map failed: " + e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ public void testGetValueWithNotMatchingType() throws JsonParseException {
@Test
public void testIntegerProcessing() throws JsonParseException {

// GSON parser toMap() adds ".0" to all integers
// GSON parser toMap() converts integers to longs

String json = "{\"k1\":1,\"k2\":2.5,\"k3\":{\"kk1\":3,\"kk2\":4.0}}";

Map<String,Object> m2 = new HashMap<String,Object>();
m2.put("kk1", 3.0);
m2.put("kk1", 3L);
m2.put("kk2", 4.0);

Map<String,Object> m1 = new HashMap<String,Object>();
m1.put("k1", 1.0);
m1.put("k1", 1L);
m1.put("k2", 2.5);
m1.put("k3", m2);

Expand Down