From 6996898ffb03347619fff0dd100edbb7bf0e27ac Mon Sep 17 00:00:00 2001 From: "Jurgen J. Vinju" Date: Sat, 23 Nov 2024 13:53:26 +0100 Subject: [PATCH] added more tests and fixed more bugs --- .../library/lang/json/internal/JsonValueReader.java | 6 +++--- .../lang/rascal/tests/library/lang/json/JSONIOTests.rsc | 7 ++++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/org/rascalmpl/library/lang/json/internal/JsonValueReader.java b/src/org/rascalmpl/library/lang/json/internal/JsonValueReader.java index ffc9c00d512..e43e658afa9 100644 --- a/src/org/rascalmpl/library/lang/json/internal/JsonValueReader.java +++ b/src/org/rascalmpl/library/lang/json/internal/JsonValueReader.java @@ -25,8 +25,6 @@ import java.util.List; import java.util.Map; import java.util.Set; -import java.util.stream.Collectors; - import org.rascalmpl.debug.IRascalMonitor; import org.rascalmpl.uri.URIUtil; import io.usethesource.vallang.IInteger; @@ -494,7 +492,6 @@ private int getCol() { } } - @Override public IValue visitAbstractData(Type type) throws IOException { if (in.peek() == JsonToken.STRING) { @@ -574,6 +571,9 @@ else if (explicitConstructorNames && consName == null) { if (alternatives.size() > 1) { monitor.warning("selecting arbitrary constructor for " + type, vf.sourceLocation(in.getPath())); } + else if (alternatives.size() == 0) { + throw new IOException("No fitting constructor found for " + in.getPath()); + } Type cons = alternatives.iterator().next(); diff --git a/src/org/rascalmpl/library/lang/rascal/tests/library/lang/json/JSONIOTests.rsc b/src/org/rascalmpl/library/lang/rascal/tests/library/lang/json/JSONIOTests.rsc index f94c3d06cff..0fd17e4119f 100644 --- a/src/org/rascalmpl/library/lang/rascal/tests/library/lang/json/JSONIOTests.rsc +++ b/src/org/rascalmpl/library/lang/rascal/tests/library/lang/json/JSONIOTests.rsc @@ -91,10 +91,15 @@ test bool explicitDataTypes() { assert json == "{\"_constructor\":\"data4\",\"_type\":\"DATA4\",\"e\":{\"_constructor\":\"z\",\"_type\":\"Enum\"}}"; + // _constructor and _type must be the first fields assert parseJSON(#DATA4, json, explicitDataTypes=true) == example; + // _type and _constructor may appear in a different order + flippedJson = "{\"_type\":\"DATA4\",\"_constructor\":\"data4\",\"e\":{\"_constructor\":\"z\",\"_type\":\"Enum\"}}"; + assert parseJSON(#DATA4, flippedJson, explicitDataTypes=true) == example; + // here we can't be sure to get z() back, but we will get some Enum - // assert data4(e=Enum _) := parseJSON(#DATA4, json, explicitDataTypes=false); + assert data4(e=Enum _) := parseJSON(#DATA4, json, explicitDataTypes=false); return true; } \ No newline at end of file