json/blueprint
Types
pub type FieldDecoder(t) =
#(dynamic.Decoder(t), #(String, SchemaDefinition))
Functions
pub fn decode(
using decoder: #(
fn(Dynamic) -> Result(a, List(DecodeError)),
SchemaDefinition,
),
from json_string: String,
) -> Result(a, gleam/json.DecodeError)
pub fn decode0(
constructor: a,
) -> #(
fn(Dynamic) -> Result(a, List(DecodeError)),
SchemaDefinition,
)
pub fn decode1(
constructor: fn(a) -> b,
t1: #(
fn(Dynamic) -> Result(a, List(DecodeError)),
#(String, SchemaDefinition),
),
) -> #(
fn(Dynamic) -> Result(b, List(DecodeError)),
SchemaDefinition,
)
pub fn decode2(
constructor: fn(a, b) -> c,
t1: #(
fn(Dynamic) -> Result(a, List(DecodeError)),
#(String, SchemaDefinition),
),
t2: #(
fn(Dynamic) -> Result(b, List(DecodeError)),
#(String, SchemaDefinition),
),
) -> #(
fn(Dynamic) -> Result(c, List(DecodeError)),
SchemaDefinition,
)
pub fn decode3(
constructor: fn(a, b, c) -> d,
t1: #(
fn(Dynamic) -> Result(a, List(DecodeError)),
#(String, SchemaDefinition),
),
t2: #(
fn(Dynamic) -> Result(b, List(DecodeError)),
#(String, SchemaDefinition),
),
t3: #(
fn(Dynamic) -> Result(c, List(DecodeError)),
#(String, SchemaDefinition),
),
) -> #(
fn(Dynamic) -> Result(d, List(DecodeError)),
SchemaDefinition,
)
pub fn decode4(
constructor: fn(a, b, c, d) -> e,
t1: #(
fn(Dynamic) -> Result(a, List(DecodeError)),
#(String, SchemaDefinition),
),
t2: #(
fn(Dynamic) -> Result(b, List(DecodeError)),
#(String, SchemaDefinition),
),
t3: #(
fn(Dynamic) -> Result(c, List(DecodeError)),
#(String, SchemaDefinition),
),
t4: #(
fn(Dynamic) -> Result(d, List(DecodeError)),
#(String, SchemaDefinition),
),
) -> #(
fn(Dynamic) -> Result(e, List(DecodeError)),
SchemaDefinition,
)
pub fn decode5(
constructor: fn(a, b, c, d, e) -> f,
t1: #(
fn(Dynamic) -> Result(a, List(DecodeError)),
#(String, SchemaDefinition),
),
t2: #(
fn(Dynamic) -> Result(b, List(DecodeError)),
#(String, SchemaDefinition),
),
t3: #(
fn(Dynamic) -> Result(c, List(DecodeError)),
#(String, SchemaDefinition),
),
t4: #(
fn(Dynamic) -> Result(d, List(DecodeError)),
#(String, SchemaDefinition),
),
t5: #(
fn(Dynamic) -> Result(e, List(DecodeError)),
#(String, SchemaDefinition),
),
) -> #(
fn(Dynamic) -> Result(f, List(DecodeError)),
SchemaDefinition,
)
pub fn decode6(
constructor: fn(a, b, c, d, e, f) -> g,
t1: #(
fn(Dynamic) -> Result(a, List(DecodeError)),
#(String, SchemaDefinition),
),
t2: #(
fn(Dynamic) -> Result(b, List(DecodeError)),
#(String, SchemaDefinition),
),
t3: #(
fn(Dynamic) -> Result(c, List(DecodeError)),
#(String, SchemaDefinition),
),
t4: #(
fn(Dynamic) -> Result(d, List(DecodeError)),
#(String, SchemaDefinition),
),
t5: #(
fn(Dynamic) -> Result(e, List(DecodeError)),
#(String, SchemaDefinition),
),
t6: #(
fn(Dynamic) -> Result(f, List(DecodeError)),
#(String, SchemaDefinition),
),
) -> #(
fn(Dynamic) -> Result(g, List(DecodeError)),
SchemaDefinition,
)
pub fn decode7(
constructor: fn(a, b, c, d, e, f, g) -> h,
t1: #(
fn(Dynamic) -> Result(a, List(DecodeError)),
#(String, SchemaDefinition),
),
t2: #(
fn(Dynamic) -> Result(b, List(DecodeError)),
#(String, SchemaDefinition),
),
t3: #(
fn(Dynamic) -> Result(c, List(DecodeError)),
#(String, SchemaDefinition),
),
t4: #(
fn(Dynamic) -> Result(d, List(DecodeError)),
#(String, SchemaDefinition),
),
t5: #(
fn(Dynamic) -> Result(e, List(DecodeError)),
#(String, SchemaDefinition),
),
t6: #(
fn(Dynamic) -> Result(f, List(DecodeError)),
#(String, SchemaDefinition),
),
t7: #(
fn(Dynamic) -> Result(g, List(DecodeError)),
#(String, SchemaDefinition),
),
) -> #(
fn(Dynamic) -> Result(h, List(DecodeError)),
SchemaDefinition,
)
pub fn decode8(
constructor: fn(a, b, c, d, e, f, g, h) -> i,
t1: #(
fn(Dynamic) -> Result(a, List(DecodeError)),
#(String, SchemaDefinition),
),
t2: #(
fn(Dynamic) -> Result(b, List(DecodeError)),
#(String, SchemaDefinition),
),
t3: #(
fn(Dynamic) -> Result(c, List(DecodeError)),
#(String, SchemaDefinition),
),
t4: #(
fn(Dynamic) -> Result(d, List(DecodeError)),
#(String, SchemaDefinition),
),
t5: #(
fn(Dynamic) -> Result(e, List(DecodeError)),
#(String, SchemaDefinition),
),
t6: #(
fn(Dynamic) -> Result(f, List(DecodeError)),
#(String, SchemaDefinition),
),
t7: #(
fn(Dynamic) -> Result(g, List(DecodeError)),
#(String, SchemaDefinition),
),
t8: #(
fn(Dynamic) -> Result(h, List(DecodeError)),
#(String, SchemaDefinition),
),
) -> #(
fn(Dynamic) -> Result(i, List(DecodeError)),
SchemaDefinition,
)
pub fn decode9(
constructor: fn(a, b, c, d, e, f, g, h, i) -> j,
t1: #(
fn(Dynamic) -> Result(a, List(DecodeError)),
#(String, SchemaDefinition),
),
t2: #(
fn(Dynamic) -> Result(b, List(DecodeError)),
#(String, SchemaDefinition),
),
t3: #(
fn(Dynamic) -> Result(c, List(DecodeError)),
#(String, SchemaDefinition),
),
t4: #(
fn(Dynamic) -> Result(d, List(DecodeError)),
#(String, SchemaDefinition),
),
t5: #(
fn(Dynamic) -> Result(e, List(DecodeError)),
#(String, SchemaDefinition),
),
t6: #(
fn(Dynamic) -> Result(f, List(DecodeError)),
#(String, SchemaDefinition),
),
t7: #(
fn(Dynamic) -> Result(g, List(DecodeError)),
#(String, SchemaDefinition),
),
t8: #(
fn(Dynamic) -> Result(h, List(DecodeError)),
#(String, SchemaDefinition),
),
t9: #(
fn(Dynamic) -> Result(i, List(DecodeError)),
#(String, SchemaDefinition),
),
) -> #(
fn(Dynamic) -> Result(j, List(DecodeError)),
SchemaDefinition,
)
pub fn field(
named name: String,
of inner_type: #(
fn(Dynamic) -> Result(a, List(DecodeError)),
SchemaDefinition,
),
) -> #(
fn(Dynamic) -> Result(a, List(DecodeError)),
#(String, SchemaDefinition),
)
pub fn generate_json_schema(
decoder: #(
fn(Dynamic) -> Result(a, List(DecodeError)),
SchemaDefinition,
),
) -> Json
pub fn get_dynamic_decoder(
decoder: #(
fn(Dynamic) -> Result(a, List(DecodeError)),
SchemaDefinition,
),
) -> fn(Dynamic) -> Result(a, List(DecodeError))
pub fn list(
of decoder_type: #(
fn(Dynamic) -> Result(a, List(DecodeError)),
SchemaDefinition,
),
) -> #(
fn(Dynamic) -> Result(List(a), List(DecodeError)),
SchemaDefinition,
)
pub fn optional(
of decode: #(
fn(Dynamic) -> Result(a, List(DecodeError)),
SchemaDefinition,
),
) -> #(
fn(Dynamic) -> Result(Option(a), List(DecodeError)),
SchemaDefinition,
)
pub fn optional_field(
named name: String,
of inner_type: #(
fn(Dynamic) -> Result(a, List(DecodeError)),
SchemaDefinition,
),
) -> #(
fn(Dynamic) -> Result(Option(a), List(DecodeError)),
#(String, SchemaDefinition),
)
pub fn string() -> #(
fn(Dynamic) -> Result(String, List(DecodeError)),
SchemaDefinition,
)
pub fn tuple2(
first decode1: #(
fn(Dynamic) -> Result(a, List(DecodeError)),
SchemaDefinition,
),
second decode2: #(
fn(Dynamic) -> Result(b, List(DecodeError)),
SchemaDefinition,
),
) -> #(
fn(Dynamic) -> Result(#(a, b), List(DecodeError)),
SchemaDefinition,
)
pub fn tuple3(
first decode1: #(
fn(Dynamic) -> Result(a, List(DecodeError)),
SchemaDefinition,
),
second decode2: #(
fn(Dynamic) -> Result(b, List(DecodeError)),
SchemaDefinition,
),
third decode3: #(
fn(Dynamic) -> Result(c, List(DecodeError)),
SchemaDefinition,
),
) -> #(
fn(Dynamic) -> Result(#(a, b, c), List(DecodeError)),
SchemaDefinition,
)
pub fn tuple4(
first decode1: #(
fn(Dynamic) -> Result(a, List(DecodeError)),
SchemaDefinition,
),
second decode2: #(
fn(Dynamic) -> Result(b, List(DecodeError)),
SchemaDefinition,
),
third decode3: #(
fn(Dynamic) -> Result(c, List(DecodeError)),
SchemaDefinition,
),
fourth decode4: #(
fn(Dynamic) -> Result(d, List(DecodeError)),
SchemaDefinition,
),
) -> #(
fn(Dynamic) -> Result(#(a, b, c, d), List(DecodeError)),
SchemaDefinition,
)
pub fn tuple5(
first decode1: #(
fn(Dynamic) -> Result(a, List(DecodeError)),
SchemaDefinition,
),
second decode2: #(
fn(Dynamic) -> Result(b, List(DecodeError)),
SchemaDefinition,
),
third decode3: #(
fn(Dynamic) -> Result(c, List(DecodeError)),
SchemaDefinition,
),
fourth decode4: #(
fn(Dynamic) -> Result(d, List(DecodeError)),
SchemaDefinition,
),
fifth decode5: #(
fn(Dynamic) -> Result(e, List(DecodeError)),
SchemaDefinition,
),
) -> #(
fn(Dynamic) -> Result(#(a, b, c, d, e), List(DecodeError)),
SchemaDefinition,
)
pub fn tuple6(
first decode1: #(
fn(Dynamic) -> Result(a, List(DecodeError)),
SchemaDefinition,
),
second decode2: #(
fn(Dynamic) -> Result(b, List(DecodeError)),
SchemaDefinition,
),
third decode3: #(
fn(Dynamic) -> Result(c, List(DecodeError)),
SchemaDefinition,
),
fourth decode4: #(
fn(Dynamic) -> Result(d, List(DecodeError)),
SchemaDefinition,
),
fifth decode5: #(
fn(Dynamic) -> Result(e, List(DecodeError)),
SchemaDefinition,
),
sixth decode6: #(
fn(Dynamic) -> Result(f, List(DecodeError)),
SchemaDefinition,
),
) -> #(
fn(Dynamic) -> Result(#(a, b, c, d, e, f), List(DecodeError)),
SchemaDefinition,
)
pub fn union_type_decoder(
constructor_decoders decoders: List(
#(
String,
#(
fn(Dynamic) -> Result(a, List(DecodeError)),
SchemaDefinition,
),
),
),
) -> #(
fn(Dynamic) -> Result(a, List(DecodeError)),
SchemaDefinition,
)
Function to defined a decoder for a union types. The function takes a list of decoders for each possible type of the union.
Make sure to add tests for every possible type of the union because it is not possible to check for exhaustiveness in the case.
Example
type Shape {
Circle(Float)
Rectangle(Float, Float)
}
let shape_decoder = union_type_decoder([
#("circle", decode1(Circle, field("radius", float()))),
#("rectangle", decode2(Rectangle,
field("width", float()),
field("height", float())
))
])
pub fn union_type_encoder(
value of: a,
encoder_fn encoder_fn: fn(a) -> #(String, Json),
) -> Json
Function to encode a union type into a JSON object. The function takes a value and an encoder function that returns a tuple of the type name and the JSON value.
Make sure to update the decoder function accordingly.
Example
type Shape {
Circle(Float)
Rectangle(Float, Float)
}
let shape_encoder = union_type_encoder(fn(shape) {
case shape {
Circle(radius) -> #("circle", json.object([#("radius", json.float(radius))]))
Rectangle(width, height) -> #(
"rectangle",
json.object([
#("width", json.float(width)),
#("height", json.float(height))
])
)
}
})