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

io.circe.ArrayEncoder.scala Maven / Gradle / Ivy

There is a newer version: 0.9.3
Show newest version
package io.circe

/**
 * A type class that provides a conversion from a value of type `A` to a JSON
 * array.
 *
 * @author Travis Brown
 */
trait ArrayEncoder[A] extends RootEncoder[A] { self =>
  final def apply(a: A): Json = Json.JArray(encodeArray(a))

  /**
   * Convert a value to a JSON array.
   */
  def encodeArray(a: A): List[Json]

  /**
   * Create a new [[ArrayEncoder]] by applying a function to the output of this
   * one.
   */
  final def mapJsonArray(f: List[Json] => List[Json]): ArrayEncoder[A] = new ArrayEncoder[A] {
    final def encodeArray(a: A): List[Json] = f(self.encodeArray(a))
  }
}

final object ArrayEncoder {
  /**
   * Return an instance for a given type.
   *
   * @group Utilities
   */
  final def apply[A](implicit instance: ArrayEncoder[A]): ArrayEncoder[A] = instance

  /**
   * Construct an instance from a function.
   *
   * @group Utilities
   */
  final def instance[A](f: A => List[Json]): ArrayEncoder[A] = new ArrayEncoder[A] {
    final def encodeArray(a: A): List[Json] = f(a)
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy