All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.setl.json.parser.ValueIterator Maven / Gradle / Ivy

Go to download

An implementation of the Canonical JSON format with support for javax.json and Jackson

The newest version!
package io.setl.json.parser;

import java.util.EnumSet;
import java.util.function.Supplier;
import jakarta.json.JsonValue;
import jakarta.json.stream.JsonParser;
import jakarta.json.stream.JsonParser.Event;

/**
 * Iterate over JSON values as they are parsed.
 *
 * @author Simon Greatrix on 24/01/2020.
 */
class ValueIterator extends BaseIterator {

  /** Should never see these events in the root context. */
  private static final EnumSet BAD_EVENTS = EnumSet.of(
      Event.END_ARRAY,
      Event.END_OBJECT,
      Event.KEY_NAME
  );

  private final StructureTag myTag;

  private final JsonParser parser;

  private final Supplier tags;

  private JsonValue next;


  public ValueIterator(Supplier tags, JsonParser parser) {
    this.tags = tags;
    this.parser = parser;
    myTag = tags.get();
  }


  @Override
  protected boolean checkNext() {
    if (!myTag.equals(tags.get())) {
      return false;
    }
    if (!parser.hasNext()) {
      return false;
    }
    Event event = parser.next();
    if (BAD_EVENTS.contains(event)) {
      throw new IllegalStateException("Iterator has lost synchronization with the parser. Saw event " + event);
    }
    next = parser.getValue();
    return true;
  }


  @Override
  protected JsonValue fetchNext() {
    JsonValue jsonValue = next;
    next = null;
    return jsonValue;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy