Skip to content

Commit

Permalink
cran release
Browse files Browse the repository at this point in the history
  • Loading branch information
dcooley committed Nov 4, 2019
1 parent e1ceb51 commit b49e585
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 60 deletions.
2 changes: 2 additions & 0 deletions CRAN-RELEASE
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This package was submitted to CRAN on 2019-11-03.
Once it is accepted, delete this file and tag the release (commit e1ceb51848).
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: jsonify
Type: Package
Title: Convert between 'R' Objects and Javascript Object Notation (JSON)
Title: Convert Between 'R' Objects and Javascript Object Notation (JSON)
Version: 1.0.0
Date: 2019-02-11
Date: 2019-11-03
Authors@R: c(
person("David", "Cooley", ,"[email protected]", role = c("aut", "cre")),
person("Chris", "Muir", role = "ctb")
Expand Down
5 changes: 2 additions & 3 deletions cran-comments.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
## Cran submission updates

* Updates v0.2.0 to v0.2.1
* updated date field
* one bug fix and one new method
* Updates v0.2.1 to v1.0.0
* added from_json() method

## R CMD check results

Expand Down
2 changes: 1 addition & 1 deletion inst/include/jsonify/from_json/simplify/simplify.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ namespace from_json {
}

if( tp == -1 && i > 0 ) {
// can't simplify because new column names"
// can't simplify because new column names
return out;
}

Expand Down
14 changes: 2 additions & 12 deletions inst/include/jsonify/to_json/writers/complex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,18 +188,8 @@ namespace complex {
const char *h = column_names[ df_col ];
writer.String( h );
SEXP this_vec = df[ h ];
write_value( writer, this_vec, unbox, digits, numeric_dates, factors_as_string, by, -1, in_data_frame );

// switch( TYPEOF( this_vec ) ) {
// case VECSXP: {
write_value( writer, this_vec, unbox, digits, numeric_dates, factors_as_string, by, -1, in_data_frame );
// break;
// }
// default: {
// Rcpp::Rcout << "by column default" << std::endl;
// // if it's a matrix, or another data.frame??
// switch_vector( writer, this_vec, unbox, digits, numeric_dates, factors_as_string );
// }
// }
}
writer.EndObject();

Expand Down Expand Up @@ -321,7 +311,7 @@ namespace complex {
const char *s = list_names[ i ];
writer.String( s );
}
// setting in_data_frame to false becaseu we're no longer at the data.frame top-level
// setting in_data_frame to false because we're no longer at the data.frame top-level
write_value( writer, recursive_list, unbox, digits, numeric_dates, factors_as_string, by, -1, false );
}

Expand Down
9 changes: 8 additions & 1 deletion src/read_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,21 @@
rapidjson::Document buffer_string( const char* file, const char* mode, int buffer_size = 1024 ) {

FILE* fp = fopen(file, mode );
char readBuffer[ buffer_size ];


//char readBuffer[ buffer_size ];
// ISO C++ forbids variable length array ‘readBuffer’ [-Wvla]
// https://stackoverflow.com/a/11379442/5977215
char *readBuffer = new char[ buffer_size ];

rapidjson::FileReadStream is(fp, readBuffer, sizeof( readBuffer ) );


rapidjson::Document d;
d.ParseStream( is );
fclose(fp);

delete[] readBuffer;
return d;
}

Expand Down
20 changes: 20 additions & 0 deletions tests/benchmarks.R
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,26 @@ microbenchmark(
# jsonlite 1.433624 1.486971 1.631894 1.537450 1.563000 2.138424 5
# jsonify 1.510242 1.528668 1.920362 1.531584 1.877338 3.153977 5

url <- 'https://github.com/SymbolixAU/data/blob/master/geojson/SA1_2016_VIC.json?raw=true'

js <- readLines(url)

microbenchmark(
jsonify = {
jfy <- jsonify::from_json( js )
},
jsonlite = {
jlt <- jsonlite::fromJSON( js )
},
times = 5
)

# Unit: seconds
# expr min lq mean median uq max neval
# jsonify 4.14308 4.248493 4.589085 4.298684 5.016845 5.238324 5
# jsonlite 38.72263 40.025316 40.089307 40.373840 40.646151 40.678591 5


## Filling NAs
rm(list=ls()); gc()
set.seed(20191012)
Expand Down
13 changes: 6 additions & 7 deletions tests/testthat/test-from_json.R
Original file line number Diff line number Diff line change
Expand Up @@ -403,12 +403,7 @@ test_that("round trips work", {
x <- list(x = 1, y = letters)
expect_identical( f1(x), x )
expect_identical( f2(x), x )

# # TODO
# x <- list(1L, "cats", 3L, NA)
# expect_identical( f1(x), x )
# expect_identical( f2(x), x )


x <- data.frame(
"col1" = c(1, 2),
"col2" = c(1L, 2L),
Expand All @@ -418,7 +413,11 @@ test_that("round trips work", {
)
expect_identical( f1(x), x )



x <- list(1L, "cats", 3L, NA)
f2 <- function( x ) from_json( to_json( x, unbox = TRUE ), simplify = FALSE )
expect_identical( f2(x), x )

})


Expand Down
33 changes: 1 addition & 32 deletions tests/testthat/test-from_json_simplify.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,6 @@ test_that("R Lists simplified to vectors",{

test_that("simplify types",{

## tests function only works for arrays

## mapping
# 0 - null
# 1 - false
# 2 - true
# 3 - object
# 4 - array
# 5 - string
# 6 - number
# - 8 - int
# - 9 - double

## Simplification rules (oposite of 'to_json')
## 1. array -> vector
## 2. array of arrays (of arrays of arrays ... )
## - each array is a vector (as per 1.)
## - each vector becomes a list element
## -

x <- 1:4
js <- to_json( x )
#js
Expand Down Expand Up @@ -137,20 +117,14 @@ test_that("arrays of arrays of same length go to matrix",{
x <- from_json( js )
expect_equal(x, matrix(1:6, ncol = 2, byrow = T ) )

# x <- from_json( js, by = "col" )
# expect_equal(x, matrix(1:6, ncol = 2, byrow = F ) )


})

test_that("array of arrays of different lenghts go to list",{

js <- '[[1,2],[1,2,3]]'
x <- from_json( js )
expect_equal( x, list(1:2, 1:3))

## TODO
## -

js <- '[[5,[6,7]]]'
x <- from_json( js )
expect_equal( x, list( list( 5, 6:7 ) ) )
Expand All @@ -159,9 +133,6 @@ test_that("array of arrays of different lenghts go to list",{
x <- from_json( js )
expect_equal( x, list( list( 5, c("6","a") ) ) )

## compare with
# jsonlite::fromJSON( js )

js <- '[[1,2],[3,4],[5,[6,7]]]'
x <- from_json( js )
expect_equal( x, list( c(1,2), c(3,4), list(5, 6:7) ) )
Expand All @@ -178,5 +149,3 @@ test_that("array of various types converted to matrices",{
expect_equal(x, matrix(c(1.1,2,3,4), ncol = 2, byrow = T ) )
})



60 changes: 58 additions & 2 deletions vignettes/jsonify.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@
title: "jsonify"
author: "David Cooley"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
output:
html_document:
toc: true
toc_float: true
number_sections: false
theme: flatly
header-includes:
- \usepackage{tikz}
- \usetikzlibrary{arrows}
vignette: >
%\VignetteIndexEntry{Vignette Title}
%\VignetteIndexEntry{Jsonify}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
Expand All @@ -18,6 +26,8 @@ knitr::opts_chunk$set(
library(jsonify)
```

# To JSON

There are two types of R objects we need to handle when converting to JSON, simple and complex.

1. Simple - scalars, vectors, matrices
Expand Down Expand Up @@ -144,4 +154,50 @@ pretty_json( df )
```

# From JSON

Use `from_json()` to convert from JSON to an R object.


```{r}
## scalar / vector
js <- '[1,2,3]'
from_json( js )
## matrix
js <- '[[1,2],[3,4],[5,6]]'
from_json( js )
## data.frame
js <- '[{"x":1,"y":"a"},{"x":2,"y":"b"}]'
from_json( js )
```

## Simplifying and NAs

By default `from_json()` will try and simplify

- arrays to vectors
- arrays of arrays to matrices
- array of objects with consistent key-value pairs to data.frames

If an array contains objects with different keys, for example `'[{"x":1},{"y":2}]'`, `from_json()` will not simplify this to a data.frame, because it would have to assume and insert `NA`s in rows where data is missing.

```{r}
js <- '[{"x":1},{"y":2}]'
from_json( js )
```

You can override this default and use `fill_na = TRUE` to force it to a data.frame with `NA`s in place of missing values

```{r}
js <- '[{"x":1},{"y":2}]'
from_json( js, fill_na = TRUE )
```







0 comments on commit b49e585

Please sign in to comment.