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

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

Go to download

Additional functionality complementing the anoa-core module, requiring additional upstream dependencies such as jackson-databind and various jackson dataformats.

There is a newer version: 3.1.2
Show newest version
package com.adgear.anoa.read;

import com.adgear.anoa.Anoa;
import com.adgear.anoa.AnoaHandler;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;

import java.util.function.Function;

/**
 * Utility class for generating functions for deserializing Jackson ObjectNode records. Unless
 * specified otherwise, the functions should not be deemed thread-safe.
 */
public class JacksonDecoders {

  protected JacksonDecoders() {
  }

  /**
   * @return A function which deserializes an ObjectNode from its JSON encoding
   */
  static public Function json() {
    JsonStreams streams = new JsonStreams();
    return bytes -> streams.decode(streams.parser(bytes));
  }

  /**
   * @param anoaHandler {@code AnoaHandler} instance to use for exception handling
   * @param          Metadata type
   * @return A function which deserializes an ObjectNode record from its JSON encoding
   */
  static public  Function, Anoa> json(
      AnoaHandler anoaHandler) {
    JsonStreams streams = new JsonStreams();
    return anoaHandler.functionChecked(
        bytes -> streams.decodeChecked(streams.parserChecked(bytes)));
  }

  /**
   * @return A function which deserializes an ObjectNode from its CBOR encoding
   */
  static public Function cbor() {
    CborStreams streams = new CborStreams();
    return bytes -> streams.decode(streams.parser(bytes));
  }

  /**
   * @param anoaHandler {@code AnoaHandler} instance to use for exception handling
   * @param          Metadata type
   * @return A function which deserializes an ObjectNode record from its CBOR encoding
   */
  static public  Function, Anoa> cbor(
      AnoaHandler anoaHandler) {
    CborStreams streams = new CborStreams();
    return anoaHandler.functionChecked(
        bytes -> streams.decodeChecked(streams.parserChecked(bytes)));
  }

  /**
   * @param csvSchema CSV schema specification (separator, etc.)
   * @return A function which deserializes an ObjectNode from a CSV encoding
   */
  static public Function csv(CsvSchema csvSchema) {
    CsvStreams streams = new CsvStreams(csvSchema);
    return string -> streams.decode(streams.parser(string));
  }

  /**
   * @param anoaHandler {@code AnoaHandler} instance to use for exception handling
   * @param csvSchema   CSV schema specification (separator, etc.)
   * @param          Metadata type
   * @return A function which deserializes an ObjectNode record from a CSV encoding
   */
  static public  Function, Anoa> csv(
      AnoaHandler anoaHandler,
      CsvSchema csvSchema) {
    CsvStreams streams = new CsvStreams(csvSchema);
    return anoaHandler.functionChecked(
        string -> streams.decodeChecked(streams.parserChecked(string)));
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy