-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
avoid the conversion to a map and go native
- Loading branch information
Showing
2 changed files
with
136 additions
and
3 deletions.
There are no files selected for viewing
107 changes: 104 additions & 3 deletions
107
src/main/java/io/mapsmessaging/selector/resolvers/JsonEvaluator.java
This file contains 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 |
---|---|---|
@@ -1,10 +1,111 @@ | ||
package io.mapsmessaging.selector.resolvers; | ||
|
||
import io.mapsmessaging.selector.IdentifierMutator; | ||
import org.json.JSONArray; | ||
import org.json.JSONObject; | ||
|
||
public class JsonEvaluator extends MapEvaluator { | ||
import java.math.BigDecimal; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.StringTokenizer; | ||
|
||
public JsonEvaluator(JSONObject bean){ | ||
super(bean.toMap()); | ||
public class JsonEvaluator implements IdentifierMutator { | ||
|
||
private final JSONObject jsonObject; | ||
|
||
public JsonEvaluator(JSONObject jsonObject){ | ||
this.jsonObject = jsonObject; | ||
} | ||
|
||
|
||
@Override | ||
public Object get(String key) { | ||
String[] keyPath = getKeyPath(key); | ||
Object lookup = locateObject(jsonObject, keyPath); | ||
return parseJSON(lookup); | ||
} | ||
|
||
@Override | ||
public Object remove(String key) { | ||
return get(key); | ||
} | ||
|
||
@Override | ||
public Object set(String key, Object value) { | ||
return false; | ||
} | ||
|
||
private String[] getKeyPath(String key){ | ||
String[] keyPath; | ||
if(key.contains(".")){ | ||
var stringTokenizer = new StringTokenizer(key, "."); | ||
List<String> tmp = new ArrayList<>(); | ||
while(stringTokenizer.hasMoreElements()){ | ||
tmp.add(stringTokenizer.nextElement().toString()); | ||
} | ||
var tmpPath = new String[tmp.size()]; | ||
keyPath = tmp.toArray(tmpPath); | ||
} | ||
else{ | ||
keyPath = new String[1]; | ||
keyPath[0] = key; | ||
} | ||
return keyPath; | ||
} | ||
|
||
private static Object locateObject(JSONObject json, String[] searchPath){ | ||
if(searchPath != null){ | ||
// Walk the JSON path first | ||
for(var x=0;x<searchPath.length;x++){ | ||
var path = searchPath[x]; | ||
var jsonLookup = json.get(path); | ||
if(jsonLookup instanceof JSONArray){ | ||
var sub = new String[searchPath.length-(x +1)]; | ||
System.arraycopy(searchPath, x+1, sub, 0, sub.length); | ||
return arrayLookup(json.getJSONArray(path), sub); | ||
} | ||
else if(jsonLookup instanceof JSONObject){ | ||
json = (JSONObject) jsonLookup; | ||
} | ||
else{ | ||
return jsonLookup; | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private static Object arrayLookup(JSONArray array, String[] path){ | ||
// We have an array, so the next element in the path must be an index ( ie number) | ||
var idx = Integer.parseInt(path[0]); | ||
Object lookup = array.get(idx); | ||
if(lookup instanceof JSONObject){ | ||
var sub = new String[path.length-1]; | ||
System.arraycopy(path, 1, sub, 0, sub.length); | ||
return locateObject( (JSONObject) lookup, sub); | ||
} | ||
else if(lookup instanceof JSONArray){ | ||
var sub = new String[path.length-1]; | ||
System.arraycopy(path, 1, sub, 0, sub.length); | ||
return arrayLookup( (JSONArray) lookup, sub); | ||
} | ||
return lookup; | ||
} | ||
|
||
private static Object parseJSON(Object lookup){ | ||
if (lookup instanceof String || | ||
lookup instanceof Float || | ||
lookup instanceof Double || | ||
lookup instanceof Byte || | ||
lookup instanceof Short || | ||
lookup instanceof Integer || | ||
lookup instanceof Long) { | ||
return lookup; | ||
} | ||
else if(lookup instanceof BigDecimal){ | ||
return ((BigDecimal)lookup).doubleValue(); | ||
} | ||
return null; | ||
} | ||
|
||
} |
This file contains 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