Skip to content

Commit

Permalink
Can use explicitConstructors and explicitTypes while reading JSON bac…
Browse files Browse the repository at this point in the history
…k as well to improve constructor and type selection accuracy
  • Loading branch information
jurgenvinju committed Sep 12, 2023
1 parent d86c683 commit 4cabf0c
Showing 1 changed file with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
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;
Expand Down Expand Up @@ -59,6 +60,8 @@ public class JsonValueReader {
private VarHandle posHandler;
private VarHandle lineHandler;
private VarHandle lineStartHandler;
private boolean explicitConstructorNames;
private boolean explicitDataTypes;

/**
* @param vf factory which will be used to construct values
Expand Down Expand Up @@ -105,6 +108,17 @@ protected SimpleDateFormat initialValue() {
return this;
}

public JsonValueReader setExplicitConstructorNames(boolean value) {
this.explicitConstructorNames = value;
return this;

Check warning on line 113 in src/org/rascalmpl/library/lang/json/internal/JsonValueReader.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/library/lang/json/internal/JsonValueReader.java#L112-L113

Added lines #L112 - L113 were not covered by tests
}

public JsonValueReader setExplicitDataTypes(boolean value) {
this.explicitDataTypes = value;
return this;

Check warning on line 118 in src/org/rascalmpl/library/lang/json/internal/JsonValueReader.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/library/lang/json/internal/JsonValueReader.java#L117-L118

Added lines #L117 - L118 were not covered by tests
}


/**
* Read and validate a Json stream as an IValue
* @param in json stream
Expand Down Expand Up @@ -495,7 +509,37 @@ public IValue visitAbstractData(Type type) throws IOException {

assert in.peek() == JsonToken.BEGIN_OBJECT;

Set<Type> alternatives = store.lookupAlternatives(type);
Set<Type> alternatives = null;

Check warning on line 512 in src/org/rascalmpl/library/lang/json/internal/JsonValueReader.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/library/lang/json/internal/JsonValueReader.java#L512

Added line #L512 was not covered by tests

// use explicit information in the JSON to select and filter constructors from the TypeStore
// we expect always to have the field _constructor before _type.
if (explicitConstructorNames) {
String consLabel = in.nextName();

Check warning on line 517 in src/org/rascalmpl/library/lang/json/internal/JsonValueReader.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/library/lang/json/internal/JsonValueReader.java#L517

Added line #L517 was not covered by tests
if ("_constructor".equals(consLabel)) {
String consName = in.nextString();

Check warning on line 519 in src/org/rascalmpl/library/lang/json/internal/JsonValueReader.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/library/lang/json/internal/JsonValueReader.java#L519

Added line #L519 was not covered by tests

alternatives = store.lookupConstructors(consName);

Check warning on line 521 in src/org/rascalmpl/library/lang/json/internal/JsonValueReader.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/library/lang/json/internal/JsonValueReader.java#L521

Added line #L521 was not covered by tests

if (explicitDataTypes) {
String dtLabel = in.nextName();

Check warning on line 524 in src/org/rascalmpl/library/lang/json/internal/JsonValueReader.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/library/lang/json/internal/JsonValueReader.java#L524

Added line #L524 was not covered by tests

if ("_type".equals(dtLabel)) {
String dtValue = in.nextString();

Check warning on line 527 in src/org/rascalmpl/library/lang/json/internal/JsonValueReader.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/library/lang/json/internal/JsonValueReader.java#L527

Added line #L527 was not covered by tests
alternatives = alternatives.stream().filter(t -> t.isAbstractData() && t.getName().equals(dtValue)).collect(Collectors.toSet());
}

Check warning on line 529 in src/org/rascalmpl/library/lang/json/internal/JsonValueReader.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/library/lang/json/internal/JsonValueReader.java#L529

Added line #L529 was not covered by tests
else {
throw new IOException("Expected _type field but got " + dtLabel);

Check warning on line 531 in src/org/rascalmpl/library/lang/json/internal/JsonValueReader.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/library/lang/json/internal/JsonValueReader.java#L531

Added line #L531 was not covered by tests
}
}
}

Check warning on line 534 in src/org/rascalmpl/library/lang/json/internal/JsonValueReader.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/library/lang/json/internal/JsonValueReader.java#L534

Added line #L534 was not covered by tests
else {
throw new IOException("Expected _constructor field but got " + consLabel);

Check warning on line 536 in src/org/rascalmpl/library/lang/json/internal/JsonValueReader.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/library/lang/json/internal/JsonValueReader.java#L536

Added line #L536 was not covered by tests
}
}

Check warning on line 538 in src/org/rascalmpl/library/lang/json/internal/JsonValueReader.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/library/lang/json/internal/JsonValueReader.java#L538

Added line #L538 was not covered by tests
else {
alternatives = store.lookupAlternatives(type);

Check warning on line 540 in src/org/rascalmpl/library/lang/json/internal/JsonValueReader.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/library/lang/json/internal/JsonValueReader.java#L540

Added line #L540 was not covered by tests
}

if (alternatives.size() > 1) {
monitor.warning("selecting arbitrary constructor for " + type, vf.sourceLocation(in.getPath()));
}
Expand Down

0 comments on commit 4cabf0c

Please sign in to comment.