JsonDecoder

com.worxbend.codeberg4s.codec.JsonDecoder
See theJsonDecoder companion trait
object JsonDecoder

Attributes

Companion
trait
Graph
Supertypes
class Object
trait Matchable
class Any
Self type

Members list

Value members

Concrete methods

def all[A](values: Vector[JsonValue])(using element: JsonDecoder[A]): Either[DecodeFailure, Vector[A]]

Decodes every element of an already-extracted array, failing on the first element that will not decode.

Decodes every element of an already-extracted array, failing on the first element that will not decode.

Failing rather than dropping is deliberate: a listing that silently discarded a malformed element would under-report, and a caller cannot tell an under-report from a short page.

Attributes

def apply[A](using decoder: JsonDecoder[A]): JsonDecoder[A]

Summons the decoder for A.

Summons the decoder for A.

Attributes

def arrayOf[A](element: JsonDecoder[A]): JsonDecoder[Vector[A]]

A decoder for a top-level array of objects, which is what most Forgejo list endpoints return.

A decoder for a top-level array of objects, which is what most Forgejo list endpoints return.

An element that is not an object fails the whole page, naming its position — a listing that silently dropped a malformed element would under-report, which is worse than failing.

Attributes

def objectOf[A](build: JsonFields => A): JsonDecoder[A]

A decoder for a type assembled from the fields of one JSON object.

A decoder for a type assembled from the fields of one JSON object.

build must be total — every accessor on JsonFields answers rather than fails, so it can be. The one thing this decoder rejects is a document that is not an object at all: an array where an object was expected, or the bare literal null. That is a structural mismatch rather than a missing field, and it is worth failing on, because it means the endpoint returned something other than what it documents.

Value parameters

build

assembles the value from the object's fields

Attributes

def objectOfEither[A](build: JsonFields => Either[DecodeFailure, A]): JsonDecoder[A]

As objectOf, for a build step that can itself fail — an envelope whose elements are decoded, say.

As objectOf, for a build step that can itself fail — an envelope whose elements are decoded, say.

Attributes

Givens

Givens

given boolean: JsonDecoder[Boolean]

A JSON boolean.

A JSON boolean.

Attributes

given fields: JsonDecoder[Map[String, JsonValue]]

A JSON object, as a map from field name to value, for a caller decoding a payload whose keys are data rather than a schema — an EditorConfig, say.

A JSON object, as a map from field name to value, for a caller decoding a payload whose keys are data rather than a schema — an EditorConfig, say.

This is the one place in the library that pays for a map. JsonFields reads named fields straight out of the vector the parser built; a caller who does not know the names has to enumerate them, and a map is the shape that caller wants. Anything with a fixed set of fields should use JsonFields.reader instead.

Attributes

A decoder that hands the parsed document over untouched, for the few payloads whose shape is not fixed.

A decoder that hands the parsed document over untouched, for the few payloads whose shape is not fixed.

Attributes

given long: JsonDecoder[Long]

A JSON number, truncated toward zero.

A JSON number, truncated toward zero.

The two number cases are named rather than matched through JsonValue.Num, whose extractor would build a BigDecimal for the JsonValue.Int64 case that this decoder would then throw away — which is the cost the two cases exist to avoid.

Attributes

given string: JsonDecoder[String]

A JSON string.

A JSON string.

Strict about the JSON kind, unlike the previous library, which coerced a number into a string so that {"name": 7} decoded as "7". Nothing in this library wanted that, and a silent coercion at the boundary is how a wrong field reaches the domain looking right.

Attributes

given vector: [A] => JsonDecoder[A] => JsonDecoder[Vector[A]]

Every Forgejo list endpoint answers a top-level array, so a decoder for one element gives a decoder for a page.

Every Forgejo list endpoint answers a top-level array, so a decoder for one element gives a decoder for a page.

A derivation-based library supplies this for free; here it is one line, and it is the line that keeps every Json.decoder[Vector[SomeDto]] call site working without each listing declaring its own.

Attributes