Skip to content

Commit 83f931a

Browse files
committed
Support serializing records (RecordValue) in addition to sequences
(TupleValues) in JsonSerialize operator. A RecordValue is serialized to a Json object, with the record value's (finite) domain of strings being the Json keys. [Feature][TLC] Signed-off-by: Markus Alexander Kuppe <[email protected]>
1 parent 0f9cf8a commit 83f931a

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

modules/tlc2/overrides/Json.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -159,31 +159,27 @@ public synchronized static BoolValue ndSerialize(final StringValue path, final V
159159
}
160160

161161
/**
162-
* Serializes a tuple of values to newline delimited JSON.
162+
* Serializes a TLA+ TupleValue or RecordValue to JSON.
163163
*
164164
* @param path the file to which to write the values
165165
* @param value the values to write
166166
* @return a boolean value indicating whether the serialization was successful
167167
*/
168168
@TLAPlusOperator(identifier = "JsonSerialize", module = "Json", warn = false)
169169
public synchronized static BoolValue serialize(final StringValue path, final Value v) throws IOException {
170-
final TupleValue value = (TupleValue) v.toTuple();
170+
Value value = v.toTuple();
171+
if (value == null) {
172+
value = v.toRcd();
173+
}
171174
if (value == null) {
172175
throw new EvalException(EC.TLC_MODULE_ARGUMENT_ERROR,
173-
new String[] { "second", "JsonSerialize", "sequence", Values.ppr(v.toString()) });
176+
new String[] { "second", "JsonSerialize", "sequence or record", Values.ppr(v.toString()) });
174177
}
175-
File file = new File(path.val.toString());
178+
179+
final File file = new File(path.val.toString());
176180
if (file.getParentFile() != null) {file.getParentFile().mkdirs();} // Cannot create parent dir for relative path.
177181
try (BufferedWriter writer = new BufferedWriter(new FileWriter(new File(path.val.toString())))) {
178-
writer.write("[\n");
179-
for (int i = 0; i < value.elems.length; i++) {
180-
writer.write(getNode(value.elems[i]).toString());
181-
if (i < value.elems.length - 1) {
182-
// No dangling "," after last element.
183-
writer.write(",\n");
184-
}
185-
}
186-
writer.write("\n]\n");
182+
writer.write(getNode(v).toString());
187183
}
188184
return BoolValue.ValTrue;
189185
}

tests/JsonTests.tla

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,13 @@ RoundTrip ==
161161
/\ output = JsonDeserialize("build/json/test.json")
162162
ASSUME(RoundTrip)
163163

164+
RoundTrip2 ==
165+
LET output == [a |-> 3, b |-> [c |-> <<4, 5, 6>>]]
166+
IN
167+
/\ JsonSerialize("target/json/test.json", output)
168+
/\ output = JsonDeserialize("target/json/test.json")
169+
ASSUME(RoundTrip2)
170+
164171
\* Deserialize existing ndjson with trailing white spaces and (empty) newlines.
165172

166173
ASSUME LET input == <<[a |-> 3], <<<<1, 2>>, "look">>, << <<[b |-> [c |-> <<4, 5, 6>>]]>> >> >>
@@ -169,21 +176,19 @@ ASSUME LET input == <<[a |-> 3], <<<<1, 2>>, "look">>, << <<[b |-> [c |-> <<4, 5
169176
-----
170177

171178
ASSUME AssertError(
172-
"The second argument of JsonSerialize should be a sequence, but instead it is:\n[a |-> 1, b |-> TRUE]",
173-
JsonSerialize("target/json/test.json", [a |-> 1, b |-> TRUE] ))
179+
"The second argument of JsonSerialize should be a sequence or record, but instead it is:\n{1, 2, 3}",
180+
JsonSerialize("target/json/test.json", {1,2,3} ))
174181

175182
ASSUME AssertError(
176183
"The second argument of ndJsonSerialize should be a sequence, but instead it is:\n[a |-> 1, b |-> TRUE]",
177184
ndJsonSerialize("target/json/test.json", [a |-> 1, b |-> TRUE] ))
178185

179-
180186
ASSUME AssertError(
181-
"The second argument of JsonSerialize should be a sequence, but instead it is:\n42",
187+
"The second argument of JsonSerialize should be a sequence or record, but instead it is:\n42",
182188
JsonSerialize("target/json/test.json", 42 ))
183189

184190
ASSUME AssertError(
185191
"The second argument of ndJsonSerialize should be a sequence, but instead it is:\n42",
186192
ndJsonSerialize("target/json/test.json", 42 ))
187193

188-
189194
=============================================================================

0 commit comments

Comments
 (0)