Skip to content

Commit dd7fd7d

Browse files
author
SymbolixAU
committed
a couple more tests
1 parent 134796e commit dd7fd7d

5 files changed

Lines changed: 14 additions & 107 deletions

File tree

.Rbuildignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
^codecov\.yml$
77
^tests/benchmarks.R
88
^cran-comments\.md$
9+
^docs/

inst/include/jsonify/to_json/writers.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,6 @@ namespace writers {
108108

109109
for ( int i = 0; i < n; i++ ) {
110110
if (Rcpp::LogicalVector::is_na( lv[i] ) ) {
111-
//Rcpp::Rcout << "NA logical found" << std::endl;
112-
// write_value( writer, "NA" );
113111
writer.Null();
114112
} else {
115113
bool l = lv[i]; // required for logical vectors

inst/include/jsonify/to_json/writers/complex.hpp

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,11 @@ namespace complex {
6464

6565
switch( TYPEOF( this_vec ) ) {
6666
case REALSXP: {
67-
// Rcpp::Rcout << "switch to num vector " << std::endl;
68-
// Rcpp::Rcout << "row: " << row << std::endl;
69-
// Rcpp::Rcout << "unbox: " << unbox << std::endl;
70-
// Rcpp::Rcout << "digits: " << digits << std::endl;
71-
// Rcpp::Rcout << "numeric dates: " << numeric_dates << std::endl;
7267
Rcpp::NumericVector nv = Rcpp::as< Rcpp::NumericVector >( this_vec );
7368
jsonify::writers::simple::write_value( writer, nv, row, digits, numeric_dates );
7469
break;
7570
}
7671
case INTSXP: {
77-
// Rcpp::Rcout << "switch to int vector " << std::endl;
7872
Rcpp::IntegerVector iv = Rcpp::as< Rcpp::IntegerVector >( this_vec );
7973
if( factors_as_string && Rf_isFactor( this_vec ) ) {
8074
Rcpp::CharacterVector lvls = iv.attr("levels");
@@ -88,7 +82,6 @@ namespace complex {
8882
} else {
8983
int this_int = iv[ row ];
9084
const char * this_char = lvls[ this_int -1 ];
91-
//jsonify::writers::simple::write_value( writer, this_char );
9285
writer.String( this_char );
9386
}
9487

@@ -156,15 +149,12 @@ namespace complex {
156149
int n_rows = df.nrows();
157150
Rcpp::StringVector column_names = df.names();
158151

159-
//writer.StartArray();
160-
161152
if ( by == "column") {
162153
writer.StartObject();
163154

164155
for( df_col = 0; df_col < n_cols; df_col++ ) {
165156

166157
const char *h = column_names[ df_col ];
167-
//jsonify::writers::simple::write_value( writer, h );
168158
writer.String( h );
169159
SEXP this_vec = df[ h ];
170160

@@ -215,7 +205,6 @@ namespace complex {
215205
for( df_col = 0; df_col < n_cols; df_col++ ) {
216206

217207
const char *h = column_names[ df_col ];
218-
//jsonify::writers::simple::write_value( writer, h );
219208
writer.String( h );
220209
SEXP this_vec = df[ h ];
221210

@@ -226,7 +215,6 @@ namespace complex {
226215
break;
227216
}
228217
default: {
229-
// Rcpp::Rcout << "default dates : " << numeric_dates << std::endl;
230218
switch_vector( writer, this_vec, unbox, digits, numeric_dates, factors_as_string, df_row );
231219
}
232220
}
@@ -236,15 +224,13 @@ namespace complex {
236224
writer.EndArray();
237225
} // end if
238226
}
239-
//writer.EndArray();
240227

241228
} else {
242229

243230
switch( TYPEOF( list_element ) ) {
244231

245232
case VECSXP: {
246-
// Rcpp::Rcout << "list elelment is another list " << std::endl;
247-
// TODO( handle the case where the list item is a row of a data.frame)
233+
// the case where the list item is a row of a data.frame
248234
// ISSUE #32
249235

250236
Rcpp::List temp_lst = Rcpp::as< Rcpp::List >( list_element );
@@ -296,7 +282,6 @@ namespace complex {
296282
SEXP recursive_list = lst[ i ];
297283
if ( has_names ) {
298284
const char *s = list_names[ i ];
299-
//jsonify::writers::simple::write_value( writer, s );
300285
writer.String( s );
301286
}
302287
write_value( writer, recursive_list, unbox, digits, numeric_dates, factors_as_string, by );

inst/include/jsonify/to_json/writers/simple.hpp

Lines changed: 1 addition & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ namespace simple {
5050
inline void write_value( Writer& writer, Rcpp::NumericVector& nv, bool unbox,
5151
int digits, bool numeric_dates ) {
5252

53-
// Rcpp::Rcout << "unbox function" << std::endl;
54-
5553
Rcpp::CharacterVector cls = jsonify::utils::getRClass( nv );
5654

5755
if( !numeric_dates && jsonify::dates::is_in( "Date", cls ) ) {
@@ -89,9 +87,6 @@ namespace simple {
8987
inline void write_value( Writer& writer, Rcpp::NumericVector& nv,
9088
int row, int digits, bool numeric_dates ) {
9189

92-
// Rcpp::Rcout << "row fucntion " << std::endl;
93-
// Rcpp::Rcout << "writing nv with row: " << row << std::endl;
94-
9590
Rcpp::CharacterVector cls = jsonify::utils::getRClass( nv );
9691

9792
if( !numeric_dates && jsonify::dates::is_in( "Date", cls ) ) {
@@ -169,7 +164,6 @@ namespace simple {
169164
inline void write_value( Writer& writer, Rcpp::IntegerVector& iv, int row,
170165
bool numeric_dates, bool factors_as_string ) {
171166

172-
// Rcpp::Rcout << "int vector" << std::endl;
173167
Rcpp::CharacterVector cls = jsonify::utils::getRClass( iv );
174168

175169

@@ -192,14 +186,11 @@ namespace simple {
192186
int ele = 0;
193187
write_value( writer, s, ele );
194188
} else {
195-
// Rcpp::Rcout << "writing lvls: " << lvls << std::endl;
196-
// Rcpp::Rcout << "row: " << row << std::endl;
197189
write_value( writer, lvls, row );
198190
}
199191

200192
} else {
201193

202-
// Rcpp::Rcout << "standard iv " << std::endl;
203194
if ( Rcpp::IntegerVector::is_na( iv[ row ] ) ) {
204195
writer.Null();
205196
} else {
@@ -268,34 +259,7 @@ namespace simple {
268259
}
269260
}
270261

271-
// template < typename Writer, typename T >
272-
// inline void write_value( Writer& writer, T& sexp, bool unbox,
273-
// int digits, bool numeric_dates ) {
274-
//
275-
// bool factors_as_string = true;
276-
// write_value( writer, sexp, unbox, digits, numeric_dates, factors_as_string );
277-
// }
278-
//
279-
// template < typename Writer, typename T >
280-
// inline void write_value( Writer& writer, T& sexp, bool unbox, int digits) {
281-
//
282-
// bool numeric_dates = true;
283-
// write_value( writer, sexp, unbox, digits, numeric_dates );
284-
// }
285-
//
286-
// template < typename Writer, typename T >
287-
// inline void write_value( Writer& writer, T& sexp, bool unbox ) {
288-
//
289-
// int digits = -1;
290-
// write_value( writer, sexp, unbox, digits );
291-
// }
292-
//
293-
// template < typename Writer, typename T >
294-
// inline void write_value( Writer& writer, T& sexp) {
295-
//
296-
// bool unbox = false;
297-
// write_value( writer, sexp, unbox );
298-
// }
262+
299263

300264
/*
301265
* template for R SEXPs for single-row from a vector
@@ -304,10 +268,6 @@ namespace simple {
304268
inline void write_value( Writer& writer, SEXP sexp, int row,
305269
int digits, bool numeric_dates, bool factors_as_string) {
306270

307-
//Rcpp::Rcout << "writing value: factors_as_string: " << factors_as_string << std::endl;
308-
309-
// Rcpp::Rcout << "writing value from vector" << std::endl;
310-
311271
switch( TYPEOF( sexp ) ) {
312272
case REALSXP: {
313273
Rcpp::NumericVector nv = Rcpp::as< Rcpp::NumericVector >( sexp );
@@ -336,54 +296,6 @@ namespace simple {
336296
}
337297
}
338298

339-
// template < typename Writer, typename T >
340-
// inline void write_value( Writer& writer, T& sexp, size_t row, bool unbox,
341-
// int digits, bool numeric_dates ) {
342-
//
343-
// bool factors_as_string = true;
344-
// write_value( writer, sexp, row, unbox, digits, numeric_dates, factors_as_string );
345-
// }
346-
//
347-
// template < typename Writer, typename T >
348-
// inline void write_value( Writer& writer, T& sexp, size_t row, bool unbox, int digits) {
349-
//
350-
// bool numeric_dates = true;
351-
// write_value( writer, sexp, row, unbox, digits, numeric_dates );
352-
// }
353-
//
354-
// template < typename Writer, typename T >
355-
// inline void write_value( Writer& writer, T& sexp, size_t row, bool unbox ) {
356-
//
357-
// int digits = -1;
358-
// write_value( writer, sexp, row, unbox, digits );
359-
// }
360-
//
361-
// template < typename Writer, typename T >
362-
// inline void write_value( Writer& writer, T& sexp, size_t row) {
363-
//
364-
// bool unbox = false;
365-
// write_value( writer, sexp, row, unbox );
366-
// }
367-
368-
369-
/*
370-
* template for C++ single object types
371-
*/
372-
// template < typename Writer, typename T >
373-
// inline void write_value( Writer& writer, T& val, int digits = -1 ) {
374-
// switch( TYPEOF( val ) ) {
375-
// case REALSXP: {
376-
// write_value( writer, val, digits );
377-
// break;
378-
// }
379-
// default: {
380-
// write_value( writer, val );
381-
// break;
382-
// }
383-
// }
384-
// }
385-
386-
387299
// ---------------------------------------------------------------------------
388300
// matrix values
389301
// ---------------------------------------------------------------------------

tests/testthat/test-to_json_lists.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,14 @@ test_that("data.frames inside lists are converted correctly", {
9090
expect_equal( as.character( js ), expected )
9191
})
9292

93+
test_that("factors in lists become characters", {
94+
95+
l <- list( x = as.factor( letters[1:3] ) )
96+
l2 <- list( x = letters[1:3] )
97+
js <- to_json( l )
98+
expect_true( js == to_json( l2 ) )
99+
100+
js <- to_json( l, factors_as_string = FALSE )
101+
l2 <- list( x = 1:3 )
102+
expect_true( js == to_json( l2 ) )
103+
})

0 commit comments

Comments
 (0)