From cf9d26740c2ecf9966ede278955cfd9e58481310 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Henry?= Date: Mon, 16 Apr 2018 00:44:23 +0200 Subject: [PATCH] Shell/RPC: allow immediate value This a HACK for allowing serialisation of toplevel value that are float, string,... and not only object or array. In a latter time, we may prefer to you `yojson` instead of `ezjsonm`, but some of our dependencies rely on `ezjsonm` and we may not want to embeds two distinct JSON libraries... --- src/lib_data_encoding/json.ml | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/lib_data_encoding/json.ml b/src/lib_data_encoding/json.ml index 617a7d3c7..63753df75 100644 --- a/src/lib_data_encoding/json.ml +++ b/src/lib_data_encoding/json.ml @@ -274,13 +274,16 @@ let to_root = function | `Null -> `O [] | oth -> `A [ oth ] -let to_string ?minify j = Ezjsonm.to_string ?minify (to_root j) +let to_string ?minify j = + Format.asprintf "%a" Json_repr.(pp ?compact:minify (module Ezjsonm)) j let pp = Json_repr.(pp (module Ezjsonm)) let from_string s = - try Ok (Ezjsonm.from_string s :> json) - with Ezjsonm.Parse_error (_, msg) -> Error msg + match Ezjsonm.from_string ("[" ^ s ^ "]") with + | exception Ezjsonm.Parse_error (_, msg) -> Error msg + | `A [ json ] -> Ok json + | _ -> Error "Malformed value" let from_stream (stream: string Lwt_stream.t) = let buffer = ref "" in