Skip to content

Commit 893b1e0

Browse files
committed
DerivationNode JSON Serialization Using Gson
1 parent 88e063c commit 893b1e0

File tree

7 files changed

+44
-96
lines changed

7 files changed

+44
-96
lines changed

liquidjava-verifier/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,11 @@
188188
<artifactId>antlr4-runtime</artifactId>
189189
<version>4.7.1</version>
190190
</dependency>
191+
<dependency>
192+
<groupId>com.google.code.gson</groupId>
193+
<artifactId>gson</artifactId>
194+
<version>2.10.1</version>
195+
</dependency>
191196

192197
</dependencies>
193198
<dependencyManagement>
Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package liquidjava.rj_language.opt.derivation_node;
22

3-
import java.util.Map;
4-
53
public class BinaryDerivationNode extends DerivationNode {
64

5+
private final String op;
76
private final ValDerivationNode left;
87
private final ValDerivationNode right;
9-
private final String op;
108

119
public BinaryDerivationNode(ValDerivationNode left, ValDerivationNode right, String op) {
1210
this.left = left;
@@ -25,15 +23,4 @@ public ValDerivationNode getRight() {
2523
public String getOp() {
2624
return op;
2725
}
28-
29-
@Override
30-
public Map<String, Object> toJson() {
31-
Map<String, Object> json = baseJson();
32-
json.put("op", op);
33-
if (left != null)
34-
json.put("left", left.toJson());
35-
if (right != null)
36-
json.put("right", right.toJson());
37-
return json;
38-
}
3926
}
Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,17 @@
11
package liquidjava.rj_language.opt.derivation_node;
22

3-
import java.util.LinkedHashMap;
4-
import java.util.Map;
3+
import com.google.gson.Gson;
4+
import com.google.gson.GsonBuilder;
55

66
public abstract class DerivationNode {
77

8+
private static final Gson gson = new GsonBuilder()
9+
.setPrettyPrinting() // remove later
10+
.disableHtmlEscaping() // to not escape characters like &, >, <, =, etc.
11+
.create();
12+
813
@Override
914
public String toString() {
10-
return prettyPrint(toJson(), "");
11-
}
12-
13-
private String prettyPrint(Map<String, Object> json, String indent) {
14-
StringBuilder sb = new StringBuilder();
15-
String nextIndent = indent + " ";
16-
sb.append("{\n");
17-
for (String key : json.keySet()) {
18-
sb.append(nextIndent + "\"" + key + "\": ");
19-
Object value = json.get(key);
20-
if (value instanceof Map) {
21-
sb.append(prettyPrint((Map<String, Object>) value, nextIndent));
22-
} else if (value instanceof String) {
23-
sb.append("\"" + value + "\"");
24-
} else {
25-
sb.append(value);
26-
}
27-
sb.append(",\n");
28-
}
29-
if (json.size() > 0) {
30-
sb.setLength(sb.length() - 2); // remove last comma
31-
sb.append("\n");
32-
}
33-
sb.append(indent + "}");
34-
return sb.toString();
35-
}
36-
37-
public abstract Map<String, Object> toJson();
38-
39-
protected Map<String, Object> baseJson() {
40-
return new LinkedHashMap<>();
15+
return gson.toJson(this);
4116
}
4217
}
Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,20 @@
11
package liquidjava.rj_language.opt.derivation_node;
22

3-
import java.util.Map;
4-
53
public class UnaryDerivationNode extends DerivationNode {
64

5+
private final String op;
76
private final ValDerivationNode operand;
8-
private final String operator;
97

10-
public UnaryDerivationNode(ValDerivationNode operand, String operator) {
8+
public UnaryDerivationNode(ValDerivationNode operand, String op) {
119
this.operand = operand;
12-
this.operator = operator;
10+
this.op = op;
1311
}
1412

1513
public ValDerivationNode getOperand() {
1614
return operand;
1715
}
1816

19-
public String getOperator() {
20-
return operator;
21-
}
22-
23-
@Override
24-
public Map<String, Object> toJson() {
25-
Map<String, Object> json = baseJson();
26-
json.put("op", operator);
27-
if (operand != null)
28-
json.put("operand", operand.toJson());
29-
return json;
17+
public String getOp() {
18+
return op;
3019
}
3120
}
Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
package liquidjava.rj_language.opt.derivation_node;
22

3-
import java.util.Map;
3+
import java.lang.reflect.Type;
4+
5+
import com.google.gson.JsonElement;
6+
import com.google.gson.JsonNull;
7+
import com.google.gson.JsonPrimitive;
8+
import com.google.gson.JsonSerializationContext;
9+
import com.google.gson.JsonSerializer;
10+
import com.google.gson.annotations.JsonAdapter;
411

512
import liquidjava.rj_language.ast.Expression;
613
import liquidjava.rj_language.ast.LiteralBoolean;
@@ -10,6 +17,7 @@
1017

1118
public class ValDerivationNode extends DerivationNode {
1219

20+
@JsonAdapter(ExpressionSerializer.class)
1321
private final Expression value;
1422
private final DerivationNode origin;
1523

@@ -26,26 +34,20 @@ public DerivationNode getOrigin() {
2634
return origin;
2735
}
2836

29-
@Override
30-
public Map<String, Object> toJson() {
31-
Map<String, Object> json = baseJson();
32-
json.put("value", expToValue(value));
33-
if (origin != null)
34-
json.put("origin", origin.toJson());
35-
return json;
36-
}
37-
38-
private Object expToValue(Expression exp) {
39-
if (exp == null)
40-
return null;
41-
if (exp instanceof LiteralInt)
42-
return ((LiteralInt) exp).getValue();
43-
if (exp instanceof LiteralReal)
44-
return ((LiteralReal) exp).getValue();
45-
if (exp instanceof LiteralBoolean)
46-
return ((LiteralBoolean) exp).isBooleanTrue();
47-
if (exp instanceof Var)
48-
return ((Var) exp).getName();
49-
return exp.toString();
37+
private static class ExpressionSerializer implements JsonSerializer<Expression> {
38+
@Override
39+
public JsonElement serialize(Expression exp, Type typeOfSrc, JsonSerializationContext context) {
40+
if (exp == null)
41+
return JsonNull.INSTANCE;
42+
if (exp instanceof LiteralInt)
43+
return new JsonPrimitive(((LiteralInt) exp).getValue());
44+
if (exp instanceof LiteralReal)
45+
return new JsonPrimitive(((LiteralReal) exp).getValue());
46+
if (exp instanceof LiteralBoolean)
47+
return new JsonPrimitive(((LiteralBoolean) exp).isBooleanTrue());
48+
if (exp instanceof Var)
49+
return new JsonPrimitive(((Var) exp).getName());
50+
return new JsonPrimitive(exp.toString());
51+
}
5052
}
5153
}
Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package liquidjava.rj_language.opt.derivation_node;
22

3-
import java.util.Map;
4-
53
public class VarDerivationNode extends DerivationNode {
64

75
private final String var;
@@ -13,12 +11,4 @@ public VarDerivationNode(String var) {
1311
public String getVar() {
1412
return var;
1513
}
16-
17-
@Override
18-
public Map<String, Object> toJson() {
19-
Map<String, Object> json = baseJson();
20-
if (var != null)
21-
json.put("var", var);
22-
return json;
23-
}
2414
}

liquidjava-verifier/src/test/java/liquidjava/rj_language/opt/ExpressionSimplifierTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ private void assertDerivationEquals(DerivationNode expected, DerivationNode actu
331331
} else if (expected instanceof UnaryDerivationNode) {
332332
UnaryDerivationNode expectedUnary = (UnaryDerivationNode) expected;
333333
UnaryDerivationNode actualUnary = (UnaryDerivationNode) actual;
334-
assertEquals(expectedUnary.getOperator(), actualUnary.getOperator(), message + ": operators should match");
334+
assertEquals(expectedUnary.getOp(), actualUnary.getOp(), message + ": operators should match");
335335
assertDerivationEquals(expectedUnary.getOperand(), actualUnary.getOperand(), message + " > operand");
336336
}
337337
}

0 commit comments

Comments
 (0)