From dd7fd7ddeba95e8c39e9d93564cf47c7efaaa384 Mon Sep 17 00:00:00 2001 From: SymbolixAU Date: Thu, 10 Jan 2019 13:31:02 +1100 Subject: [PATCH] a couple more tests --- .Rbuildignore | 1 + inst/include/jsonify/to_json/writers.hpp | 2 - .../jsonify/to_json/writers/complex.hpp | 17 +--- .../jsonify/to_json/writers/simple.hpp | 90 +------------------ tests/testthat/test-to_json_lists.R | 11 +++ 5 files changed, 14 insertions(+), 107 deletions(-) diff --git a/.Rbuildignore b/.Rbuildignore index 4237a46..87618ee 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -6,3 +6,4 @@ ^codecov\.yml$ ^tests/benchmarks.R ^cran-comments\.md$ +^docs/ \ No newline at end of file diff --git a/inst/include/jsonify/to_json/writers.hpp b/inst/include/jsonify/to_json/writers.hpp index 6bd69ac..544d621 100644 --- a/inst/include/jsonify/to_json/writers.hpp +++ b/inst/include/jsonify/to_json/writers.hpp @@ -108,8 +108,6 @@ namespace writers { for ( int i = 0; i < n; i++ ) { if (Rcpp::LogicalVector::is_na( lv[i] ) ) { - //Rcpp::Rcout << "NA logical found" << std::endl; - // write_value( writer, "NA" ); writer.Null(); } else { bool l = lv[i]; // required for logical vectors diff --git a/inst/include/jsonify/to_json/writers/complex.hpp b/inst/include/jsonify/to_json/writers/complex.hpp index 3868b62..76275ac 100644 --- a/inst/include/jsonify/to_json/writers/complex.hpp +++ b/inst/include/jsonify/to_json/writers/complex.hpp @@ -64,17 +64,11 @@ namespace complex { switch( TYPEOF( this_vec ) ) { case REALSXP: { - // Rcpp::Rcout << "switch to num vector " << std::endl; - // Rcpp::Rcout << "row: " << row << std::endl; - // Rcpp::Rcout << "unbox: " << unbox << std::endl; - // Rcpp::Rcout << "digits: " << digits << std::endl; - // Rcpp::Rcout << "numeric dates: " << numeric_dates << std::endl; Rcpp::NumericVector nv = Rcpp::as< Rcpp::NumericVector >( this_vec ); jsonify::writers::simple::write_value( writer, nv, row, digits, numeric_dates ); break; } case INTSXP: { - // Rcpp::Rcout << "switch to int vector " << std::endl; Rcpp::IntegerVector iv = Rcpp::as< Rcpp::IntegerVector >( this_vec ); if( factors_as_string && Rf_isFactor( this_vec ) ) { Rcpp::CharacterVector lvls = iv.attr("levels"); @@ -88,7 +82,6 @@ namespace complex { } else { int this_int = iv[ row ]; const char * this_char = lvls[ this_int -1 ]; - //jsonify::writers::simple::write_value( writer, this_char ); writer.String( this_char ); } @@ -156,15 +149,12 @@ namespace complex { int n_rows = df.nrows(); Rcpp::StringVector column_names = df.names(); - //writer.StartArray(); - if ( by == "column") { writer.StartObject(); for( df_col = 0; df_col < n_cols; df_col++ ) { const char *h = column_names[ df_col ]; - //jsonify::writers::simple::write_value( writer, h ); writer.String( h ); SEXP this_vec = df[ h ]; @@ -215,7 +205,6 @@ namespace complex { for( df_col = 0; df_col < n_cols; df_col++ ) { const char *h = column_names[ df_col ]; - //jsonify::writers::simple::write_value( writer, h ); writer.String( h ); SEXP this_vec = df[ h ]; @@ -226,7 +215,6 @@ namespace complex { break; } default: { - // Rcpp::Rcout << "default dates : " << numeric_dates << std::endl; switch_vector( writer, this_vec, unbox, digits, numeric_dates, factors_as_string, df_row ); } } @@ -236,15 +224,13 @@ namespace complex { writer.EndArray(); } // end if } - //writer.EndArray(); } else { switch( TYPEOF( list_element ) ) { case VECSXP: { - // Rcpp::Rcout << "list elelment is another list " << std::endl; - // TODO( handle the case where the list item is a row of a data.frame) + // the case where the list item is a row of a data.frame // ISSUE #32 Rcpp::List temp_lst = Rcpp::as< Rcpp::List >( list_element ); @@ -296,7 +282,6 @@ namespace complex { SEXP recursive_list = lst[ i ]; if ( has_names ) { const char *s = list_names[ i ]; - //jsonify::writers::simple::write_value( writer, s ); writer.String( s ); } write_value( writer, recursive_list, unbox, digits, numeric_dates, factors_as_string, by ); diff --git a/inst/include/jsonify/to_json/writers/simple.hpp b/inst/include/jsonify/to_json/writers/simple.hpp index b57958e..c977e5a 100644 --- a/inst/include/jsonify/to_json/writers/simple.hpp +++ b/inst/include/jsonify/to_json/writers/simple.hpp @@ -50,8 +50,6 @@ namespace simple { inline void write_value( Writer& writer, Rcpp::NumericVector& nv, bool unbox, int digits, bool numeric_dates ) { - // Rcpp::Rcout << "unbox function" << std::endl; - Rcpp::CharacterVector cls = jsonify::utils::getRClass( nv ); if( !numeric_dates && jsonify::dates::is_in( "Date", cls ) ) { @@ -89,9 +87,6 @@ namespace simple { inline void write_value( Writer& writer, Rcpp::NumericVector& nv, int row, int digits, bool numeric_dates ) { - // Rcpp::Rcout << "row fucntion " << std::endl; - // Rcpp::Rcout << "writing nv with row: " << row << std::endl; - Rcpp::CharacterVector cls = jsonify::utils::getRClass( nv ); if( !numeric_dates && jsonify::dates::is_in( "Date", cls ) ) { @@ -169,7 +164,6 @@ namespace simple { inline void write_value( Writer& writer, Rcpp::IntegerVector& iv, int row, bool numeric_dates, bool factors_as_string ) { - // Rcpp::Rcout << "int vector" << std::endl; Rcpp::CharacterVector cls = jsonify::utils::getRClass( iv ); @@ -192,14 +186,11 @@ namespace simple { int ele = 0; write_value( writer, s, ele ); } else { - // Rcpp::Rcout << "writing lvls: " << lvls << std::endl; - // Rcpp::Rcout << "row: " << row << std::endl; write_value( writer, lvls, row ); } } else { - // Rcpp::Rcout << "standard iv " << std::endl; if ( Rcpp::IntegerVector::is_na( iv[ row ] ) ) { writer.Null(); } else { @@ -268,34 +259,7 @@ namespace simple { } } - // template < typename Writer, typename T > - // inline void write_value( Writer& writer, T& sexp, bool unbox, - // int digits, bool numeric_dates ) { - // - // bool factors_as_string = true; - // write_value( writer, sexp, unbox, digits, numeric_dates, factors_as_string ); - // } - // - // template < typename Writer, typename T > - // inline void write_value( Writer& writer, T& sexp, bool unbox, int digits) { - // - // bool numeric_dates = true; - // write_value( writer, sexp, unbox, digits, numeric_dates ); - // } - // - // template < typename Writer, typename T > - // inline void write_value( Writer& writer, T& sexp, bool unbox ) { - // - // int digits = -1; - // write_value( writer, sexp, unbox, digits ); - // } - // - // template < typename Writer, typename T > - // inline void write_value( Writer& writer, T& sexp) { - // - // bool unbox = false; - // write_value( writer, sexp, unbox ); - // } + /* * template for R SEXPs for single-row from a vector @@ -304,10 +268,6 @@ namespace simple { inline void write_value( Writer& writer, SEXP sexp, int row, int digits, bool numeric_dates, bool factors_as_string) { - //Rcpp::Rcout << "writing value: factors_as_string: " << factors_as_string << std::endl; - - // Rcpp::Rcout << "writing value from vector" << std::endl; - switch( TYPEOF( sexp ) ) { case REALSXP: { Rcpp::NumericVector nv = Rcpp::as< Rcpp::NumericVector >( sexp ); @@ -336,54 +296,6 @@ namespace simple { } } - // template < typename Writer, typename T > - // inline void write_value( Writer& writer, T& sexp, size_t row, bool unbox, - // int digits, bool numeric_dates ) { - // - // bool factors_as_string = true; - // write_value( writer, sexp, row, unbox, digits, numeric_dates, factors_as_string ); - // } - // - // template < typename Writer, typename T > - // inline void write_value( Writer& writer, T& sexp, size_t row, bool unbox, int digits) { - // - // bool numeric_dates = true; - // write_value( writer, sexp, row, unbox, digits, numeric_dates ); - // } - // - // template < typename Writer, typename T > - // inline void write_value( Writer& writer, T& sexp, size_t row, bool unbox ) { - // - // int digits = -1; - // write_value( writer, sexp, row, unbox, digits ); - // } - // - // template < typename Writer, typename T > - // inline void write_value( Writer& writer, T& sexp, size_t row) { - // - // bool unbox = false; - // write_value( writer, sexp, row, unbox ); - // } - - - /* - * template for C++ single object types - */ - // template < typename Writer, typename T > - // inline void write_value( Writer& writer, T& val, int digits = -1 ) { - // switch( TYPEOF( val ) ) { - // case REALSXP: { - // write_value( writer, val, digits ); - // break; - // } - // default: { - // write_value( writer, val ); - // break; - // } - // } - // } - - // --------------------------------------------------------------------------- // matrix values // --------------------------------------------------------------------------- diff --git a/tests/testthat/test-to_json_lists.R b/tests/testthat/test-to_json_lists.R index 5850333..d650c0f 100644 --- a/tests/testthat/test-to_json_lists.R +++ b/tests/testthat/test-to_json_lists.R @@ -90,3 +90,14 @@ test_that("data.frames inside lists are converted correctly", { expect_equal( as.character( js ), expected ) }) +test_that("factors in lists become characters", { + + l <- list( x = as.factor( letters[1:3] ) ) + l2 <- list( x = letters[1:3] ) + js <- to_json( l ) + expect_true( js == to_json( l2 ) ) + + js <- to_json( l, factors_as_string = FALSE ) + l2 <- list( x = 1:3 ) + expect_true( js == to_json( l2 ) ) +}) \ No newline at end of file