Skip to content

DecodeJSON[A: Any val]

[Source]

Parse and decode an HTTP response body as a typed domain object in one step.

Combines ResponseJSON (JSON parsing) with a JSONDecoder (structural decoding) into a single call. The three-way return type distinguishes between success, parse failure, and decode failure:

  • A — the response body was valid JSON and matched the decoder's expected structure
  • JsonParseError — the response body was not valid JSON syntax
  • JSONDecodeError — the JSON was valid but didn't match the decoder's expected structure (missing fields, wrong types, etc.)
use "courier"
use json = "json"

// In on_response_complete():
match DecodeJSON[User](response, UserDecoder)
| let user: User =>
  env.out.print("Hello, " + user.name)
| let err: json.JsonParseError =>
  env.out.print("Invalid JSON: " + err.string())
| let err: JSONDecodeError =>
  env.out.print("Unexpected structure: " + err.string())
end
primitive val DecodeJSON[A: Any val]

Constructors

create

[Source]

new val create()
: DecodeJSON[A] val^

Returns


Public Functions

apply

[Source]

Parse response.body as JSON, then decode with decoder.

Returns JsonParseError if the body isn't valid JSON, JSONDecodeError if the JSON doesn't match the decoder's expected structure, or the decoded value on success.

fun box apply(
  response: HTTPResponse val,
  decoder: JSONDecoder[A] val)
: (A | JsonParseError val | JSONDecodeError val)

Parameters

Returns


eq

[Source]

fun box eq(
  that: DecodeJSON[A] val)
: Bool val

Parameters

Returns


ne

[Source]

fun box ne(
  that: DecodeJSON[A] val)
: Bool val

Parameters

Returns