Skip to content

[FR] - Option to preserve the insertion order #1

@cgiachalis

Description

@cgiachalis

By default, the insertion order is not preserved; but usually in R related tasks preserving the order of the object elements is important, i.e., reconstruct data-frames from jsons.

l <- list(Jan = 1L, Feb = 2L, Mar = 3L)

j1 <- yyjsonr::write_json_str(l, auto_unbox = TRUE)

j1
# {"Jan":1,"Feb":2,"Mar":3} 

# Default behaviour: object keys are sorted alphabetically
jstring <- jsonString$new(j1)

jstring
# {
#    "Feb": 2,
#    "Jan": 1,
#    "Mar": 3
# }

all.equal(jstring$asString(), j1)
# "1 string mismatch"

# Or reconstruct to list
l2 <- yyjsonr::read_json_str(jstring$asString())
all.equal(l, l2)
# [1] "Names: 2 string mismatches"                
# [2] "Component 1: Mean relative difference: 1"  
# [3] "Component 2: Mean relative difference: 0.5"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions