(.using [library [lux (.except) ["[0]" debug] [abstract [monad (.only do)] ["[0]" codec]] [control ["[0]" try] ["<>" parser (.only) ["" json] ["<[0]>" type] ["<[0]>" code]]] [data ["[0]" text (.only) ["%" format (.only format)]] [collection ["[0]" list ("[1]#[0]" monad)] ["[0]" sequence (.only sequence)] ["[0]" dictionary]]] [macro [syntax (.only syntax:)] ["[0]" code]] [math [number ["n" nat ("[1]#[0]" decimal)] ["[0]" i64] ["[0]" int] ["[0]" frac]]] [time ... ["[0]" instant] ... ["[0]" duration] ["[0]" date] ["[0]" day] ["[0]" month]] ["[0]" type (.only) ["[0]" unit] ["[0]" poly (.only poly:)]]]] [\\library ["[0]" / (.only JSON)]]) (def: tag (-> Nat Frac) (|>> .int int.frac)) (def: (rec_encoded non_rec) (All (_ a) (-> (-> (-> a JSON) (-> a JSON)) (-> a JSON))) (function (_ input) (non_rec (rec_encoded non_rec) input))) (def: low_mask Nat (|> 1 (i64.left_shifted 32) --)) (def: high_mask Nat (|> low_mask (i64.left_shifted 32))) (implementation: nat_codec (codec.Codec JSON Nat) (def: (encoded input) (let [high (|> input (i64.and high_mask) (i64.right_shifted 32)) low (i64.and low_mask input)] {/.#Array (sequence (|> high .int int.frac {/.#Number}) (|> low .int int.frac {/.#Number}))})) (def: decoded (.result (.array (do <>.monad [high .number low .number] (in (n.+ (|> high frac.int .nat (i64.left_shifted 32)) (|> low frac.int .nat)))))))) (implementation: int_codec (codec.Codec JSON Int) (def: encoded (|>> .nat (# nat_codec encoded))) (def: decoded (|>> (# nat_codec decoded) (# try.functor each (|>> .int))))) ... Builds a JSON generator for potentially inexistent values. (def: (nullable writer) (All (_ a) (-> (-> a JSON) (-> (Maybe a) JSON))) (function (_ elem) (case elem {.#None} {/.#Null} {.#Some value} (writer value)))) (implementation: qty_codec (All (_ unit) (codec.Codec JSON (unit.Qty unit))) (def: encoded (|>> ((debug.private unit.out')) (# ..int_codec encoded))) (def: decoded (|>> (# ..int_codec decoded) (# try.functor each (debug.private unit.in'))))) (poly: encoded (with_expansions [ (template [ ] [(do ! [.let [g!_ (code.local "_______")] _ ] (in (` (is (~ (@JSON#encoded inputT)) ))))] [(.exactly Any) (function ((~ g!_) (~ (code.symbol ["" "0"]))) {/.#Null})] [(.sub Bit) (|>> {/.#Boolean})] [(.sub Nat) (# (~! ..nat_codec) (~' encoded))] [(.sub Int) (# (~! ..int_codec) (~' encoded))] [(.sub Frac) (|>> {/.#Number})] [(.sub Text) (|>> {/.#String})])