json_encode() re-emits a decoded empty JSON object as [], so {} and {"":1} are not round-trip stable

Open Beginner friendly
#361 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
r, rust
Domain
backend

Research direction

Start by reading .json_write() and the .json_array_of() path mentioned in the issue. Check how decoded lists with an empty names attribute and with an empty-string key are classified, then run the relevant JSON codec tests. Done means those cases encode as JSON objects and the listed round trips remain stable.

Written by the indexing model from the issue text.

Description

area/api-bindings good first issue help wanted kind/contract severity/4-minor

json_encode() re-emits a decoded JSON object as an array whenever every key is empty: {} becomes [], {"a":{}} becomes {"a":[]}, and {"":1} becomes [1], so the codec is not round-trip stable for objects.

library(rssn)
str(json_decode("{}"))                       # Named list()  -- decoded correctly as an object
json_encode(json_decode("{}"))               # "[]"          -- should be "{}"
json_encode(json_decode('{"a":{}}'))         # '{"a":[]}'    -- should be '{"a":{}}'
json_encode(json_decode('{"":1}'))           # "[1]"         -- should be '{"":1}'

Actual round-trip stability:

{}           -> []        -> []        stable=FALSE
{"a":{}}     -> {"a":[]}  -> {"a":[]}  stable=FALSE
{"":1}       -> [1]       -> [1]       stable=FALSE
[]           -> []        -> []        stable=TRUE

json_decode() does its job: str(json_decode("{}")) is Named list() (a names attribute of character(0), distinct from the NULL names of []), and str(json_decode('{"":1}')) carries the name "". The defect is in .json_write(), which decides object-vs-array with if (!is.null(names) && any(nzchar(names))): an empty names vector, or one whose only entries are "", fails any(nzchar(...)) and falls through to .json_array_of(). The presence of a names attribute (even empty) is what marks a JSON object, so the test should not require a non-empty name.

This makes json_encode(json_decode(x)) lossy for any API response containing an empty object or an empty-string key, and it is independent of the escaping defects in #302/#303/#307 (no strings with escapes are involved here).

rssn 0.3.0, R 4.6.2 binding, Windows x86_64.

Dominant language
Rust
Stars
36
Forks
3
Avg merge
14h 51m
Merged PRs (30d)
29

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Apich-Organization/rssn

All issues in Apich-Organization/rssn

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.