Skip to content

Commit

Permalink
remove *WithNull references
Browse files Browse the repository at this point in the history
  • Loading branch information
pfeodrippe committed Jan 8, 2021
1 parent 225281b commit 620e358
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 79 deletions.
16 changes: 0 additions & 16 deletions modules/Json.tla
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,6 @@ JsonDeserialize(absoluteFilename) ==
(*************************************************************************)
CHOOSE val : TRUE

JsonDeserializeWithNull(absoluteFilename, nullValue) ==
(*************************************************************************)
(* Deserializes JSON values from the given file. JSON objects will be *)
(* deserialized to records, and arrays will be deserialized to tuples. *)
(*************************************************************************)
CHOOSE val : TRUE

ndJsonSerialize(absoluteFilename, value) ==
(*************************************************************************)
(* Serializes a tuple of values to the given file as newline delimited *)
Expand All @@ -108,13 +101,4 @@ ndJsonDeserialize(absoluteFilename) ==
(* and arrays will be deserialized to tuples. *)
(*************************************************************************)
CHOOSE val : TRUE

ndJsonDeserializeWithNull(absoluteFilename, nullValue) ==
(*************************************************************************)
(* Deserializes JSON values from the given file. JSON values must be *)
(* separated by newlines. JSON objects will be deserialized to records, *)
(* and arrays will be deserialized to tuples. *)
(*************************************************************************)
CHOOSE val : TRUE

=============================================================================
61 changes: 7 additions & 54 deletions modules/tlc2/overrides/Json.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,27 +117,6 @@ public static IValue ndDeserialize(final StringValue path) throws IOException {
return new TupleValue(values.toArray(new Value[0]));
}

/**
* Deserializes a tuple of newline delimited JSON values from the given path.
*
* @param path the JSON file path
* @param nullValue the {@code Value} which is interpreted as the null value
* @return a tuple of JSON values
*/
@TLAPlusOperator(identifier = "ndJsonDeserializeWithNull", module = "Json", warn = false)
public static IValue ndDeserializeWithNull(final StringValue path, Value nullValue) throws IOException {
List<Value> values = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(new File(path.val.toString())))) {
String line = reader.readLine();
while (line != null) {
JsonElement node = JsonParser.parseString(line);
values.add(getValue(node, nullValue));
line = reader.readLine();
}
}
return new TupleValue(values.toArray(new Value[0]));
}

/**
* Deserializes a tuple of *plain* JSON values from the given path.
*
Expand All @@ -150,19 +129,6 @@ public static IValue deserialize(final StringValue path) throws IOException {
return getValue(node);
}

/**
* Deserializes a tuple of *plain* JSON values from the given path.
*
* @param path the JSON file path
* @param nullValue the {@code Value} which is interpreted as the null value
* @return a tuple of JSON values
*/
@TLAPlusOperator(identifier = "JsonDeserializeWithNull", module = "Json", warn = false)
public static IValue deserializeWithNull(final StringValue path, final Value nullValue) throws IOException {
JsonElement node = JsonParser.parseReader(new FileReader(new File(path.val.toString())));
return getValue(node, nullValue);
}

/**
* Serializes a tuple of values to newline delimited JSON.
*
Expand Down Expand Up @@ -428,22 +394,11 @@ private static JsonElement getArrayNode(SetEnumValue value) throws IOException {
* @return the converted value
*/
private static Value getValue(JsonElement node) throws IOException {
return getValue(node, null);
}

/**
* Recursively converts the given {@code JsonElement} to a TLC value.
*
* @param node the {@code JsonElement} to convert
* @param nullValue the {@code Value} which is interpreted as the null value
* @return the converted value
*/
private static Value getValue(JsonElement node, Value nullValue) throws IOException {
if (node.isJsonArray()) {
return getTupleValue(node, nullValue);
return getTupleValue(node);
}
else if (node.isJsonObject()) {
return getRecordValue(node, nullValue);
return getRecordValue(node);
}
else if (node.isJsonPrimitive()) {
JsonPrimitive primitive = node.getAsJsonPrimitive();
Expand All @@ -458,7 +413,7 @@ else if (primitive.isString()) {
}
}
else if (node.isJsonNull()) {
return nullValue;
return null;
}
throw new IOException("Cannot convert value: unsupported JSON value " + node.toString());
}
Expand All @@ -467,14 +422,13 @@ else if (node.isJsonNull()) {
* Converts the given {@code JsonElement} to a tuple.
*
* @param node the {@code JsonElement} to convert
* @param nullValue the {@code Value} which is interpreted as the null value
* @return the tuple value
*/
private static TupleValue getTupleValue(JsonElement node, Value nullValue) throws IOException {
private static TupleValue getTupleValue(JsonElement node) throws IOException {
List<Value> values = new ArrayList<>();
JsonArray jsonArray = node.getAsJsonArray();
for (int i = 0; i < jsonArray.size(); i++) {
values.add(getValue(jsonArray.get(i), nullValue));
values.add(getValue(jsonArray.get(i)));
}
return new TupleValue(values.toArray(new Value[0]));
}
Expand All @@ -483,17 +437,16 @@ private static TupleValue getTupleValue(JsonElement node, Value nullValue) throw
* Converts the given {@code JsonElement} to a record.
*
* @param node the {@code JsonElement} to convert
* @param nullValue the {@code Value} which is interpreted as the null value
* @return the record value
*/
private static RecordValue getRecordValue(JsonElement node, Value nullValue) throws IOException {
private static RecordValue getRecordValue(JsonElement node) throws IOException {
List<UniqueString> keys = new ArrayList<>();
List<Value> values = new ArrayList<>();
Iterator<Map.Entry<String, JsonElement>> iterator = node.getAsJsonObject().entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, JsonElement> entry = iterator.next();
keys.add(UniqueString.uniqueStringOf(entry.getKey()));
values.add(getValue(entry.getValue(), nullValue));
values.add(getValue(entry.getValue()));
}
return new RecordValue(keys.toArray(new UniqueString[0]), values.toArray(new Value[0]), false);
}
Expand Down
8 changes: 0 additions & 8 deletions tests/JsonTests.tla
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,4 @@ RoundTrip ==
/\ JsonSerialize("build/json/test.json", output)
/\ output = JsonDeserialize("build/json/test.json")
ASSUME(RoundTrip)

\* Test the null support
NullValues ==
/\ [eita |-> {1, 2}, abc |-> <<{1, 2}, "null">>] =
JsonDeserializeWithNull("tests/fixture/json-with-null.json", {1, 2})
/\ <<[eita |-> {1, 2}, abc |-> <<{1, 2}, "null">>]>> =
ndJsonDeserializeWithNull("tests/fixture/json-with-null.json", {1, 2})
ASSUME(NullValues)
=============================================================================
1 change: 0 additions & 1 deletion tests/fixture/json-with-null.json

This file was deleted.

0 comments on commit 620e358

Please sign in to comment.