Skip to content

Proposal: Named list should be a JSON object, regardless of length #79

Description

@mmuurr

While no cross-language marshaling scheme will be perfect, but I think when it comes to R lists there can be a distinction between named (representing an JSON object) and unnamed (representing a generic JSON array). Current conversions look like so, along with what I think are reasonable expectations in the comments.

library(jsonify)

unnamed_empty_list <- list()  ## names() -> NULL
named_empty_list <- unnamed_empty_list |> rlang::set_names(character(0))  ## names() -> character(0)
str(unnamed_empty_list)
#>  list()
str(named_empty_list)
#>  Named list()

json_empty_array <- "[]"
json_empty_object <- "{}"

## expect: []
to_json(unnamed_empty_list)
#> []

## expect: {}
to_json(named_empty_list)
#> []

## expect: unnamed list()
from_json(json_empty_array) |> str()
#>  list()

## expect: named list()
from_json(json_empty_object) |> str()
#>  NULL

Thoughts on supporting this distinction between a named and unnamed list? (This really only affects the empty list case.)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions