Skip to content

Commit

Permalink
Merge pull request #100 from AestheticIntegration/ewen/MattJSONDecoders
Browse files Browse the repository at this point in the history
Ewen/matt json decoders
  • Loading branch information
ewenmaclean authored Oct 26, 2018
2 parents ffa9123 + 2756816 commit 6e9be06
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 173 deletions.
128 changes: 70 additions & 58 deletions src-core-pp/base_types_json.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
open Yojson;;
open Numeric;;
module JU = Yojson.Basic.Util;;
module D = Decoders_yojson.Basic.Decode;;
open D.Infix;;

(**
* Int
Expand All @@ -22,8 +24,8 @@ let int_opt_to_json : int option -> json = function
| Some x -> int_to_json x
;;

let json_to_int_opt x : int option=
JU.to_int_option x;;
let int_decoder : int Decoders_yojson.Basic.Decode.decoder =
D.int;;


(**
Expand All @@ -35,8 +37,8 @@ let char_opt_to_json = function
| None -> `Null
| Some x -> `String x;;

let json_to_char_opt x : string option =
JU.to_string_option x;;
let char_decoder : string Decoders_yojson.Basic.Decode.decoder =
D.string;;


(**
Expand All @@ -56,11 +58,13 @@ let float_0_opt_to_json = function
| Some x -> float_0_to_json x
;;

let json_to_float_0_opt json : Numeric.fix_float_0 option =
match JU.(json |> member "Precision" |> to_int_option ) with None -> None | Some precision ->
if precision != 0 then None else
match JU.(json |> member "Number" |> to_int_option ) with None -> None | Some number ->
Some Numeric.( float_Create_0 number);;
let float_0_decoder : Numeric.fix_float_0 Decoders_yojson.Basic.Decode.decoder =
D.field "Precision" D.int >>= fun p ->
if (p <> 0) then
D.fail "Precision must be 0 in float_0 json encoding." else
D.field "Number" D.int >>= fun x ->
D.succeed
Numeric.(float_Create_0 x);;

let float_1_to_json : fix_float_1 -> json = function
| Float_1 x ->
Expand All @@ -75,11 +79,13 @@ let float_1_opt_to_json = function
| Some x -> float_1_to_json x
;;

let json_to_float_1_opt json : Numeric.fix_float_1 option =
match JU.(json |> member "Precision" |> to_int_option ) with None -> None | Some precision ->
if precision != 1 then None else
match JU.(json |> member "Number" |> to_int_option ) with None -> None | Some number ->
Some Numeric.( float_Create_1 number);;
let float_1_decoder : Numeric.fix_float_1 Decoders_yojson.Basic.Decode.decoder =
D.field "Precision" D.int >>= fun p ->
if (p <> 1) then
D.fail "Precision must be 1 in float_1 json encoding." else
D.field "Number" D.int >>= fun x ->
D.succeed
Numeric.(float_Create_1 x);;

let float_2_to_json : fix_float_2 -> json = function
| Float_2 x ->
Expand All @@ -94,11 +100,14 @@ let float_2_opt_to_json = function
| Some x -> float_2_to_json x
;;

let json_to_float_2_opt json : Numeric.fix_float_2 option =
match JU.(json |> member "Precision" |> to_int_option ) with None -> None | Some precision ->
if precision != 2 then None else
match JU.(json |> member "Number" |> to_int_option ) with None -> None | Some number ->
Some Numeric.( float_Create_2 number);;
let float_2_decoder : Numeric.fix_float_2 Decoders_yojson.Basic.Decode.decoder =
D.field "Precision" D.int >>= fun p ->
if (p <> 2) then
D.fail "Precision must be 2 in float_2 json encoding." else
D.field "Number" D.int >>= fun x ->
D.succeed
Numeric.(float_Create_2 x);;


let float_3_to_json : fix_float_3 -> json = function
| Float_3 x ->
Expand All @@ -113,11 +122,14 @@ let float_3_opt_to_json = function
| Some x -> float_3_to_json x
;;

let json_to_float_3_opt json : Numeric.fix_float_3 option =
match JU.(json |> member "Precision" |> to_int_option ) with None -> None | Some precision ->
if precision != 3 then None else
match JU.(json |> member "Number" |> to_int_option ) with None -> None | Some number ->
Some Numeric.( float_Create_3 number);;
let float_3_decoder : Numeric.fix_float_3 Decoders_yojson.Basic.Decode.decoder =
D.field "Precision" D.int >>= fun p ->
if (p <> 3) then
D.fail "Precision must be 3 in float_3 json encoding." else
D.field "Number" D.int >>= fun x ->
D.succeed
Numeric.(float_Create_3 x);;


let float_4_to_json : fix_float_4 -> json = function
| Float_4 x ->
Expand All @@ -132,11 +144,14 @@ let float_4_opt_to_json = function
| Some x -> float_4_to_json x
;;

let json_to_float_4_opt json : Numeric.fix_float_4 option =
match JU.(json |> member "Precision" |> to_int_option ) with None -> None | Some precision ->
if precision != 4 then None else
match JU.(json |> member "Number" |> to_int_option ) with None -> None | Some number ->
Some Numeric.( float_Create_4 number);;
let float_4_decoder : Numeric.fix_float_4 Decoders_yojson.Basic.Decode.decoder =
D.field "Precision" D.int >>= fun p ->
if (p <> 4) then
D.fail "Precision must be 4 in float_4 json encoding." else
D.field "Number" D.int >>= fun x ->
D.succeed
Numeric.(float_Create_4 x);;


let float_5_to_json : fix_float_5-> json = function
| Float_5 x ->
Expand All @@ -151,11 +166,14 @@ let float_5_opt_to_json = function
| Some x -> float_5_to_json x
;;

let json_to_float_5_opt json : Numeric.fix_float_5 option =
match JU.(json |> member "Precision" |> to_int_option ) with None -> None | Some precision ->
if precision != 5 then None else
match JU.(json |> member "Number" |> to_int_option ) with None -> None | Some number ->
Some Numeric.( float_Create_5 number);;
let float_5_decoder : Numeric.fix_float_5 Decoders_yojson.Basic.Decode.decoder =
D.field "Precision" D.int >>= fun p ->
if (p <> 5) then
D.fail "Precision must be 5 in float_5 json encoding." else
D.field "Number" D.int >>= fun x ->
D.succeed
Numeric.(float_Create_5 x);;


let float_6_to_json : fix_float_6 -> json = function
| Float_6 x ->
Expand All @@ -170,19 +188,21 @@ let float_6_opt_to_json = function
| Some x -> float_6_to_json x
;;

let json_to_float_6_opt json : Numeric.fix_float_6 option =
match JU.(json |> member "Precision" |> to_int_option ) with None -> None | Some precision ->
if precision != 6 then None else
match JU.(json |> member "Number" |> to_int_option ) with None -> None | Some number ->
Some Numeric.( float_Create_6 number);;
let float_6_decoder : Numeric.fix_float_6 Decoders_yojson.Basic.Decode.decoder =
D.field "Precision" D.int >>= fun p ->
if (p <> 6) then
D.fail "Precision must be 6 in float_6 json encoding." else
D.field "Number" D.int >>= fun x ->
D.succeed
Numeric.(float_Create_6 x);;


let float_to_json x = float_6_to_json x;;

let float_opt_to_json x = float_6_opt_to_json x;;


let json_to_float_opt = json_to_float_6_opt;;
let float_decoder = float_6_decoder;;
(*
FIX_String
*)
Expand All @@ -191,16 +211,15 @@ let string_to_json x : json =
`String x
;;

let json_to_string_opt x : string option =
JU.to_string_option x
;;


let string_opt_to_json = function
| None -> `Null
| Some x -> string_to_json x
;;

let string_decoder : string Decoders_yojson.Basic.Decode.decoder =
D.string
;;


(**
* FIX_Symbol
Expand All @@ -210,15 +229,15 @@ let symbol_to_json x : json =
`String x
;;

let json_to_symbol_opt x : string option =
JU.to_string_option x
;;

let symbol_opt_to_json = function
| None -> `Null
| Some x -> symbol_to_json x
;;

let symbol_decoder : string Decoders_yojson.Basic.Decode.decoder =
D.string
;;


(**
* FIX_Bool
Expand All @@ -229,15 +248,8 @@ let bool_to_json : bool -> json = function
| false -> `String "false"
;;

let json_to_bool_opt x : bool option =
match (JU.to_string_option x) with
| None -> None
| Some b -> (match b with
| "true" -> Some true
| "false" -> Some false
| _ -> None
)
;;
let bool_decoder : bool Decoders_yojson.Basic.Decode.decoder =
D.bool;;

let bool_opt_to_json : bool option -> json = function
| None -> `Null
Expand Down
Loading

0 comments on commit 6e9be06

Please sign in to comment.