Skip to content

Commit 2b743d0

Browse files
committed
ToJsonObject formats a PlusCal pc variable in Json syntax. The output
of ToJsonObject is valid input for [ShiViz](https://bestchai.bitbucket.io/shiviz/).
1 parent 61c21a9 commit 2b743d0

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

Json.tla

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
-------------------------------- MODULE Json --------------------------------
2+
3+
LOCAL INSTANCE Sequences
4+
LOCAL INSTANCE TLC
5+
(*************************************************************************)
6+
(* Imports the definitions from the modules, but doesn't export them. *)
7+
(*************************************************************************)
8+
9+
-----------------------------------------------------------------------------
10+
11+
LOCAL ToJsonKeyValue(F, d) ==
12+
ToString(ToString(d)) \o ":" \o ToString(F[d])
13+
14+
RECURSIVE ToJson(_,_)
15+
LOCAL ToJson(F, D) == \* LOCAL just a hint for humans.
16+
LET d == CHOOSE d \in D: TRUE
17+
IN IF D = DOMAIN F
18+
THEN "{" \o ToJsonKeyValue(F, d) \o IF D \ {d} = {}
19+
THEN "}"
20+
ELSE ToJson(F, D \ {d})
21+
ELSE ", " \o ToJsonKeyValue(F, d) \o IF D \ {d} = {}
22+
THEN "}"
23+
ELSE ToJson(F, D \ {d})
24+
25+
ToJsonObject(F) ==
26+
(*************************************************************************)
27+
(* This equals a string that is the Json representation of the given *)
28+
(* function F s.t. DOMAIN F \subseteq Nat and Range(F) \subseteq STRING *)
29+
(* with Range(g) == { g[x] : x \in DOMAIN g }. *)
30+
(*************************************************************************)
31+
IF DOMAIN F = {}
32+
THEN "{}"
33+
ELSE ToJson(F, DOMAIN F)
34+
35+
36+
=============================================================================

JsonTests.tla

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
----------------------------- MODULE JsonTests -----------------------------
2+
EXTENDS Json, TLC
3+
4+
ASSUME(ToJsonObject(<< >>) = "{}")
5+
ASSUME(ToJsonObject([ i \in {0,1,3} |-> "a" ]) = "{\"0\":\"a\", \"1\":\"a\", \"3\":\"a\"}")
6+
ASSUME(ToJsonObject([ i \in {0,1} |-> "a" ]) = "{\"0\":\"a\", \"1\":\"a\"}")
7+
ASSUME(ToJsonObject([ i \in {1} |-> "a" ]) = "{\"1\":\"a\"}")
8+
9+
=============================================================================

0 commit comments

Comments
 (0)