Skip to content

Commit

Permalink
R api for #29
Browse files Browse the repository at this point in the history
  • Loading branch information
dcooley committed Jan 28, 2020
1 parent 29cce69 commit 7d19389
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 23 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export(from_json)
export(minify_json)
export(pretty_json)
export(to_json)
export(to_ndjson)
export(validate_json)
importFrom(Rcpp,sourceCpp)
useDynLib(jsonify, .registration = TRUE)
14 changes: 14 additions & 0 deletions R/to_json.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ to_json <- function( x, unbox = FALSE, digits = NULL, numeric_dates = TRUE,
rcpp_to_json( x, unbox, digits, numeric_dates, factors_as_string, by )
}

#' To ndjson
#'
#' Converts R objects to ndjson
#'
#'
#' @export
to_ndjson <- function( x, unbox = FALSE, digits = NULL, numeric_dates = TRUE,
factors_as_string = TRUE, by = "row" ) {
if( "col" %in% by ) by <- "column"
by <- match.arg( by, choices = c("row", "column") )
digits <- handle_digits( digits )
rcpp_to_ndjson( x, unbox, digits, numeric_dates, factors_as_string, by )
}

handle_digits <- function( digits ) {
if( is.null( digits ) ) return(-1)
return( as.integer( digits ) )
Expand Down
69 changes: 46 additions & 23 deletions inst/include/jsonify/to_json/api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,38 +102,61 @@ namespace api {

std::ostringstream os; // for storing the final string of ndjson

for( row = 0; row < n_row; row++ ) {

// create new stream each row
rapidjson::StringBuffer sb;
rapidjson::Writer < rapidjson::StringBuffer > writer( sb );

if( by == "row" ) {

writer.StartObject();
for( row = 0; row < n_row; row++ ) {

// create new stream each row
rapidjson::StringBuffer sb;
rapidjson::Writer < rapidjson::StringBuffer > writer( sb );

writer.StartObject();
for( df_col = 0; df_col < n_cols; df_col++ ) {

const char *h = column_names[ df_col ];
writer.String( h );

SEXP this_vec = df[ h ];

switch( TYPEOF( this_vec ) ) {

case VECSXP: {
Rcpp::List lst = Rcpp::as< Rcpp::List >( this_vec );
jsonify::writers::complex::write_value( writer, lst, unbox, digits, numeric_dates, factors_as_string, by, row, in_data_frame );
break;
}
default: {
jsonify::writers::complex::switch_vector( writer, this_vec, unbox, digits, numeric_dates, factors_as_string, row );
}
} // end switch

} // end for
writer.EndObject();

os << sb.GetString();
os << '\n';
} // end for (row)

} else {
// by == "column"
for( df_col = 0; df_col < n_cols; df_col++ ) {
// create new stream each row
rapidjson::StringBuffer sb;
rapidjson::Writer < rapidjson::StringBuffer > writer( sb );

writer.StartObject();

const char *h = column_names[ df_col ];
writer.String( h );

SEXP this_vec = df[ h ];
jsonify::writers::complex::write_value( writer, this_vec, unbox, digits, numeric_dates, factors_as_string, by, -1, in_data_frame );

switch( TYPEOF( this_vec ) ) {

case VECSXP: {
Rcpp::List lst = Rcpp::as< Rcpp::List >( this_vec );
jsonify::writers::complex::write_value( writer, lst, unbox, digits, numeric_dates, factors_as_string, by, row, in_data_frame );
break;
}
default: {
jsonify::writers::complex::switch_vector( writer, this_vec, unbox, digits, numeric_dates, factors_as_string, row );
}
} // end switch
writer.EndObject();

} // end for
writer.EndObject();
os << sb.GetString();
os << '\n';
} // end for (column)

os << sb.GetString();
os << '\n';
}

Rcpp::StringVector sv = os.str();
Expand Down
12 changes: 12 additions & 0 deletions man/to_ndjson.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7d19389

Please sign in to comment.