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