Skip to content

Commit

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

Signed-off-by: Anton Pinsky <anton.pinsky@ionos.com>
  • Loading branch information
api-from-the-ion authored and lukasj committed Feb 26, 2024
1 parent a38b7d1 commit 146a2f7
Show file tree
Hide file tree
Showing 5 changed files with 986 additions and 159 deletions.
109 changes: 11 additions & 98 deletions impl/src/main/java/org/eclipse/parsson/JsonParserImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,10 @@
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.stream.Stream;
import java.util.stream.StreamSupport;

import jakarta.json.JsonArray;
import jakarta.json.JsonArrayBuilder;
Expand Down Expand Up @@ -60,7 +55,7 @@
* @author Jitendra Kotamraju
* @author Kin-man Chung
*/
public class JsonParserImpl implements JsonParser {
class JsonParserImpl implements JsonParser {

private Context currentContext = new NoneContext();
private Event currentEvent;
Expand All @@ -71,20 +66,24 @@ public class JsonParserImpl implements JsonParser {

private final JsonContext jsonContext;

public JsonParserImpl(Reader reader, JsonContext jsonContext) {
private final JsonParserStreamCreator streamCreator = new JsonParserStreamCreator(this, true, () -> currentEvent,
() -> currentContext instanceof NoneContext);


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

public JsonParserImpl(InputStream in, JsonContext jsonContext) {
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);
}

public JsonParserImpl(InputStream in, Charset encoding, JsonContext jsonContext) {
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 @@ -194,103 +193,17 @@ public JsonValue getValue() {

@Override
public Stream<JsonValue> 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);
return streamCreator.getArrayStream();
}

@Override
public Stream<Map.Entry<String, JsonValue>> 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);
return streamCreator.getObjectStream();
}

@Override
public Stream<JsonValue> 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);
return streamCreator.getValueStream();
}

@Override
Expand Down
108 changes: 108 additions & 0 deletions impl/src/main/java/org/eclipse/parsson/JsonParserStreamCreator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.eclipse.parsson;

import java.util.AbstractMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.Supplier;
import java.util.stream.Stream;

import jakarta.json.JsonException;
import jakarta.json.JsonValue;
import jakarta.json.stream.JsonParser;
import jakarta.json.stream.JsonParser.Event;

class JsonParserStreamCreator {

private final JsonParser parser;
private final boolean nextBeforeCreationOfValueStream;
private final Supplier<Event> currenEventSupplier;
private final Supplier<Boolean> canProduceValueStream;

JsonParserStreamCreator(JsonParser parser, boolean nextBeforeCreationOfValueStream, Supplier<Event> currenEventSupplier,
Supplier<Boolean> canProduceValueStream) {

this.parser = Objects.requireNonNull(parser);
this.nextBeforeCreationOfValueStream = nextBeforeCreationOfValueStream;
this.currenEventSupplier = Objects.requireNonNull(currenEventSupplier);
this.canProduceValueStream = Objects.requireNonNull(canProduceValueStream);
}

/**
* Creates new {@link Stream} from values from {@link Supplier}. The stream delivers the values as long as supplier delivers non-null values
*
* @param supplier supplier of the values
* @param <T> type of the values which are delivered by the supplier and the stream
* @return stream of values from given supplier
*/
private static <T> Stream<T> streamFromSupplier(Supplier<T> supplier) {
return StreamCreator.iterate(Objects.requireNonNull(supplier).get(), Objects::nonNull, value -> supplier.get());
}

public Stream<JsonValue> getArrayStream() {
if (currenEventSupplier.get() == Event.START_ARRAY) {
return streamFromSupplier(() -> (parser.hasNext() && parser.next() != Event.END_ARRAY) ? parser.getValue() : null);
} else {
throw new IllegalStateException(JsonMessages.PARSER_GETARRAY_ERR(parser.currentEvent()));
}
}

public Stream<Map.Entry<String, JsonValue>> getObjectStream() {
if (currenEventSupplier.get() == Event.START_OBJECT) {
return streamFromSupplier(() -> {
if (!parser.hasNext()) {
return null;
}
Event e = parser.next();
if (e == Event.END_OBJECT) {
return null;
} else if (e != Event.KEY_NAME) {
throw new JsonException(JsonMessages.INTERNAL_ERROR());
} else {
String key = parser.getString();
if (!parser.hasNext()) {
throw new JsonException(JsonMessages.INTERNAL_ERROR());
} else {
parser.next();
return new AbstractMap.SimpleImmutableEntry<>(key, parser.getValue());
}
}
});
} else {
throw new IllegalStateException(JsonMessages.PARSER_GETOBJECT_ERR(parser.currentEvent()));
}
}

public Stream<JsonValue> getValueStream() {
if (canProduceValueStream.get()) {
if (nextBeforeCreationOfValueStream) {
parser.next();
}

return streamFromSupplier(() -> {
if (parser.hasNext()) {
return parser.getValue();
} else {
return null;
}
});
} else {
throw new IllegalStateException(JsonMessages.PARSER_GETVALUESTREAM_ERR());
}
}
}
Loading

0 comments on commit 146a2f7

Please sign in to comment.