package decoders-cbor

  1. Overview
  2. Docs

Source file decode.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
open Decoders

module Cbor_decodeable : Decode.Decodeable with type value = CBOR.Simple.t =
struct
  type value = CBOR.Simple.t

  let pp fmt t = Format.fprintf fmt "@[%s@]" (CBOR.Simple.to_diagnostic t)

  let of_string (input : string) : (value, string) result =
    try Ok (CBOR.Simple.decode input) with CBOR.Error msg -> Error msg


  let of_file (file : string) : (value, string) result =
    try
      Ok
        (Util.with_file_in file (fun chan ->
             Util.read_all chan |> CBOR.Simple.decode ) )
    with
    | e ->
        Error (Printexc.to_string e)


  let get_string = function `Text str -> Some str | _ -> None

  let get_int = function `Int int -> Some int | _ -> None

  let get_float = function `Float float -> Some float | _ -> None

  let get_null = function `Null -> Some () | _ -> None

  let get_bool = function `Bool bool -> Some bool | _ -> None

  let get_list = function `Array a -> Some a | _ -> None

  let get_key_value_pairs = function `Map assoc -> Some assoc | _ -> None

  let to_list vs = `Array vs
end

include Decode.Make (Cbor_decodeable)

(* CBOR-specific decoders *)

let undefined : unit decoder = function
  | `Undefined ->
      Ok ()
  | json ->
      (fail "Expected Undefined") json


let simple : int decoder = function
  | `Simple i ->
      Ok i
  | json ->
      (fail "Expected Simple") json


let bytes : string decoder = function
  | `Bytes b ->
      Ok b
  | json ->
      (fail "Expected bytes") json
OCaml

Innovation. Community. Security.