From e5d3b2aa618ba4f2823563596a5e7a4d7272db30 Mon Sep 17 00:00:00 2001 From: "Jurgen J. Vinju" Date: Wed, 18 Dec 2024 20:03:10 +0100 Subject: [PATCH] fixed bugs with tuples and nodes --- .../library/lang/json/internal/JsonValueReader.java | 10 +++++++++- .../library/lang/json/internal/JsonValueWriter.java | 3 +++ .../rascal/tests/library/lang/json/JSONIOTests.rsc | 5 +++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/org/rascalmpl/library/lang/json/internal/JsonValueReader.java b/src/org/rascalmpl/library/lang/json/internal/JsonValueReader.java index 051a482798..29d69e808b 100644 --- a/src/org/rascalmpl/library/lang/json/internal/JsonValueReader.java +++ b/src/org/rascalmpl/library/lang/json/internal/JsonValueReader.java @@ -42,6 +42,7 @@ import io.usethesource.vallang.IMapWriter; import io.usethesource.vallang.ISetWriter; import io.usethesource.vallang.ISourceLocation; +import io.usethesource.vallang.IString; import io.usethesource.vallang.IValue; import io.usethesource.vallang.IValueFactory; import io.usethesource.vallang.io.StandardTextReader; @@ -726,9 +727,16 @@ public IValue visitNode(Type type) throws IOException { Map kws = new HashMap<>(); Map args = new HashMap<>(); + String name = "object"; while (in.hasNext()) { String kwName = nextName(); + + if (kwName.equals("_name")) { + name = ((IString) read(in, TF.stringType())).getValue(); + continue; + } + boolean positioned = kwName.startsWith("arg"); if (!isNull()) { // lookahead for null to give default parameters the preference. @@ -761,7 +769,7 @@ public IValue visitNode(Type type) throws IOException { .map(e -> e.getValue()) .toArray(IValue[]::new); - return vf.node("object", argArray, kws); + return vf.node(name, argArray, kws); } @Override diff --git a/src/org/rascalmpl/library/lang/json/internal/JsonValueWriter.java b/src/org/rascalmpl/library/lang/json/internal/JsonValueWriter.java index c60811295b..49df691818 100644 --- a/src/org/rascalmpl/library/lang/json/internal/JsonValueWriter.java +++ b/src/org/rascalmpl/library/lang/json/internal/JsonValueWriter.java @@ -226,6 +226,9 @@ public Void visitTuple(ITuple o) throws IOException { @Override public Void visitNode(INode o) throws IOException { out.beginObject(); + out.name("_name"); + out.value(o.getName()); + int i = 0; for (IValue arg : o) { out.name("arg" + i++); 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 d05277067d..fdd25198a7 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 @@ -84,6 +84,11 @@ 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), "" == "\<\>"; +value allContainersToList() = [x]; +value allContainersToList() = [x,y]; +value allContainersToList() = [x,y,z]; + default value allContainersToLists(value x) = x; test bool accurateParseErrors() {