flix

0.79.0

FromJson

Trait for types that can be decoded from a Json value, and its functions.

trait FromJson[a: Type]Source

Trait for types that can be decoded from a Json value.

Instances

Primitives
Unit, Bool, Float32, Float64, BigDecimal, Int8, Int16, Int32, Int64, BigInt, String, Vector[a] and tuples of 2 to 10 elements
Non-primitives
Json, List[a], Map[k, v], Option[a], Set[a]

Signatures

def fromJson(j: Json): Result[JsonError, a] with FromJson[a]§ Source

Decodes the JSON value j into a value of type a, or returns a JsonError.

Module Definitions

def decodeAtIndex(i: Int32, j: Json): Result[JsonError, a] with FromJson[a]§ Source

Looks up index i in j, which must be a JArray, and decodes the element via FromJson[a].

If i is out of range, errors with MissingIndex(i) at the empty path. If the element is present but fails to decode, the inner error's path is prepended with Index(i).

def decodeAtKey(k: String, j: Json): Result[JsonError, a] with FromJson[a]§ Source

Looks up k in j and decodes the value via FromJson[a].

If k is absent, errors with MissingKey(k) at the empty path. If k is present but the value fails to decode, the inner error's path is prepended with Key(k).

def decodeAtKeyOpt(k: String, j: Json): Result[JsonError, Option[a]] with FromJson[a]§ Source

Looks up k in j and, if present, decodes the value via FromJson[a].

Returns Ok(None) if k is absent, Ok(Some(v)) if present and decodable, or the decode error otherwise.

def decodeAtPath(p: JsonPath, j: Json): Result[JsonError, a] with FromJson[a]§ Source

Looks up the value at p in j and decodes it via FromJson[a].

Navigation failures follow the same path/kind convention as getAtPath. If navigation succeeds and the leaf decoder fails, the leaf's own inner path (e.g. /0 from a failing Vector element) is appended to p, giving the full root-to-failure path.

def decodeAtPathOpt(p: JsonPath, j: Json): Result[JsonError, Option[a]] with FromJson[a]§ Source

Looks up the value at p in j and, if present, decodes it via FromJson[a].

Missing-key and missing-index failures along p yield Ok(None). Type-mismatch failures (a Key step into a non-object, or an Index step into a non-array) still error, since those signal a real shape disagreement. Leaf decode failures are reported with the full p-prefixed path.

def getAtKey(k: String, j: Json): Result[JsonError, Json]§ Source

Looks up k in j, which must be a JObject.

Returns the raw Json value at k, MissingKey(k) if k is absent, or TypeMismatch if j is not a JObject. Both errors carry the empty path (the failure is at the level of j itself).

def getAtKeyOpt(k: String, j: Json): Result[JsonError, Option[Json]]§ Source

Looks up k in j, which must be a JObject.

Returns Some(v) if present, None if absent, or TypeMismatch if j is not a JObject.

def getAtPath(p: JsonPath, j: Json): Result[JsonError, Json]§ Source

Walks p in j and returns the raw Json value at the leaf.

On failure the error's path points to the prefix of p that was successfully traversed — i.e. up to but not including the failing step. The failing step is reflected in the error's kind: MissingKey(k) for an absent object key, MissingIndex(i) for an out-of-range array index, and TypeMismatch for a step that descends into the wrong shape (e.g. a Key into a non-object).