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

Switch from RJSONIO to jsonlite #606

Merged
merged 1 commit into from
Jan 21, 2015
Merged

Switch from RJSONIO to jsonlite #606

merged 1 commit into from
Jan 21, 2015

Conversation

wch
Copy link
Collaborator

@wch wch commented Oct 3, 2014

This fixes #572 and #228. I think we'd want jsonlite to support different behavior for digits before merging this.

I'm a bit less certain of the fromJSON() compatibility than I am of toJSON.

Some comparisons:

compare <- function(x, ...) {
  resr <- jsonlite::minify(RJSONIO::toJSON(x, ...))
  resj <- jsonlite::minify(jsonlite::toJSON(x, dataframe="columns",
            null="null", na="null", auto_unbox=TRUE, ...))

  if (!identical(resr, resj)) {
    cat(paste0("Results differ.",
      "\nRJSONIO:  ", resr,
      "\njsonlite: ", resj
    ))
  } else {
    cat(resj)
  }

  invisible()
}


compare(list(2))
# [2]

compare(data.frame(x=1:2, y=c("a","b")))
# {"x":[1,2],"y":["a","b"]}

# Differences in displaying digits
compare(
  data.frame(x=c(1.2323123231, 5.12345678901234e19), y=c("a","b"))
)
# Results differ.
# RJSONIO:  {"x":[1.2323,5.1235e+19],"y":["a","b"]}
# jsonlite: {"x":[1.2323,51234567890123399168],"y":["a","b"]}


# In RJSONIO, digits controls precision; in jsonlite, it controls digits after
# the decimal.
compare(
  data.frame(x=c(1.2323123231, 5.12345678901234e19), y=c("a","b")),
  digits = 2
)
# Results differ.
# RJSONIO:  {"x":[1.2,5.1e+19],"y":["a","b"]}
# jsonlite: {"x":[1.23,51234567890123399168],"y":["a","b"]}

# This is a RJSONIO bug
compare(list(x = matrix(nrow = 0, ncol = 1)))
#  Error: parse error: unallowed token at this point in JSON text
#                              {  "x":   }
#                      (right here) ------^

compare(list(matrix(1:2, nrow=1)))
# [[[1,2]]]

compare(list(matrix(1:2, ncol=1)))
# [[[1],[2]]]

compare(list(x = matrix(1:4, nrow=2)))
# {"x":[[1,3],[2,4]]}

# Difference in 1x1 matrix - I think jsonlite is correct
compare(list(x = matrix(1)))
# Results differ.
# RJSONIO:  {"x":[1]}
# jsonlite: {"x":[[1]]}

compare(list(x=NULL))
# {"x":null}

compare(list(as.data.frame(t(matrix(1:2, ncol=1)))))
# [{"V1":[1],"V2":[2]}]

compare(list(x=NULL))
# {"x":null}

compare(list(x=list()))
# {"x":[]}

compare(list(x=list(a=1)[0])) # Empty named list
# {"x":{}}

@wch
Copy link
Collaborator Author

wch commented Nov 5, 2014

Updated comparisons with jsonlite 0.9.13.99, with use_signif=TRUE. The precision issue is fixed, so i think we'll be able to merge this after the next release of jsonlite.

compare <- function(x, ...) {
  resr <- jsonlite::minify(RJSONIO::toJSON(x, ...))
  resj <- jsonlite::minify(jsonlite::toJSON(x, dataframe="columns",
            null="null", na="null", auto_unbox=TRUE, use_signif=TRUE, ...))

  if (!identical(resr, resj)) {
    cat(paste0("Results differ.",
      "\nRJSONIO:  ", resr,
      "\njsonlite: ", resj
    ))
  } else {
    cat(resj)
  }

  invisible()
}


compare(list(2))
# [2]

compare(data.frame(x=1:2, y=c("a","b")))
# {"x":[1,2],"y":["a","b"]}

compare(
  data.frame(x=c(1.2323123231, 5.12345678901234e19), y=c("a","b")),
  digits = 2
)
# {"x":[1.2,5.1e+19],"y":["a","b"]}

# This is a RJSONIO bug
compare(list(x = matrix(nrow = 0, ncol = 1)))
#  Error: parse error: unallowed token at this point in JSON text
#                              {  "x":   }
#                      (right here) ------^

compare(list(matrix(1:2, nrow=1)))
# [[[1,2]]]

compare(list(matrix(1:2, ncol=1)))
# [[[1],[2]]]

compare(list(x = matrix(1:4, nrow=2)))
# {"x":[[1,3],[2,4]]}

# Difference in 1x1 matrix - I think jsonlite is correct
compare(list(x = matrix(1)))
# Results differ.
# RJSONIO:  {"x":[1]}
# jsonlite: {"x":[[1]]}

compare(list(x=NULL))
# {"x":null}

compare(list(as.data.frame(t(matrix(1:2, ncol=1)))))
# [{"V1":[1],"V2":[2]}]

compare(list(x=NULL))
# {"x":null}

compare(list(x=list()))
# {"x":[]}

compare(list(x=list(a=1)[0])) # Empty named list
# {"x":{}}

@wch wch removed the incomplete label Nov 5, 2014
@wch wch force-pushed the feature/jsonlite branch from 5505a88 to 8395520 Compare November 26, 2014 14:57
@wch wch force-pushed the feature/jsonlite branch 2 times, most recently from 7f7ed66 to dd7cd0f Compare December 2, 2014 15:05
@jeroen
Copy link

jeroen commented Dec 23, 2014

If there is anything else you need from jsonlite to merge this, please let me know.

@wch
Copy link
Collaborator Author

wch commented Dec 23, 2014

Thanks for asking! I think we're good, but we probably won't merge until after the next Shiny release.

@wch wch force-pushed the feature/jsonlite branch from dd7cd0f to 0755579 Compare January 21, 2015 20:13
wch added a commit that referenced this pull request Jan 21, 2015
Switch from RJSONIO to jsonlite
@wch wch merged commit dbe1e24 into rstudio:master Jan 21, 2015
@wch wch deleted the feature/jsonlite branch January 21, 2015 20:16
@wch wch mentioned this pull request Jan 21, 2015
zeehio added a commit to zeehio/shinyFiles that referenced this pull request Oct 2, 2016
Shiny switched to jsonlite in rstudio/shiny#606.

Using jsonlite may fix thomasp85#43.
thomasp85 pushed a commit to thomasp85/shinyFiles that referenced this pull request Apr 29, 2017
* Update travis to sudo:false

* RJSONIO to jsonlite

Shiny switched to jsonlite in rstudio/shiny#606.

Using jsonlite may fix #43.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

switch from RJSONIO to jsonlite
2 participants