Skip to content

Commit

Permalink
a couple more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SymbolixAU committed Jan 10, 2019
1 parent 134796e commit dd7fd7d
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 107 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
^codecov\.yml$
^tests/benchmarks.R
^cran-comments\.md$
^docs/
2 changes: 0 additions & 2 deletions inst/include/jsonify/to_json/writers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 1 addition & 16 deletions inst/include/jsonify/to_json/writers/complex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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 );
}

Expand Down Expand Up @@ -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 ];

Expand Down Expand Up @@ -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 ];

Expand All @@ -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 );
}
}
Expand All @@ -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 );
Expand Down Expand Up @@ -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 );
Expand Down
90 changes: 1 addition & 89 deletions inst/include/jsonify/to_json/writers/simple.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ) {
Expand Down Expand Up @@ -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 ) ) {
Expand Down Expand Up @@ -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 );


Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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 );
Expand Down Expand Up @@ -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
// ---------------------------------------------------------------------------
Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/test-to_json_lists.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) )
})

0 comments on commit dd7fd7d

Please sign in to comment.