-
Notifications
You must be signed in to change notification settings - Fork 32
feature: add OptimizelyJSON #371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
032c270
add OptimizelyJSON
jaeopt 99014d2
clean up
jaeopt 9de2535
add doc comments
jaeopt 641d215
Merge branch 'master' into jae/optimizely-json
jaeopt 69144a0
add more tests
jaeopt f319fdc
fix test
jaeopt 096bcb9
(wip) add multiple parser support to OptimizelyJSON
jaeopt 5be5f3f
split getvalue tests
jaeopt 01970f5
disable getValue for legacy parsers
jaeopt 2401784
add legacy parser tests
jaeopt afdb8be
fix tests for all parsers
jaeopt 2952834
Merge branch 'master' into jae/optimizely-json
jaeopt 24ed0d6
cleanup
jaeopt 5d45085
fix findbugs issues
jaeopt 2af6f14
cleanup
jaeopt b5db429
fix test per review
jaeopt b6fc66c
fix JsonSimple parser
jaeopt ef7963b
add unit tests for all parsers
jaeopt 880111b
add unit tests for JsonHelpers
jaeopt 14ace05
fix test types
jaeopt f0df4e2
refact json parser tests
jaeopt ceddf49
cleanup parser tests
jaeopt 37efc58
cleanup
jaeopt 8cd3b55
change to parameterized tests
jaeopt c92459f
fix merge conflict
jaeopt e5d792a
fix getValue destroy test
jaeopt 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
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
core-api/src/main/java/com/optimizely/ab/config/parser/JsonHelpers.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** | ||
* | ||
* Copyright 2020, Optimizely and contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.optimizely.ab.config.parser; | ||
|
||
import org.json.JSONArray; | ||
import org.json.JSONObject; | ||
|
||
import java.util.*; | ||
|
||
final class JsonHelpers { | ||
|
||
static Object convertToJsonObject(Object obj) { | ||
if (obj instanceof Map) { | ||
Map<Object, Object> map = (Map)obj; | ||
JSONObject jObj = new JSONObject(); | ||
for (Map.Entry entry : map.entrySet()) { | ||
jObj.put(entry.getKey().toString(), convertToJsonObject(entry.getValue())); | ||
} | ||
return jObj; | ||
} else if (obj instanceof List) { | ||
List list = (List)obj; | ||
JSONArray jArray = new JSONArray(); | ||
for (Object value : list) { | ||
jArray.put(convertToJsonObject(value)); | ||
} | ||
return jArray; | ||
} else { | ||
return obj; | ||
} | ||
} | ||
|
||
static Map<String, Object> jsonObjectToMap(JSONObject jObj) { | ||
Map<String, Object> map = new HashMap<>(); | ||
|
||
Iterator<String> keys = jObj.keys(); | ||
while(keys.hasNext()) { | ||
String key = keys.next(); | ||
Object value = jObj.get(key); | ||
|
||
if (value instanceof JSONArray) { | ||
value = jsonArrayToList((JSONArray)value); | ||
} else if (value instanceof JSONObject) { | ||
value = jsonObjectToMap((JSONObject)value); | ||
} | ||
|
||
map.put(key, value); | ||
} | ||
|
||
return map; | ||
} | ||
|
||
static List<Object> jsonArrayToList(JSONArray array) { | ||
List<Object> list = new ArrayList<>(); | ||
for(Object value : array) { | ||
if (value instanceof JSONArray) { | ||
value = jsonArrayToList((JSONArray)value); | ||
} else if (value instanceof JSONObject) { | ||
value = jsonObjectToMap((JSONObject)value); | ||
} | ||
|
||
list.add(value); | ||
} | ||
|
||
return list; | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
core-api/src/main/java/com/optimizely/ab/config/parser/JsonParseException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* | ||
* Copyright 2020, Optimizely and contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.optimizely.ab.config.parser; | ||
|
||
public final class JsonParseException extends Exception { | ||
public JsonParseException(String message) { | ||
super(message); | ||
} | ||
|
||
public JsonParseException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should have a corresponding
JsonHelpersTest
class for unit tests.