Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip JSON serialisation if already JSON (json_verbatim) #25

Open
anthonynorth opened this issue Jan 19, 2024 · 2 comments
Open

Skip JSON serialisation if already JSON (json_verbatim) #25

anthonynorth opened this issue Jan 19, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@anthonynorth
Copy link

Could we optionally skip json serialisation of character vectors that are already json? This is {jsonlite}'s json_verbatim = TRUE option. Example:

my_obj <- list(already_json = structure('{"leave_me_alone":"yes"}', class = "json"))
yyjsonr::write_json_str(my_obj, list(skip_json = TRUE))

Expected output

{"already_json":{"leave_me_alone":"yes"}}
@coolbutuseless coolbutuseless added the enhancement New feature or request label Jan 20, 2024
@mmuurr
Copy link

mmuurr commented Apr 25, 2024

+1 for this, as it's the only blocker to me switching much of my code that uses {jsonlite} to {yyjsonr} instead.

I took a brief look at the yyjson docs and think the canonical way to do this is:

  1. Parse the "json"-classed string (i.e. length-1 STRSXP) into a new yyjsonr doc (call it *x).
  2. Insert x into the recursively-growing parent doc.

It looks like yyjson requires that first parsing step to force validity of the growing structure (prior to serialization), though it smells a bit of inefficiency (if we know a priori that the string is guaranteed to be valid JSON). Nevertheless, if there's general agreement from the maintainers here that this is indeed the appropriate approach, I'm happy to take a stab at a PR to implement this feature.

@mmuurr
Copy link

mmuurr commented Apr 26, 2024

A closer look at the yyjson architecture suggests that one might be able to skip the parsing step (mentioned above) and set an element's value to a "raw" *char.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants