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

com.adgear.anoa.read.JsonStreams Maven / Gradle / Ivy

package com.adgear.anoa.read;

import com.adgear.anoa.Anoa;
import com.adgear.anoa.AnoaHandler;
import com.fasterxml.jackson.core.FormatSchema;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.util.TokenBuffer;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.function.Function;

/**
 * Utility class for streaming Jackson records from JSON serializations.
 */
public class JsonStreams extends JacksonStreamsBase<
    ObjectMapper,
    JsonFactory,
    FormatSchema,
    JsonParser> {

  public JsonStreams() {
    super(new ObjectMapper());
  }

  public JsonParser parser(TokenBuffer tokenBuffer) {
    return tokenBuffer.asParser(objectCodec);
  }

  /**
   * @return Object-mapping function appliable to TokenBuffer instances
   */
  public Function tokenBuffer() {
    return (TokenBuffer tb) -> {
      try {
        return parser(tb).readValueAsTree();
      } catch (IOException e) {
        throw new UncheckedIOException(e);
      }
    };
  }

  /**
   * @param anoaHandler {@code AnoaHandler} instance to use for exception handling
   * @param  Metadata type
   * @return Object-mapping function appliable to TokenBuffer instances
   */
  public  /*@NonNull*/ Function, Anoa> tokenBuffer(
      /*@NonNull*/ AnoaHandler anoaHandler) {
    return anoaHandler.functionChecked((TokenBuffer tb) -> parser(tb).readValueAsTree());
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy