Skip to content

Commit

Permalink
Revert "Implement stream methods in JsonStructureParser; fulfill more…
Browse files Browse the repository at this point in the history
… of JSONP Parser in it & JsonParserImpl; higher code coverage"

the change breaks TCKs

This reverts commit 146a2f7.
  • Loading branch information
lukasj committed Feb 27, 2024
1 parent 30da884 commit f2b0547
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 979 deletions.
107 changes: 97 additions & 10 deletions impl/src/main/java/org/eclipse/parsson/JsonParserImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@
import java.io.Reader;
import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.util.AbstractMap;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import jakarta.json.JsonArray;
import jakarta.json.JsonArrayBuilder;
Expand Down Expand Up @@ -58,24 +63,20 @@ public class JsonParserImpl implements JsonParser {

private final JsonContext jsonContext;

private final JsonParserStreamCreator streamCreator = new JsonParserStreamCreator(this, true, () -> currentEvent,
() -> currentContext instanceof NoneContext);


JsonParserImpl(Reader reader, JsonContext jsonContext) {
public JsonParserImpl(Reader reader, JsonContext jsonContext) {
this.jsonContext = jsonContext;
stack = new Stack(jsonContext.depthLimit());
this.tokenizer = new JsonTokenizer(reader, jsonContext);
}

JsonParserImpl(InputStream in, JsonContext jsonContext) {
public JsonParserImpl(InputStream in, JsonContext jsonContext) {
this.jsonContext = jsonContext;
stack = new Stack(jsonContext.depthLimit());
UnicodeDetectingInputStream uin = new UnicodeDetectingInputStream(in);
this.tokenizer = new JsonTokenizer(new InputStreamReader(uin, uin.getCharset()), jsonContext);
}

JsonParserImpl(InputStream in, Charset encoding, JsonContext jsonContext) {
public JsonParserImpl(InputStream in, Charset encoding, JsonContext jsonContext) {
this.jsonContext = jsonContext;
stack = new Stack(jsonContext.depthLimit());
this.tokenizer = new JsonTokenizer(new InputStreamReader(in, encoding), jsonContext);
Expand Down Expand Up @@ -177,17 +178,103 @@ public JsonValue getValue() {

@Override
public Stream<JsonValue> getArrayStream() {
return streamCreator.getArrayStream();
if (currentEvent != Event.START_ARRAY) {
throw new IllegalStateException(
JsonMessages.PARSER_GETARRAY_ERR(currentEvent));
}
Spliterator<JsonValue> spliterator =
new Spliterators.AbstractSpliterator<JsonValue>(Long.MAX_VALUE, Spliterator.ORDERED) {
@Override
public Spliterator<JsonValue> trySplit() {
return null;
}

@Override
public boolean tryAdvance(Consumer<? super JsonValue> action) {
if (action == null) {
throw new NullPointerException();
}
if (!hasNext()) {
return false;
}
if (next() == JsonParser.Event.END_ARRAY) {
return false;
}
action.accept(getValue());
return true;
}
};
return StreamSupport.stream(spliterator, false);
}

@Override
public Stream<Map.Entry<String, JsonValue>> getObjectStream() {
return streamCreator.getObjectStream();
if (currentEvent != Event.START_OBJECT) {
throw new IllegalStateException(
JsonMessages.PARSER_GETOBJECT_ERR(currentEvent));
}
Spliterator<Map.Entry<String, JsonValue>> spliterator =
new Spliterators.AbstractSpliterator<Map.Entry<String, JsonValue>>(Long.MAX_VALUE, Spliterator.ORDERED) {
@Override
public Spliterator<Map.Entry<String, JsonValue>> trySplit() {
return null;
}

@Override
public boolean tryAdvance(Consumer<? super Map.Entry<String, JsonValue>> action) {
if (action == null) {
throw new NullPointerException();
}
if (!hasNext()) {
return false;
}
JsonParser.Event e = next();
if (e == JsonParser.Event.END_OBJECT) {
return false;
}
if (e != JsonParser.Event.KEY_NAME) {
throw new JsonException(JsonMessages.INTERNAL_ERROR());
}
String key = getString();
if (!hasNext()) {
throw new JsonException(JsonMessages.INTERNAL_ERROR());
}
next();
JsonValue value = getValue();
action.accept(new AbstractMap.SimpleImmutableEntry<>(key, value));
return true;
}
};
return StreamSupport.stream(spliterator, false);
}

@Override
public Stream<JsonValue> getValueStream() {
return streamCreator.getValueStream();
if (! (currentContext instanceof NoneContext)) {
throw new IllegalStateException(
JsonMessages.PARSER_GETVALUESTREAM_ERR());
}
Spliterator<JsonValue> spliterator =
new Spliterators.AbstractSpliterator<JsonValue>(Long.MAX_VALUE, Spliterator.ORDERED) {
@Override
public Spliterator<JsonValue> trySplit() {
return null;
}

@Override
public boolean tryAdvance(Consumer<? super JsonValue> action) {
if (action == null) {
throw new NullPointerException();
}
if (!hasNext()) {
return false;
}
next();
action.accept(getValue());
return true;
}
};
return StreamSupport.stream(spliterator, false);
}

@Override
Expand Down
108 changes: 0 additions & 108 deletions impl/src/main/java/org/eclipse/parsson/JsonParserStreamCreator.java

This file was deleted.

Loading

0 comments on commit f2b0547

Please sign in to comment.