LogtalkDotOrg / logtalk3

Logtalk - declarative object-oriented logic programming language
https://logtalk.org
Apache License 2.0
415 stars 30 forks source link

JSON generation bug: string-with-an-empty-object #196

Closed yrashk closed 8 months ago

yrashk commented 8 months ago

It looks like generating JSON with a string that looks like an empty object ("{}") generates unexpected JSON:

Compare this:

?- json::parse(chars("\"{}\""), JSON), json::generate(chars(O), JSON).
JSON = {},
O = ['{', '}'].

to

?- json::parse(chars("{}"), JSON), json::generate(chars(O), JSON).
JSON = {},
O = ['{', '}'].
pmoura commented 8 months ago

The object json is a shortcut to json(curly,dash,atom). The bug is more clear if we switch the object representation from curly to list:

?- json(list,dash,atom)::(parse(chars("\"{}\""), JSON), generate(chars(Chars), JSON)).
JSON = {},
Chars = ['{', '}'].

?- json(list,dash,atom)::(parse(chars("{}"), JSON), generate(chars(Chars), JSON)).
JSON = json([]),
Chars = ['{', '}'].

In the first query we get an atom representation of the string, {}. In the second case, you get an empty object using list representation, json([]). I.e. the binding of the JSON variable is correct in both cases. But the binding for the variable Chars is incorrect in the first query. Fixed in bb44467c002f609fdfe86cfbdfab080c0d71acb7.

yrashk commented 8 months ago

Thank you! This was great 💯