Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix JsonSimple parser
  • Loading branch information
jaeopt committed May 4, 2020
commit b6fc66c370e19e182fc430174fc45a733df08e6e
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import com.optimizely.ab.config.*;
import com.optimizely.ab.config.audience.Audience;
import com.optimizely.ab.config.audience.TypedAudience;
Expand All @@ -29,6 +28,22 @@
* {@link Gson}-based config parser implementation.
*/
final public class GsonConfigParser implements ConfigParser {
private Gson gson;

public GsonConfigParser() {
this(new GsonBuilder()
.registerTypeAdapter(Audience.class, new AudienceGsonDeserializer())
.registerTypeAdapter(TypedAudience.class, new AudienceGsonDeserializer())
.registerTypeAdapter(Experiment.class, new ExperimentGsonDeserializer())
.registerTypeAdapter(FeatureFlag.class, new FeatureFlagGsonDeserializer())
.registerTypeAdapter(Group.class, new GroupGsonDeserializer())
.registerTypeAdapter(DatafileProjectConfig.class, new DatafileGsonDeserializer())
.create());
}

GsonConfigParser(Gson gson) {
this.gson = gson;
}

@Override
public ProjectConfig parseProjectConfig(@Nonnull String json) throws ConfigParseException {
Expand All @@ -38,14 +53,6 @@ public ProjectConfig parseProjectConfig(@Nonnull String json) throws ConfigParse
if (json.length() == 0) {
throw new ConfigParseException("Unable to parse empty json.");
}
Gson gson = new GsonBuilder()
.registerTypeAdapter(Audience.class, new AudienceGsonDeserializer())
.registerTypeAdapter(TypedAudience.class, new AudienceGsonDeserializer())
.registerTypeAdapter(Experiment.class, new ExperimentGsonDeserializer())
.registerTypeAdapter(FeatureFlag.class, new FeatureFlagGsonDeserializer())
.registerTypeAdapter(Group.class, new GroupGsonDeserializer())
.registerTypeAdapter(DatafileProjectConfig.class, new DatafileGsonDeserializer())
.create();

try {
return gson.fromJson(json, DatafileProjectConfig.class);
Expand All @@ -55,12 +62,12 @@ public ProjectConfig parseProjectConfig(@Nonnull String json) throws ConfigParse
}

public String toJson(Object src) {
return new Gson().toJson(src);
return gson.toJson(src);
}

public <T> T fromJson(String json, Class<T> clazz) throws ConfigParseException {
try {
return new Gson().fromJson(json, clazz);
return gson.fromJson(json, clazz);
} catch (Exception e) {
throw new ConfigParseException("Unable to parse JSON string: " + e.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
*/
package com.optimizely.ab.config.parser;

public final class UnsupportedOperationException extends Exception {
public UnsupportedOperationException(String message) {
public final class JsonParseException extends Exception {
public JsonParseException(String message) {
super(message);
}

public UnsupportedOperationException(String message, Throwable cause) {
public JsonParseException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,12 @@
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.json.simple.parser.ContainerFactory;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -381,8 +378,21 @@ public String toJson(Object src) {
@Override
public <T> T fromJson(String json, Class<T> clazz) throws ConfigParseException {
if (Map.class.isAssignableFrom(clazz)) {
org.json.JSONObject obj = new org.json.JSONObject(json);
return (T)JsonHelpers.jsonObjectToMap(obj);
try {
return (T)new JSONParser().parse(json, new ContainerFactory() {
@Override
public Map createObjectContainer() {
return new HashMap();
}

@Override
public List creatArrayContainer() {
return new ArrayList();
}
});
} catch (ParseException e) {
e.printStackTrace();
}
}

// org.json.simple does not support parsing to user objects
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
*/
package com.optimizely.ab.optimizelyjson;

import com.optimizely.ab.Optimizely;
import com.optimizely.ab.config.parser.*;
import com.optimizely.ab.config.parser.UnsupportedOperationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -105,9 +103,9 @@ public Map<String,Object> toMap() {
* @return an instance of clazz type with the parsed data filled in (or null if parse fails)
*/
@Nullable
public <T> T getValue(@Nullable String jsonKey, Class<T> clazz) throws UnsupportedOperationException {
public <T> T getValue(@Nullable String jsonKey, Class<T> clazz) throws JsonParseException {
if (!(parser instanceof GsonConfigParser || parser instanceof JacksonConfigParser)) {
throw new UnsupportedOperationException("A proper JSON parser is not available. Use Gson or Jackson parser for this operation.");
throw new JsonParseException("A proper JSON parser is not available. Use Gson or Jackson parser for this operation.");
}

Map<String,Object> subMap = toMap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
package com.optimizely.ab.optimizelyjson;

import com.optimizely.ab.config.parser.UnsupportedOperationException;
import com.optimizely.ab.config.parser.JsonParseException;
import com.optimizely.ab.optimizelyjson.types.MD1;
import com.optimizely.ab.optimizelyjson.types.MD2;
import com.optimizely.ab.optimizelyjson.types.MD3;
Expand All @@ -27,7 +27,7 @@
public class OptimizelyJSONExtendedTest extends OptimizelyJSONCoreTest {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of Core vs Extended you can use assumeThat to conditionally ignore a parameterized variant.


@Test
public void testGetValueNullKeyPath() throws UnsupportedOperationException {
public void testGetValueNullKeyPath() throws JsonParseException {
OptimizelyJSON oj1 = new OptimizelyJSON(orgJson, getParser());

MD1 md1 = oj1.getValue(null, MD1.class);
Expand All @@ -51,7 +51,7 @@ public void testGetValueNullKeyPath() throws UnsupportedOperationException {
}

@Test
public void testGetValueEmptyKeyPath() throws UnsupportedOperationException {
public void testGetValueEmptyKeyPath() throws JsonParseException {
OptimizelyJSON oj1 = new OptimizelyJSON(orgJson, getParser());

MD1 md1 = oj1.getValue("", MD1.class);
Expand All @@ -64,7 +64,7 @@ public void testGetValueEmptyKeyPath() throws UnsupportedOperationException {
}

@Test
public void testGetValueWithKeyPathToMapWithLevel1() throws UnsupportedOperationException {
public void testGetValueWithKeyPathToMapWithLevel1() throws JsonParseException {
OptimizelyJSON oj1 = new OptimizelyJSON(orgJson, getParser());

MD2 md2 = oj1.getValue("k3", MD2.class);
Expand All @@ -74,7 +74,7 @@ public void testGetValueWithKeyPathToMapWithLevel1() throws UnsupportedOperation
}

@Test
public void testGetValueWithKeyPathToMapWithLevel2() throws UnsupportedOperationException {
public void testGetValueWithKeyPathToMapWithLevel2() throws JsonParseException {
OptimizelyJSON oj1 = new OptimizelyJSON(orgJson, getParser());

MD3 md3 = oj1.getValue("k3.kk2", MD3.class);
Expand All @@ -83,7 +83,7 @@ public void testGetValueWithKeyPathToMapWithLevel2() throws UnsupportedOperation
}

@Test
public void testGetValueWithKeyPathToBoolean() throws UnsupportedOperationException {
public void testGetValueWithKeyPathToBoolean() throws JsonParseException {
OptimizelyJSON oj1 = new OptimizelyJSON(orgJson, getParser());

Boolean value = oj1.getValue("k3.kk2.kkk1", Boolean.class);
Expand All @@ -92,7 +92,7 @@ public void testGetValueWithKeyPathToBoolean() throws UnsupportedOperationExcept
}

@Test
public void testGetValueWithKeyPathToDouble() throws UnsupportedOperationException {
public void testGetValueWithKeyPathToDouble() throws JsonParseException {
OptimizelyJSON oj1 = new OptimizelyJSON(orgJson, getParser());

Double value = oj1.getValue("k3.kk2.kkk2", Double.class);
Expand All @@ -101,7 +101,7 @@ public void testGetValueWithKeyPathToDouble() throws UnsupportedOperationExcepti
}

@Test
public void testGetValueWithKeyPathToString() throws UnsupportedOperationException {
public void testGetValueWithKeyPathToString() throws JsonParseException {
OptimizelyJSON oj1 = new OptimizelyJSON(orgJson, getParser());

String value = oj1.getValue("k3.kk2.kkk3", String.class);
Expand All @@ -110,39 +110,39 @@ public void testGetValueWithKeyPathToString() throws UnsupportedOperationExcepti
}

@Test
public void testGetValueWithInvalidKeyPath() throws UnsupportedOperationException {
public void testGetValueWithInvalidKeyPath() throws JsonParseException {
OptimizelyJSON oj1 = new OptimizelyJSON(orgJson, getParser());

String value = oj1.getValue("k3..kkk3", String.class);
assertNull(value);
}

@Test
public void testGetValueWithInvalidKeyPath2() throws UnsupportedOperationException {
public void testGetValueWithInvalidKeyPath2() throws JsonParseException {
OptimizelyJSON oj1 = new OptimizelyJSON(orgJson, getParser());

String value = oj1.getValue("k1.", String.class);
assertNull(value);
}

@Test
public void testGetValueWithInvalidKeyPath3() throws UnsupportedOperationException {
public void testGetValueWithInvalidKeyPath3() throws JsonParseException {
OptimizelyJSON oj1 = new OptimizelyJSON(orgJson, getParser());

String value = oj1.getValue("x9", String.class);
assertNull(value);
}

@Test
public void testGetValueWithInvalidKeyPath4() throws UnsupportedOperationException {
public void testGetValueWithInvalidKeyPath4() throws JsonParseException {
OptimizelyJSON oj1 = new OptimizelyJSON(orgJson, getParser());

String value = oj1.getValue("k3.x9", String.class);
assertNull(value);
}

@Test
public void testGetValueWithWrongType() throws UnsupportedOperationException {
public void testGetValueWithWrongType() throws JsonParseException {
OptimizelyJSON oj1 = new OptimizelyJSON(orgJson, getParser());

Integer value = oj1.getValue("k3.kk2.kkk3", Integer.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import com.optimizely.ab.config.parser.ConfigParser;
import com.optimizely.ab.config.parser.GsonConfigParser;
import com.optimizely.ab.config.parser.UnsupportedOperationException;
import com.optimizely.ab.config.parser.JsonParseException;
import com.optimizely.ab.optimizelyjson.types.MDN1;
import com.optimizely.ab.optimizelyjson.types.NotMatchingType;
import org.junit.Test;
Expand All @@ -38,7 +38,7 @@ protected ConfigParser getParser() {
// Tests for GSON only
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess these classes are ok, but per the comments above, they should not be extending from OptimizelyJSONExtendedTest.


@Test
public void testGetValueWithNotMatchingType() throws UnsupportedOperationException {
public void testGetValueWithNotMatchingType() throws JsonParseException {
OptimizelyJSON oj1 = new OptimizelyJSON(orgJson, getParser());

NotMatchingType md = oj1.getValue(null, NotMatchingType.class);
Expand All @@ -48,7 +48,7 @@ public void testGetValueWithNotMatchingType() throws UnsupportedOperationExcepti
// Tests for integer/double processing

@Test
public void testIntegerProcessing() throws UnsupportedOperationException {
public void testIntegerProcessing() throws JsonParseException {

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

Expand All @@ -68,7 +68,7 @@ public void testIntegerProcessing() throws UnsupportedOperationException {
}

@Test
public void testIntegerProcessing2() throws UnsupportedOperationException {
public void testIntegerProcessing2() throws JsonParseException {

// GSON parser toString() keeps ".0" in double

Expand All @@ -88,7 +88,7 @@ public void testIntegerProcessing2() throws UnsupportedOperationException {
}

@Test
public void testIntegerProcessing3() throws UnsupportedOperationException {
public void testIntegerProcessing3() throws JsonParseException {
String json = "{\"k1\":1,\"k2\":2.5,\"k3\":{\"kk1\":3,\"kk2\":4.0}}";

OptimizelyJSON oj1 = new OptimizelyJSON(json, getParser());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import com.optimizely.ab.config.parser.ConfigParser;
import com.optimizely.ab.config.parser.JacksonConfigParser;
import com.optimizely.ab.config.parser.UnsupportedOperationException;
import com.optimizely.ab.config.parser.JsonParseException;
import com.optimizely.ab.optimizelyjson.types.MDN1;
import com.optimizely.ab.optimizelyjson.types.NotMatchingType;
import org.junit.Test;
Expand All @@ -38,7 +38,7 @@ protected ConfigParser getParser() {
// Tests for Jackson only

@Test
public void testGetValueWithNotMatchingType() throws UnsupportedOperationException {
public void testGetValueWithNotMatchingType() throws JsonParseException {
OptimizelyJSON oj1 = new OptimizelyJSON(orgJson, getParser());

// Jackson returns null object when variables not matching (while GSON returns an object with null variables
Expand All @@ -49,7 +49,7 @@ public void testGetValueWithNotMatchingType() throws UnsupportedOperationExcepti
// Tests for integer/double processing

@Test
public void testIntegerProcessing() throws UnsupportedOperationException {
public void testIntegerProcessing() throws JsonParseException {

// Jackson parser toMap() keeps ".0" in double

Expand All @@ -69,7 +69,7 @@ public void testIntegerProcessing() throws UnsupportedOperationException {
}

@Test
public void testIntegerProcessing2() throws UnsupportedOperationException {
public void testIntegerProcessing2() throws JsonParseException {

// Jackson parser toString() keeps ".0" in double

Expand All @@ -89,7 +89,7 @@ public void testIntegerProcessing2() throws UnsupportedOperationException {
}

@Test
public void testIntegerProcessing3() throws UnsupportedOperationException {
public void testIntegerProcessing3() throws JsonParseException {
String json = "{\"k1\":1,\"k2\":2.5,\"k3\":{\"kk1\":3,\"kk2\":4.0}}";

OptimizelyJSON oj1 = new OptimizelyJSON(json, getParser());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import com.optimizely.ab.config.parser.ConfigParser;
import com.optimizely.ab.config.parser.JsonConfigParser;
import com.optimizely.ab.config.parser.UnsupportedOperationException;
import com.optimizely.ab.config.parser.JsonParseException;
import org.junit.Test;

import java.util.HashMap;
Expand All @@ -41,15 +41,15 @@ public void testGetValueThrowsException() {
try {
String str = oj1.getValue(null, String.class);
fail("GetValue is not supported for or.json paraser: " + str);
} catch (UnsupportedOperationException e) {
} catch (JsonParseException e) {
assertEquals(e.getMessage(), "A proper JSON parser is not available. Use Gson or Jackson parser for this operation.");
}
}

// Tests for integer/double processing

@Test
public void testIntegerProcessing() throws UnsupportedOperationException {
public void testIntegerProcessing() throws JsonParseException {

// org.json parser toMap() keeps ".0" in double

Expand All @@ -69,7 +69,7 @@ public void testIntegerProcessing() throws UnsupportedOperationException {
}

@Test
public void testIntegerProcessing2() throws UnsupportedOperationException {
public void testIntegerProcessing2() throws JsonParseException {

// org.json parser toString() drops ".0" from double

Expand Down
Loading