Skip to content

Commit

Permalink
remove restriction against generating JSON with an atomic root value (f…
Browse files Browse the repository at this point in the history
…ixes #121)
  • Loading branch information
cemerick committed Jan 25, 2022
1 parent caa7cd8 commit 75b6766
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 18 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 0 additions & 7 deletions lib/common.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 *)
Expand Down
6 changes: 1 addition & 5 deletions lib/pretty.ml
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,7 @@ and format_field std out (name, x) =
Format.fprintf out "@[<hv2>%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 "@[<hv2>%a@]" (format std) (x :> t)
Format.fprintf out "@[<hv2>%a@]" (format std) (x :> t)

let to_string ?std x =
Format.asprintf "%a" (pp ?std) x
Expand Down
8 changes: 2 additions & 6 deletions lib/write.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 75b6766

Please sign in to comment.