Skip to content

JSONDecoder[A: Any val]

[Source]

Interface for converting a parsed JsonValue into a typed domain object.

Implement this interface to define how a specific JSON structure maps to your application type. Return JSONDecodeError when the JSON doesn't match the expected structure.

use "courier"
use json = "json"

class val User
  let name: String
  let age: I64

  new val create(name': String, age': I64) =>
    name = name'
    age = age'

primitive UserDecoder is JSONDecoder[User]
  fun apply(value: json.JsonValue): (User | JSONDecodeError) =>
    let nav = json.JsonNav(value)
    try
      User(nav("name").as_string()?, nav("age").as_i64()?)
    else
      JSONDecodeError("expected object with string 'name' and integer 'age'")
    end
interface val JSONDecoder[A: Any val]

Public Functions

apply

[Source]

Decode a parsed JSON value into a domain object, or return an error if the JSON structure doesn't match.

fun box apply(
  value: (JsonObject val | JsonArray val | String val | 
    I64 val | F64 val | Bool val | 
    None val))
: (A | JSONDecodeError val)

Parameters

Returns