serde-ml / serde

Serialization framework for OCaml
MIT License
174 stars 11 forks source link

Serde failing to parse string with additional whitespace #37

Open maxRN opened 1 month ago

maxRN commented 1 month ago

Serde fails to parse this JSON object correctly:

{ "x": [ { "name": "max" } ] }

with the following type definitions:

type a = { name : string } [@@deriving deserialize, serialize,]
type t = { x : a list } [@@deriving deserialize, serialize,]

error message:

Fatal error: exception Yojson__Common.Json_error("Line 6, bytes 5-12:\nExpected ',' but found '\n  ]\n}\n'")

I have created a reproduction repo here: https://github.com/maxrn/serde_list_repro

linear[bot] commented 1 month ago

RIOT-35 Serde failing to parse list with one object

maxRN commented 1 month ago

Did some more investigating and it seems to be related to white space. Here are two examples that don't work

let does_not_work = {| { "records": [ { "hello": "some str" } ] } |};;
(*                                                           ^ the problem *)
let does_not_work2 = {| { "records": [ { "hello": "some str" } , { "hello": "some other str" }] } |};;
(*                                                            ^ the problematic whitespace *)

whereas these two strings parse fine:

let this_works = {| { "records": [ { "hello": "some str" }] } |};;
let this_works2 = {| { "records": [ { "hello": "some str" }, { "hello": "some other str" }] } |};;

Note the additional whitespace between } ] and } , in the non-working examples.