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 encode_tuple2(
tuple tuple: #(a, b),
first encode1: fn(a) -> Json,
second encode2: fn(b) -> Json,
) -> Json
pub fn encode_tuple3(
tuple tuple: #(a, b, c),
first encode1: fn(a) -> Json,
second encode2: fn(b) -> Json,
third encode3: fn(c) -> Json,
) -> Json
pub fn encode_tuple4(
tuple tuple: #(a, b, c, d),
first encode1: fn(a) -> Json,
second encode2: fn(b) -> Json,
third encode3: fn(c) -> Json,
fourth encode4: fn(d) -> Json,
) -> Json
pub fn encode_tuple5(
tuple tuple: #(a, b, c, d, e),
first encode1: fn(a) -> Json,
second encode2: fn(b) -> Json,
third encode3: fn(c) -> Json,
fourth encode4: fn(d) -> Json,
fifth encode5: fn(e) -> Json,
) -> Json
pub fn encode_tuple6(
tuple tuple: #(a, b, c, d, e, f),
first encode1: fn(a) -> Json,
second encode2: fn(b) -> Json,
third encode3: fn(c) -> Json,
fourth encode4: fn(d) -> Json,
fifth encode5: fn(e) -> Json,
sixth encode6: fn(f) -> Json,
) -> Json
pub fn encode_tuple7(
tuple tuple: #(a, b, c, d, e, f, g),
first encode1: fn(a) -> Json,
second encode2: fn(b) -> Json,
third encode3: fn(c) -> Json,
fourth encode4: fn(d) -> Json,
fifth encode5: fn(e) -> Json,
sixth encode6: fn(f) -> Json,
seventh encode7: fn(g) -> Json,
) -> Json
pub fn encode_tuple8(
tuple tuple: #(a, b, c, d, e, f, g, h),
first encode1: fn(a) -> Json,
second encode2: fn(b) -> Json,
third encode3: fn(c) -> Json,
fourth encode4: fn(d) -> Json,
fifth encode5: fn(e) -> Json,
sixth encode6: fn(f) -> Json,
seventh encode7: fn(g) -> Json,
eighth encode8: fn(h) -> Json,
) -> Json
pub fn encode_tuple9(
tuple tuple: #(a, b, c, d, e, f, g, h, i),
first encode1: fn(a) -> Json,
second encode2: fn(b) -> Json,
third encode3: fn(c) -> Json,
fourth encode4: fn(d) -> Json,
fifth encode5: fn(e) -> Json,
sixth encode6: fn(f) -> Json,
seventh encode7: fn(g) -> Json,
eighth encode8: fn(h) -> Json,
ninth encode9: fn(i) -> Json,
) -> Json
pub fn enum_type_decoder(
constructor_decoders decoders: List(#(String, a)),
) -> #(
fn(Dynamic) -> Result(a, List(DecodeError)),
SchemaDefinition,
)
Function to define a decoder for enum types (unions where constructors have no arguments). The function takes a list of tuples containing the string representation and the corresponding enum value.
Make sure to add tests for every possible enum value because it is not possible to check for exhaustiveness.
Example
type Color {
Red
Green
Blue
}
let color_decoder = enum_type_decoder([
#("red", Red),
#("green", Green),
#("blue", Blue),
])
pub fn enum_type_encoder(
value of: a,
encoder_fn encoder_fn: fn(a) -> String,
) -> Json
Function to encode an enum type (unions where constructors have no arguments) into a JSON object. The function takes a value and an encoder function that returns the string representation of the enum value.
Make sure to update the decoder function accordingly.
Example
type Color {
Red
Green
Blue
}
let color_encoder = enum_type_encoder(fn(color) {
case color {
Red -> "red"
Green -> "green"
Blue -> "blue"
}
})
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 map(
decoder decoder: #(
fn(Dynamic) -> Result(a, List(DecodeError)),
SchemaDefinition,
),
over foo: fn(a) -> b,
) -> #(
fn(Dynamic) -> Result(b, 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),
)
Decode a Option
value where the underlaying JSON field can be missing or have null
value
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))
])
)
}
})