Skip to content

Commit

Permalink
fix: handle whitespace in arrays properly
Browse files Browse the repository at this point in the history
  • Loading branch information
maxRN committed Oct 2, 2024
1 parent fb89819 commit 2c993d7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions serde_json/serde_json.ml
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ module Deserializer = struct
Ok v

let deserialize_element self s de =
Parser.skip_space s.reader;
match Parser.peek s.reader with
| Some ']' -> Ok None
| _ ->
Expand Down
30 changes: 30 additions & 0 deletions serde_json/serde_json_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,36 @@ let _serde_json_parse_test_no_key =
(error "Failed!");
assert false

type list_of_record = { records : hello list } [@@deriving deserialize]

let _serde_json_parse_test_list_of_records =
let str = {| { "records": [ { "hello": "just one element" } ] } |} in
let parsed = Serde_json.of_string deserialize_list_of_record str in
match parsed with
| Ok parsed ->
let _ = (List.hd parsed.records).hello in
Format.printf "serde_json.ser/de test %S %s\r\n%!" "parsed with no key"
(keyword "OK")
| Error _ ->
Format.printf "serde_json.ser/de test %S %s\r\n%!" "parsed with no key"
(error "Failed!");
assert false

let _serde_json_parse_test_list_of_records_with_space =
let str =
{| { "records": [ { "hello": "two elements: one" } , { "hello": "two elements: two" }] } |}
in
let parsed = Serde_json.of_string deserialize_list_of_record str in
match parsed with
| Ok parsed ->
let _ = (List.hd parsed.records).hello in
Format.printf "serde_json.ser/de test %S %s\r\n%!" "parsed with no key"
(keyword "OK")
| Error _ ->
Format.printf "serde_json.ser/de test %S %s\r\n%!" "parsed with no key"
(error "Failed!");
assert false

type with_default = {
greeting : string;
count_with_default : int; [@serde { default = 5 }]
Expand Down

0 comments on commit 2c993d7

Please sign in to comment.