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
30 changes: 14 additions & 16 deletions json-logic-java/src/main/java/com/jsonlogic/FilterNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,27 @@ Result eval(Map<String, Result> data) throws EvaluationException{

right.treeToString(sb);

if(sb.toString().contains("{\"var\":\"\"}"))
{
JsonArray results=new JsonArray();

JsonArray results=new JsonArray();

for(int i=0;i<leftResult.getArrayValue().size();i++)
{
String afterReplace=sb.toString().replace("{\"var\":\"\"}",leftResult.getArrayValue().get(i).getAsDouble()+"");
for(int i=0;i<leftResult.getArrayValue().size();i++)
{
//String afterReplace=sb.toString().replace("{\"var\":\"\"}",leftResult.getArrayValue().get(i).getAsDouble()+"");

try {
Result r=new JsonLogic().apply(afterReplace,"");
try {
Result r=new JsonLogic().apply(sb.toString(),leftResult.getArrayValue().get(i).toString());

if(r.getBooleanValue())
results.add(leftResult.getArrayValue().get(i).getAsDouble());
if(r.getBooleanValue())
results.add(leftResult.getArrayValue().get(i));

}catch (Exception ex){
ex.printStackTrace();
}
}catch (Exception ex){
ex.printStackTrace();
}

return new Result(results);
}

return null;
return new Result(results);


}

@Override
Expand Down
12 changes: 11 additions & 1 deletion json-logic-java/src/main/java/com/jsonlogic/JsonLogicTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.IOException;
import java.io.StringReader;
import java.util.*;
import java.util.regex.Matcher;

/**
* JsonLogicTree is a reusable representation of a 'JsonLogic' expression.
Expand Down Expand Up @@ -131,14 +132,20 @@ private void readValue(String name, JsonReader jsonReader, Map<String, Result> t
{
if(entry.getKey().startsWith(name+".$"+i))
{
String fieldName=entry.getKey().replaceAll("^"+name+"\\.\\$"+i+"\\.","");
String regName = name.replaceAll("\\$", Matcher.quoteReplacement("\\$"));

String fieldName=entry.getKey().replaceAll("^"+regName+"\\.\\$"+i+"\\.","");

if(entry.getValue().isString())
jj.addProperty(fieldName,entry.getValue().getStringValue());
else if(entry.getValue().isDouble())
jj.addProperty(fieldName,entry.getValue().getDoubleValue());
else if(entry.getValue().isBoolean())
jj.addProperty(fieldName,entry.getValue().getBooleanValue());
else if(entry.getValue().isNull())
jj.add(fieldName,null);
else if(entry.getValue().isArray())
jj.add(fieldName,entry.getValue().getArrayValue());
}
}

Expand All @@ -160,6 +167,9 @@ else if(entry.getValue().isDouble())
doomed.add(entry.getValue().getDoubleValue());
else if(entry.getValue().isBoolean())
doomed.add(entry.getValue().getBooleanValue());

else if(entry.getValue().isArray())
doomed.add(entry.getValue().getArrayValue());
}

jsonReader.endArray();
Expand Down
13 changes: 10 additions & 3 deletions json-logic-java/src/main/java/com/jsonlogic/ReduceNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ Result eval(Map<String, Result> data) throws EvaluationException{

nodes.get(1).treeToString(sb);

if(!sb.toString().contains("{\"var\":\"current\"}")||!sb.toString().contains("{\"var\":\"accumulator\"}"))
throw new EvaluationException("reduce operation missing current or accumulator");
if(!sb.toString().contains("{\"var\":\"accumulator\"}"))
throw new EvaluationException("reduce operation missing accumulator");

Double accumulator=0.0;

Expand All @@ -54,10 +54,17 @@ Result eval(Map<String, Result> data) throws EvaluationException{

for(int i=0;i<leftResult.getArrayValue().size();i++)
{
String afterReplace=sb.toString()
String afterReplace;
if(sb.toString().contains("{\"var\":\"current\"}")){
afterReplace=sb.toString()
.replace("{\"var\":\"current\"}",leftResult.getArrayValue().get(i).getAsDouble()+"")
.replace("{\"var\":\"accumulator\"}",accumulator+"")
;
}else{
afterReplace=sb.toString()
.replace("{\"var\":\"accumulator\"}",accumulator+"")
;
}

try {
Result r=new JsonLogic().apply(afterReplace,"");
Expand Down