Skip to content

Instantly share code, notes, and snippets.

@cbare
Created October 6, 2012 00:13
Show Gist options
  • Select an option

  • Save cbare/3843189 to your computer and use it in GitHub Desktop.

Select an option

Save cbare/3843189 to your computer and use it in GitHub Desktop.
Test some corner-cases in JSON serialization of R objects
library(RJSONIO)
# locations will be an array of objects in JSON
# expected serialization: {"locations": [ {"path":"https://s3.amazonaws.com/foo/bar/", "type":"awss3"} ]}
e1 <- list(locations=list(list(path="https://s3.amazonaws.com/foo/bar/",type="awss3")))
e2 <- fromJSON(toJSON(e1))
if(!(names(e2) == 'locations'))
stop("Roundtrip R->JSON->R failed names")
if( class(e2$locations)!='list' )
stop("Roundtrip R->JSON->R failed singleton list of objects")
if( !all( c('path','type') %in% names(e2$locations[[1]]) ) )
stop("Roundtrip R->JSON->R failed singleton list of objects")
# the only interpretation here is a JSON array with 1 in it.
# expected serialization: [1]
a <- list(c(1))
b <- fromJSON(toJSON(a))
if( b!=1 )
stop("Roundtrip R->JSON->R failed list(1)")
# should be an array of arrays?
# expected serialization: [[1]]
a <- list(list(1))
b <- fromJSON(toJSON(a))
if( !(class(b)=='list' && b[[1]]==1) )
stop("Roundtrip R->JSON->R failed list(list(1))")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment