-
Notifications
You must be signed in to change notification settings - Fork 10
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
Per-element control of unbox
#75
Comments
These give the same output x <- 1:3
l <- list(
unboxed_prop = "foo",
boxed_prop = I(x) ## 'protect' the value here with I()/AsIs
)
l |> jsonlite::toJSON(auto_unbox = TRUE)
l |> jsonify::to_json(unbox = TRUE) so I'm not really sure what your question is |
When x <- 1
l <- list(
unboxed_prop = "foo",
boxed_prop = I(x) ## 'protect' the value here with I()/AsIs
)
l |> jsonlite::toJSON(auto_unbox = TRUE) ## {"unboxed_prop":"foo","boxed_prop":[1]}
l |> jsonify::to_json(unbox = TRUE) ## {"unboxed_prop":"foo","boxed_prop":1.0} And here the key is that a receiver of this JSON may require that "boxed_prop" be an array, while also requiring the "unboxed_prop" is scalar. |
There's a working prototype on branch remotes::install_github("SymbolixAU/jsonify", ref = "issue75") |
Thanks! I'll take a look over the next day or so and report back :-) |
Hi @mmuurr did this work for you? |
+1 on this request; |
This is working on branch Install the
Then this should work x <- 1
l <- list(
unboxed_prop = "foo",
boxed_prop = I(x) ## 'protect' the value here with I()/AsIs
)
l |> jsonlite::toJSON(auto_unbox = TRUE)
# {"unboxed_prop":"foo","boxed_prop":[1]}
l |> jsonify::to_json(unbox = TRUE)
# {"unboxed_prop":"foo","boxed_prop":[1.0]} |
Note, however, using x <- 1
l <- list(
unboxed_prop = "foo",
boxed_prop = I(c(x,x)) ## length > 1 vector
)
l |> jsonlite::toJSON(auto_unbox = TRUE)
# {"unboxed_prop":"foo","boxed_prop":[1,1]}
l |> jsonify::to_json(unbox = TRUE)
# {"unboxed_prop":"foo","boxed_prop":[1.0,1.0]} |
{jsonify} is great, but I struggle to use it as a mediator for communication with some external APIs due to one missing feature: element-level control of the
unbox
parameter (into_json()
).A third-party API might expect this object:
With {jsonlite}, we'd coerce the corresponding R object to something like:
With
I(x)
,boxed_prop
is guaranteed to be JSON-encoded as an array regardless oflength(x)
.Is something like this possible with {jsonify}?
The text was updated successfully, but these errors were encountered: