From 75b67665d4b8161981478843268ddc2a5c5418b7 Mon Sep 17 00:00:00 2001 From: Chas Emerick Date: Fri, 21 Jan 2022 00:52:34 -0500 Subject: [PATCH] remove restriction against generating JSON with an atomic root value (fixes #121) --- CHANGES.md | 2 ++ lib/common.ml | 7 ------- lib/pretty.ml | 6 +----- lib/write.ml | 8 ++------ 4 files changed, 5 insertions(+), 18 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 43ae3640..3667d313 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -12,6 +12,8 @@ - Removed `yojson-biniou` library - Removed deprecated `json` type aliasing type `t` which has been available since 1.6.0 (@Leonidas-from-XIV, #100). +- Removed constraint that the "root" value being rendered (via either + `pretty_print` or `to_string`) must be an object or array. (@cemerick, #121) ### Add diff --git a/lib/common.ml b/lib/common.ml index df94ac11..d15e651b 100644 --- a/lib/common.ml +++ b/lib/common.ml @@ -72,13 +72,6 @@ let code_of_surrogate_pair i j = let utf8_of_surrogate_pair buf i j = utf8_of_code buf (code_of_surrogate_pair i j) -let is_object_or_array x = - match x with - `List _ - | `Assoc _ -> true - | _ -> false - - type lexer_state = { buf : Buffer.t; (* Buffer used to accumulate substrings *) diff --git a/lib/pretty.ml b/lib/pretty.ml index fef7c715..d3d4f405 100644 --- a/lib/pretty.ml +++ b/lib/pretty.ml @@ -49,11 +49,7 @@ and format_field std out (name, x) = Format.fprintf out "@[%s: %a@]" (json_string_of_string name) (format std) x let pp ?(std = false) out x = - if std && not (is_object_or_array x) then - json_error - "Root is not an object or array as requested by the JSON standard" - else - Format.fprintf out "@[%a@]" (format std) (x :> t) + Format.fprintf out "@[%a@]" (format std) (x :> t) let to_string ?std x = Format.asprintf "%a" (pp ?std) x diff --git a/lib/write.ml b/lib/write.ml index 0bd87543..0e2090c9 100644 --- a/lib/write.ml +++ b/lib/write.ml @@ -418,12 +418,8 @@ and write_std_variant ob s o = let to_buffer ?(std = false) ob x = - if std then ( - if not (is_object_or_array x) then - json_error "Root is not an object or array" - else - write_std_json ob x - ) + if std then + write_std_json ob x else write_json ob x