11package 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
512import liquidjava .rj_language .ast .Expression ;
613import liquidjava .rj_language .ast .LiteralBoolean ;
1017
1118public 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}
0 commit comments