Skip to content

Commit

Permalink
POC for #29
Browse files Browse the repository at this point in the history
  • Loading branch information
dcooley committed Jan 28, 2020
1 parent 6d96ad7 commit 29cce69
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 23 deletions.
104 changes: 85 additions & 19 deletions inst/include/jsonify/to_json/api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,74 +22,140 @@ namespace api {
return jsonify::utils::finalise_json( sb );
}

// inline Rcpp::StringVector to_ndjson(
// Rcpp::DataFrame& df,
// bool unbox = false,
// int digits = -1,
// bool numeric_dates = true,
// bool factors_as_string = true,
// std::string by = "row"
// ) {
// // loop over rows or columns,
// }

inline Rcpp::StringVector to_ndjson(
SEXP lst,
bool unbox = false,
int digits = -1,
bool numeric_dates = true,
bool factors_as_string = true,
SEXP lst,
bool unbox = false,
int digits = -1,
bool numeric_dates = true,
bool factors_as_string = true,
std::string by = "row"
) {


// rapidjson::StringBuffer sb;
// rapidjson::Writer < rapidjson::StringBuffer > writer( sb );
//
// std::ostringstream os; // for storing the final string of ndjson

// TODO
// the way ndjson is created depends on the type of input object
// a list will be element-wise
// data.frame / matrix will be whatever 'by' is set
// other cases not handled?
//
// given the rapidjson DOM doesn't accept '\n' characters,
// I may have to iterate over the object and create one json object at a time
// and output to an OSStream, append '\n',
// then at the end convert to StringVector.
switch( TYPEOF( lst ) ) {
case LGLSXP: {
if( !Rf_isMatrix( lst ) ) {
Rcpp::stop("jsonify - expecting a matrix, data.frame or list");
} else {
Rcpp::LogicalMatrix im = Rcpp::as< Rcpp::LogicalMatrix >( lst );
Rcpp::LogicalMatrix im = Rcpp::as< Rcpp::LogicalMatrix >( lst );
}
break;
}
case INTSXP: {
if( !Rf_isMatrix( lst ) ) {
Rcpp::stop("jsonify - expecting a matrix, data.frame or list");
} else {
Rcpp::IntegerMatrix im = Rcpp::as< Rcpp::IntegerMatrix >( lst );
Rcpp::IntegerMatrix im = Rcpp::as< Rcpp::IntegerMatrix >( lst );
}
break;
}
case REALSXP: {
if( !Rf_isMatrix( lst ) ) {
Rcpp::stop("jsonify - expecting a matrix, data.frame or list");
} else {
Rcpp::NumericMatrix nm = Rcpp::as< Rcpp::NumericMatrix >( lst );
Rcpp::NumericMatrix nm = Rcpp::as< Rcpp::NumericMatrix >( lst );
}
break;
}
case STRSXP: {
if( !Rf_isMatrix( lst ) ) {
Rcpp::stop("jsonify - expecting a matrix, data.frame or list");
} else {
Rcpp::StringMatrix im = Rcpp::as< Rcpp::StringMatrix >( lst );
Rcpp::StringMatrix im = Rcpp::as< Rcpp::StringMatrix >( lst );
}
break;
}
case VECSXP: {
if( Rf_inherits( lst, "data.frame") ) {
Rcpp::DataFrame df = Rcpp::as< Rcpp::DataFrame >( lst );
R_xlen_t n_row = df.nrow();
R_xlen_t n_cols = df.ncol();
R_xlen_t df_col;
R_xlen_t row;
Rcpp::StringVector column_names = df.names();
bool in_data_frame = true;

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 );


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';
}

Rcpp::StringVector sv = os.str();
return sv;

} else {
// list
}
break;
}
default: {
Rcpp::stop("jsonify - expecting a matrix, data.frame or list")
Rcpp::stop("jsonify - expecting a matrix, data.frame or list");
}

}
rapidjson::StringBuffer sb;
rapidjson::Writer < rapidjson::StringBuffer > writer( sb );
jsonify::writers::complex::write_value( writer, lst, unbox, digits, numeric_dates, factors_as_string, by );
return jsonify::utils::finalise_json( sb );

return Rcpp::StringVector::create();

//rapidjson::StringBuffer sb;
//rapidjson::Writer < rapidjson::StringBuffer > writer( sb );
//jsonify::writers::complex::write_value( writer, lst, unbox, digits, numeric_dates, factors_as_string, by );
//return jsonify::utils::finalise_json( sb );
}


Expand Down
10 changes: 6 additions & 4 deletions src/to_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
#include "jsonify/to_json/api.hpp"

// [[Rcpp::export]]
Rcpp::StringVector rcpp_to_json( SEXP lst, bool unbox = false, int digits = -1,
bool numeric_dates = true, bool factors_as_string = true,
std::string by = "row") {
Rcpp::StringVector rcpp_to_json(
SEXP lst, bool unbox = false, int digits = -1,
bool numeric_dates = true, bool factors_as_string = true,
std::string by = "row"
) {

if ( digits >= 0 ) {
SEXP lst2 = Rcpp::clone( lst );
Expand All @@ -18,5 +20,5 @@ Rcpp::StringVector rcpp_to_ndjson(
SEXP lst, bool unbox = false, int digits = -1, bool numeric_dates = true,
bool factors_as_string = true, std::string by = "row"
) {

return jsonify::api::to_ndjson(lst, unbox, digits, numeric_dates, factors_as_string, by );
}

0 comments on commit 29cce69

Please sign in to comment.