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

io.setl.json.parser.ParserFactory 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 static java.nio.charset.StandardCharsets.UTF_8;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;
import jakarta.json.JsonArray;
import jakarta.json.JsonObject;
import jakarta.json.stream.JsonParser;
import jakarta.json.stream.JsonParserFactory;

/**
 * Factory for generating parsers.
 *
 * @author Simon Greatrix on 13/01/2020.
 */
public class ParserFactory implements JsonParserFactory {

  /** Should the parser expect a single root value in a stream, or multiple ones?. By default, the parser expects a single root. */
  public static final String REQUIRE_SINGLE_ROOT = "setl.json.parser.requireSingleRoot";

  private boolean singleRoot = true;


  /**
   * New instance.
   *
   * @param config Configuration - may be null or specify if a single root is required (which defaults to true).
   */
  public ParserFactory(Map config) {
    if (config != null && config.containsKey(REQUIRE_SINGLE_ROOT)) {
      singleRoot = Boolean.parseBoolean(String.valueOf(config.get(REQUIRE_SINGLE_ROOT)));
    }
  }


  @Override
  public JsonParser createParser(Reader reader) {
    Parser parser = new Parser(reader);
    parser.setRequireSingleRoot(singleRoot);
    return parser;
  }


  @Override
  public JsonParser createParser(InputStream in) {
    return createParser(in, UTF_8);
  }


  @Override
  public JsonParser createParser(InputStream in, Charset charset) {
    return createParser(new InputStreamReader(in, charset));
  }


  @Override
  public JsonParser createParser(JsonObject obj) {
    return new StructureParser(obj);
  }


  @Override
  public JsonParser createParser(JsonArray array) {
    return new StructureParser(array);
  }


  @Override
  public Map getConfigInUse() {
    TreeMap map = new TreeMap<>();
    map.put(REQUIRE_SINGLE_ROOT, singleRoot);
    return Collections.unmodifiableSortedMap(map);
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy