Skip to content

Commit

Permalink
improving tests and fixing bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
jurgenvinju committed Dec 19, 2024
1 parent e5d3b2a commit 0f2e799
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
10 changes: 6 additions & 4 deletions src/org/rascalmpl/library/lang/json/IO.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import Exception;
}
data RuntimeException(str cause="", str path="");

private str DEFAULT_DATETIME_FORMAT = "yyyy-MM-dd\'T\'HH:mm:ssZ";

@javaClass{org.rascalmpl.library.lang.json.IO}
@synopsis{reads JSON values from a stream}
@description{
Expand All @@ -45,7 +47,7 @@ First the expected type is used as a literal lookup, and then each value is test
java &T readJSON(
type[&T] expected,
loc src,
str dateTimeFormat = "yyyy-MM-dd\'T\'HH:mm:ssZ",
str dateTimeFormat = DEFAULT_DATETIME_FORMAT,
bool lenient=false,
bool trackOrigins=false,
JSONParser[value] parser = (type[value] _, str _) { throw ""; },
Expand Down Expand Up @@ -74,7 +76,7 @@ In general the translation behaves as the same as for ((readJSON)).}
java &T parseJSON(
type[&T] expected,
str src,
str dateTimeFormat = "yyyy-MM-dd\'T\'HH:mm:ssZ",
str dateTimeFormat = DEFAULT_DATETIME_FORMAT,
bool lenient=false,
bool trackOrigins=false,
JSONParser[value] parser = (type[value] _, str _) { throw ""; },
Expand Down Expand Up @@ -106,7 +108,7 @@ For `real` numbers that are larger than JVM's double you get "negative infinity"
}
java void writeJSON(loc target, value val,
bool unpackedLocations=false,
str dateTimeFormat="yyyy-MM-dd\'T\'HH:mm:ssZ",
str dateTimeFormat=DEFAULT_DATETIME_FORMAT,
bool dateTimeAsInt=false,
int indent=0,
bool dropOrigins=true,
Expand All @@ -121,7 +123,7 @@ java void writeJSON(loc target, value val,
@description{
This function uses `writeJSON` and stores the result in a string.
}
java str asJSON(value val, bool unpackedLocations=false, str dateTimeFormat="yyyy-MM-dd\'T\'HH:mm:ssZ", bool dateTimeAsInt=false, int indent = 0, bool dropOrigins=true, JSONFormatter[value] formatter = str (value _) { fail; }, bool explicitConstructorNames=false, bool explicitDataTypes=false);
java str asJSON(value val, bool unpackedLocations=false, str dateTimeFormat=DEFAULT_DATETIME_FORMAT, bool dateTimeAsInt=false, int indent = 0, bool dropOrigins=true, JSONFormatter[value] formatter = str (value _) { fail; }, bool explicitConstructorNames=false, bool explicitDataTypes=false);

@synopsis{((writeJSON)) and ((asJSON)) uses `Formatter` functions to flatten structured data to strings, on-demand}
@description{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.io.StringReader;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import java.math.RoundingMode;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -51,6 +52,7 @@
import io.usethesource.vallang.type.TypeFactory;
import io.usethesource.vallang.type.TypeStore;

import com.google.common.math.DoubleMath;
import com.google.gson.JsonParseException;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
Expand Down Expand Up @@ -774,7 +776,26 @@ public IValue visitNode(Type type) throws IOException {

@Override
public IValue visitNumber(Type type) throws IOException {
return visitInteger(type);
if (in.peek() == JsonToken.NUMBER) {
double d = in.nextDouble();

if (DoubleMath.isMathematicalInteger(d)) {
return vf.integer(DoubleMath.roundToLong(d, RoundingMode.FLOOR));
}
else {
return vf.real(d);
}
}
else if (in.peek() == JsonToken.STRING) {
var num = in.nextString();

if (num.contains("r")) {
var parts = num.split("r");
return vf.rational(vf.integer(parts[0]),vf.integer(parts[1]));
}
}

throw parseErrorHere("unexpected kind of number");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ test bool jsonWithDatetime1(datetime dt) = writeRead(#datetime, dt);
test bool jsonWithList1(list[int] dt) = writeRead(#list[int], dt);
test bool jsonWithSet1(set[int] dt) = writeRead(#set[int], dt);
test bool jsonWithMap1(map[int, int] dt) = writeRead(#map[int,int], dt);
test bool jsonWithNode1(node dt) = writeRead(#node, dt);
test bool jsonWithNode1(node dt) = writeRead(#node, dt, normalizer = allContainersToLists);

test bool jsonWithDATA11(DATA1 dt) = writeRead(#DATA1, dt);
test bool jsonWithDATA21(DATA2 dt) = writeRead(#DATA2, dt);
Expand Down Expand Up @@ -85,9 +85,9 @@ test bool originTracking() {
@synopsis{Normalizer used to create lists out of other containers}
value allContainersToLists(set[value] x) = [*x];
value allContainersToLists(value tup) = [] when \tuple(_) := typeOf(tup), "<tup>" == "\<\>";
value allContainersToList(<x>) = [x];
value allContainersToList(<x,y>) = [x,y];
value allContainersToList(<x,y,z>) = [x,y,z];
value allContainersToLists(<x>) = [x];
value allContainersToLists(<x,y>) = [x,y];
value allContainersToLists(<x,y,z>) = [x,y,z];
default value allContainersToLists(value x) = x;
Expand Down

0 comments on commit 0f2e799

Please sign in to comment.