Skip to content

Commit

Permalink
setting up structure for #29
Browse files Browse the repository at this point in the history
  • Loading branch information
SymbolixAU committed Nov 8, 2019
1 parent 7dff7ef commit 6d96ad7
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 14 deletions.
4 changes: 4 additions & 0 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ rcpp_to_json <- function(lst, unbox = FALSE, digits = -1L, numeric_dates = TRUE,
.Call(`_jsonify_rcpp_to_json`, lst, unbox, digits, numeric_dates, factors_as_string, by)
}

rcpp_to_ndjson <- function(lst, unbox = FALSE, digits = -1L, numeric_dates = TRUE, factors_as_string = TRUE, by = "row") {
.Call(`_jsonify_rcpp_to_ndjson`, lst, unbox, digits, numeric_dates, factors_as_string, by)
}

rcpp_validate_json <- function(json) {
.Call(`_jsonify_rcpp_validate_json`, json)
}
Expand Down
96 changes: 82 additions & 14 deletions inst/include/jsonify/to_json/api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,93 @@
#include "jsonify/to_json/utils.hpp"
#include "jsonify/to_json/writers/complex.hpp"

using namespace rapidjson;

namespace jsonify {
namespace api {

inline Rcpp::StringVector to_json(
SEXP lst,
bool unbox = false,
int digits = -1,
bool numeric_dates = true,
bool factors_as_string = true,
std::string by = "row"
) {
inline Rcpp::StringVector to_json(
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 );
jsonify::writers::complex::write_value( writer, lst, unbox, digits, numeric_dates, factors_as_string, by );
return jsonify::utils::finalise_json( sb );
}

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

// 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?
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 );
}
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 );
}
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 );
}
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 );
}
break;
}
case VECSXP: {
if( Rf_inherits( lst, "data.frame") ) {
Rcpp::DataFrame df = Rcpp::as< Rcpp::DataFrame >( lst );

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 );
} else {
// list
}
break;
}
default: {
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 );
}


} // namespace api
} // namespace jsonify
Expand Down
17 changes: 17 additions & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,22 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// rcpp_to_ndjson
Rcpp::StringVector rcpp_to_ndjson(SEXP lst, bool unbox, int digits, bool numeric_dates, bool factors_as_string, std::string by);
RcppExport SEXP _jsonify_rcpp_to_ndjson(SEXP lstSEXP, SEXP unboxSEXP, SEXP digitsSEXP, SEXP numeric_datesSEXP, SEXP factors_as_stringSEXP, SEXP bySEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< SEXP >::type lst(lstSEXP);
Rcpp::traits::input_parameter< bool >::type unbox(unboxSEXP);
Rcpp::traits::input_parameter< int >::type digits(digitsSEXP);
Rcpp::traits::input_parameter< bool >::type numeric_dates(numeric_datesSEXP);
Rcpp::traits::input_parameter< bool >::type factors_as_string(factors_as_stringSEXP);
Rcpp::traits::input_parameter< std::string >::type by(bySEXP);
rcpp_result_gen = Rcpp::wrap(rcpp_to_ndjson(lst, unbox, digits, numeric_dates, factors_as_string, by));
return rcpp_result_gen;
END_RCPP
}
// rcpp_validate_json
Rcpp::LogicalVector rcpp_validate_json(Rcpp::StringVector json);
RcppExport SEXP _jsonify_rcpp_validate_json(SEXP jsonSEXP) {
Expand All @@ -136,6 +152,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_jsonify_rcpp_read_json_file", (DL_FUNC) &_jsonify_rcpp_read_json_file, 5},
{"_jsonify_source_tests", (DL_FUNC) &_jsonify_source_tests, 0},
{"_jsonify_rcpp_to_json", (DL_FUNC) &_jsonify_rcpp_to_json, 6},
{"_jsonify_rcpp_to_ndjson", (DL_FUNC) &_jsonify_rcpp_to_ndjson, 6},
{"_jsonify_rcpp_validate_json", (DL_FUNC) &_jsonify_rcpp_validate_json, 1},
{NULL, NULL, 0}
};
Expand Down
7 changes: 7 additions & 0 deletions src/to_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,10 @@ Rcpp::StringVector rcpp_to_json( SEXP lst, bool unbox = false, int digits = -1,
return jsonify::api::to_json( lst, unbox, digits, numeric_dates, factors_as_string, by );
}

// [[Rcpp::export]]
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"
) {

}

0 comments on commit 6d96ad7

Please sign in to comment.