+ relative time jsont
This commit is contained in:
parent
4930cd3ae3
commit
19a05f84e7
1 changed files with 30 additions and 0 deletions
30
src/json.ml
30
src/json.ml
|
|
@ -4,6 +4,8 @@ let encode_exn jsont v = Jsont_bytesrw.encode_string jsont v |> Result.get_ok
|
||||||
let encode jsont v = Jsont_bytesrw.encode_string jsont v
|
let encode jsont v = Jsont_bytesrw.encode_string jsont v
|
||||||
let decode jsont v = Jsont_bytesrw.decode_string jsont v
|
let decode jsont v = Jsont_bytesrw.decode_string jsont v
|
||||||
|
|
||||||
|
(* JavaScript numbers restricted to integers. *)
|
||||||
|
|
||||||
module Error_detail = struct
|
module Error_detail = struct
|
||||||
open Error_detail
|
open Error_detail
|
||||||
|
|
||||||
|
|
@ -44,3 +46,31 @@ module Timestamp = struct
|
||||||
|> Jsont.Object.mem "t_s" number_or_never_jsont ~enc:Fun.id
|
|> Jsont.Object.mem "t_s" number_or_never_jsont ~enc:Fun.id
|
||||||
|> Jsont.Object.finish
|
|> Jsont.Object.finish
|
||||||
end
|
end
|
||||||
|
|
||||||
|
module Relative_time = struct
|
||||||
|
open Relative_time
|
||||||
|
|
||||||
|
let number_or_forever_jsont =
|
||||||
|
let forever =
|
||||||
|
let dec s =
|
||||||
|
match s with
|
||||||
|
| "forever" -> Forever
|
||||||
|
| _ -> Jsont.Error.msg Jsont.Meta.none "unexpected string value"
|
||||||
|
in
|
||||||
|
let enc = function Forever -> "forever" | _ -> assert false in
|
||||||
|
Jsont.map ~dec ~enc Jsont.string
|
||||||
|
in
|
||||||
|
let number =
|
||||||
|
let dec n = Microseconds n in
|
||||||
|
let enc = function Microseconds n -> n | _ -> assert false in
|
||||||
|
Jsont.map ~dec ~enc Jsont.number
|
||||||
|
in
|
||||||
|
let enc = function Forever -> forever | Microseconds _ -> number in
|
||||||
|
Jsont.any ~dec_string:forever ~dec_number:number ~enc ()
|
||||||
|
|
||||||
|
let jsont =
|
||||||
|
let make t_s = t_s in
|
||||||
|
Jsont.Object.map ~kind:"RelativeTime" make
|
||||||
|
|> Jsont.Object.mem "t_s" number_or_forever_jsont ~enc:Fun.id
|
||||||
|
|> Jsont.Object.finish
|
||||||
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue