| 
 | 1 | +type t<'a> = 'a => Js.Json.t  | 
 | 2 | + | 
1 | 3 | external int: int => Js.Json.t = "%identity"  | 
2 | 4 | external float: float => Js.Json.t = "%identity"  | 
3 | 5 | external bool: bool => Js.Json.t = "%identity"  | 
4 | 6 | external string: string => Js.Json.t = "%identity"  | 
5 | 7 | 
 
  | 
 | 8 | +module Composite = {  | 
 | 9 | +  external jsonDict: Js.Dict.t<Js.Json.t> => Js.Json.t = "%identity"  | 
 | 10 | +  external jsonArray: array<Js.Json.t> => Js.Json.t = "%identity"  | 
 | 11 | +  external stringArray: array<string> => Js.Json.t = "%identity"  | 
 | 12 | +  external intArray: array<int> => Js.Json.t = "%identity"  | 
 | 13 | +  external floatArray: array<float> => Js.Json.t = "%identity"  | 
 | 14 | +  external boolArray: array<bool> => Js.Json.t = "%identity"  | 
 | 15 | +}  | 
 | 16 | + | 
 | 17 | +@val external null: Js.Json.t = "null"  | 
 | 18 | + | 
 | 19 | +let array = (encode, arr) => arr->Js.Array2.map(encode)->Composite.jsonArray  | 
 | 20 | + | 
 | 21 | +let list = (encode, l) =>  | 
 | 22 | +  switch l {  | 
 | 23 | +  | list{} => Composite.jsonArray([])  | 
 | 24 | +  | list{hd, ...tl} =>  | 
 | 25 | +    let arr = Array.make(l->List.length, hd->encode)  | 
 | 26 | +    let rec fill = (i, l) =>  | 
 | 27 | +      switch l {  | 
 | 28 | +      | list{} => arr  | 
 | 29 | +      | list{hd, ...tl} =>  | 
 | 30 | +        Array.unsafe_set(arr, i, hd->encode)  | 
 | 31 | +        fill(i + 1, tl)  | 
 | 32 | +      }  | 
 | 33 | +    fill(1, tl)->Composite.jsonArray  | 
 | 34 | +  }  | 
 | 35 | + | 
 | 36 | +let option = (encode, opt) =>  | 
 | 37 | +  switch opt {  | 
 | 38 | +  | None => null  | 
 | 39 | +  | Some(v) => v->encode  | 
 | 40 | +  }  | 
 | 41 | + | 
 | 42 | +let withDefault = (default, encode, opt) =>  | 
 | 43 | +  switch opt {  | 
 | 44 | +  | None => default  | 
 | 45 | +  | Some(v) => v->encode  | 
 | 46 | +  }  | 
 | 47 | + | 
 | 48 | +let date = date => date->Js.Date.toJSONUnsafe->string  | 
 | 49 | + | 
 | 50 | +let pair = (encodeA, encodeB, (a, b)) => [a->encodeA, b->encodeB]->Composite.jsonArray  | 
 | 51 | +let tuple2 = (encodeA, encodeB, (a, b)) => [a->encodeA, b->encodeB]->Composite.jsonArray  | 
 | 52 | +let tuple3 = (encodeA, encodeB, encodeC, (a, b, c)) =>  | 
 | 53 | +  [a->encodeA, b->encodeB, c->encodeC]->Composite.jsonArray  | 
 | 54 | +let tuple4 = (encodeA, encodeB, encodeC, encodeD, (a, b, c, d)) =>  | 
 | 55 | +  [a->encodeA, b->encodeB, c->encodeC, d->encodeD]->Composite.jsonArray  | 
 | 56 | + | 
 | 57 | +let dict = (encode, dict) => Js.Dict.map((. v) => encode(v), dict)->Composite.jsonDict  | 
 | 58 | + | 
6 | 59 | module Unsafe = {  | 
7 | 60 |   external object: {..} => Js.Json.t = "%identity"  | 
8 | 61 | }  | 
0 commit comments