Skip to content

Commit

Permalink
debugging #58
Browse files Browse the repository at this point in the history
  • Loading branch information
dcooley committed Feb 4, 2020
1 parent 167a453 commit 0c0e7ef
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 106 deletions.
8 changes: 4 additions & 4 deletions R/scratch2.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
#
# json <- "~/Desktop/google_timeline/Takeout/Location History/Location History.json"
#
# js <- jsonlite::fromJSON(json)
# # js <- jsonlite::fromJSON(json)
# #
# # js2 <- jsonify::from_json(json)
#
# json_small <- "~/Desktop/google_timeline/Takeout/Location History/Location History Small.json"
# jfy <- from_json( json_small, simplify = T, fill_na = T)
# jlt <- fromJSON( json_small )
#
# ls <- readLines(json)
#
# js <- paste0(ls, collapse = "")
#
# #jsonify::validate_json( js )
#
# json <- "~/Desktop/google_timeline/Takeout/Location History/Location History Reducing.json"
# ls <- readLines(json); js <- paste0(ls, collapse = "")
#
# res <- jsonify::from_json( js, fill_na = T )
#
6 changes: 2 additions & 4 deletions inst/include/jsonify/from_json/api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace api {
//' @export
inline SEXP from_json(rapidjson::Value& doc, bool& simplify, bool& fill_na ) {

int sequential_array_counter = 0;
R_xlen_t sequential_array_counter = 0;

// If the input is a scalar value of type int, double, string, or bool,
// return Rcpp vector with length 1.
Expand Down Expand Up @@ -44,9 +44,7 @@ namespace api {
return x;
}

R_xlen_t depth = 0;

return jsonify::from_json::json_to_sexp( doc, simplify, fill_na, sequential_array_counter, depth );
return jsonify::from_json::json_to_sexp( doc, simplify, fill_na, sequential_array_counter );
}

inline SEXP from_json( const char* json, bool& simplify, bool& fill_na ) {
Expand Down
15 changes: 6 additions & 9 deletions inst/include/jsonify/from_json/from_json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ namespace from_json {
const rapidjson::Value& json,
bool& simplify,
bool& fill_na,
int sequential_array_counter,
R_xlen_t& depth
R_xlen_t sequential_array_counter
) {

Rcpp::List res(1);


int json_type = json.GetType();
int json_length = json.Size();
R_xlen_t json_length = json.Size();

if(json_length == 0) {
if( json_type == 4 ) {
Expand All @@ -34,8 +33,6 @@ namespace from_json {


R_xlen_t i;

depth = depth + 1;
//Rcpp::Rcout << "depth: " << depth << std::endl;
//Rcpp::Rcout << "i: " << i << std::endl;

Expand Down Expand Up @@ -91,11 +88,11 @@ namespace from_json {
// array
case rapidjson::kArrayType: {
const rapidjson::Value& temp_array = itr->value;
out[i] = json_to_sexp( temp_array, simplify, fill_na, sequential_array_counter, depth );
out[i] = json_to_sexp( temp_array, simplify, fill_na, sequential_array_counter );
break;
}
case rapidjson::kObjectType: {
out[i] = json_to_sexp( itr->value, simplify, fill_na, sequential_array_counter, depth );
out[i] = json_to_sexp( itr->value, simplify, fill_na, sequential_array_counter );
break;
}

Expand Down Expand Up @@ -164,15 +161,15 @@ namespace from_json {
}
// array
case rapidjson::kArrayType: {
array_of_array[i] = json_to_sexp( json[i], simplify, fill_na, sequential_array_counter, depth );
array_of_array[i] = json_to_sexp( json[i], simplify, fill_na, sequential_array_counter );
sequential_array_counter++;
break;
}
// object
case rapidjson::kObjectType: {
sequential_array_counter = 0;
const rapidjson::Value& temp_val = json[i];
array_of_array[i] = json_to_sexp( temp_val, simplify, fill_na, sequential_array_counter, depth );
array_of_array[i] = json_to_sexp( temp_val, simplify, fill_na, sequential_array_counter );
break;
}
default: {
Expand Down
36 changes: 19 additions & 17 deletions inst/include/jsonify/from_json/from_json_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
namespace jsonify {
namespace from_json {

inline int where_is(
Rcpp::String to_find,
Rcpp::StringVector& sv ) {
int n = sv.size();
int i;
inline R_xlen_t where_is(
Rcpp::String to_find,
Rcpp::StringVector& sv
) {
R_xlen_t n = sv.size();
R_xlen_t i;
for( i = 0; i < n; i++ ) {
if ( to_find == sv[i] ) {
return i;
Expand All @@ -32,10 +33,11 @@ namespace from_json {

std::unordered_set< int > dtypes;

int doc_len = doc.Size();
R_xlen_t doc_len = doc.Size();

int curr_dtype;
for(int i = 0; i < doc_len; ++i) {
R_xlen_t i;
for(i = 0; i < doc_len; ++i) {
curr_dtype = doc[i].GetType();
// rapidjson uses separate ints for types true (2) and false (1)...combine
// them into one value such that bool is 1.
Expand Down Expand Up @@ -105,18 +107,18 @@ namespace from_json {
return 0;
}

// Convert all NULL elements in a list to NA.
inline void null_to_na(Rcpp::List& x) {
for(unsigned int i = 0; i < x.size(); ++i) {
if(Rf_isNull(x[i])) {
x[i] = R_NA_VAL;
}
}
}
// // Convert all NULL elements in a list to NA.
// inline void null_to_na(Rcpp::List& x) {
// for(unsigned int i = 0; i < x.size(); ++i) {
// if(Rf_isNull(x[i])) {
// x[i] = R_NA_VAL;
// }
// }
// }

// returns -1 if doens't exist
// else the stored r_type
inline int column_value(
inline R_xlen_t column_value(
std::unordered_map< std::string, int >& column_map,
const char* to_find
) {
Expand All @@ -125,7 +127,7 @@ namespace from_json {
it = column_map.find( str );

if( it != column_map.end() ) {
int res = it->second;
R_xlen_t res = it->second;
return res;
}
return -1;
Expand Down
54 changes: 34 additions & 20 deletions inst/include/jsonify/from_json/simplify/simplify.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ namespace from_json {

inline SEXP simplify_matrix(
Rcpp::List& out,
int& n_col,
int& n_row,
R_xlen_t& n_col,
R_xlen_t& n_row,
int& r_type
) {

Expand All @@ -168,10 +168,13 @@ namespace from_json {
// } else {

Rcpp::IntegerMatrix mat( n_row, n_col );
for( int i = 0; i < n_row; i++ ) {
R_xlen_t i;
R_xlen_t j;

for( i = 0; i < n_row; i++ ) {
Rcpp::IntegerVector this_vec = out[i];
for( int j = 0; j < n_col; j++ ) {
int this_val = this_vec[j];
for( j = 0; j < n_col; j++ ) {
R_xlen_t this_val = this_vec[j];
mat( i, j ) = this_val;
}
}
Expand All @@ -190,9 +193,12 @@ namespace from_json {
// } else {

Rcpp::NumericMatrix mat( n_row, n_col );
for( int i = 0; i < n_row; i++ ) {
R_xlen_t i;
R_xlen_t j;

for( i = 0; i < n_row; i++ ) {
Rcpp::NumericVector this_vec = out[i];
for( int j = 0; j < n_col; j++ ) {
for( j = 0; j < n_col; j++ ) {
double this_val = this_vec[j];
mat( i, j ) = this_val;
}
Expand All @@ -212,9 +218,12 @@ namespace from_json {
// } else {

Rcpp::LogicalMatrix mat( n_row, n_col );
for( int i = 0; i < n_row; i++ ) {
R_xlen_t i;
R_xlen_t j;

for( i = 0; i < n_row; i++ ) {
Rcpp::LogicalVector this_vec = out[i];
for( int j = 0; j < n_col; j++ ) {
for( j = 0; j < n_col; j++ ) {
bool this_val = this_vec[j];
mat( i, j ) = this_val;
}
Expand All @@ -238,9 +247,12 @@ namespace from_json {
// return mat;
// } else {
Rcpp::StringMatrix mat( n_row, n_col );
for( int i = 0; i < n_row; i++ ) {
R_xlen_t i;
R_xlen_t j;

for( i = 0; i < n_row; i++ ) {
Rcpp::StringVector this_vec = out[i];
for( int j = 0; j < n_col; j++ ) {
for( j = 0; j < n_col; j++ ) {
Rcpp::String this_val = this_vec[j];
mat( i, j ) = this_val;
}
Expand All @@ -260,6 +272,7 @@ namespace from_json {
std::unordered_set<int> array_lengths;
std::unordered_set<int> array_types;
bool can_be_matrix = true;

for( j = 0; j < n; j++ ) {
SEXP s = array_of_array[j];
int this_type = TYPEOF( s );
Expand All @@ -281,8 +294,9 @@ namespace from_json {
if( can_be_matrix ) {
Rcpp::IntegerVector arr_types( array_types.begin(), array_types.end() );
int r_type = Rcpp::max( arr_types );
int n_col = *array_lengths.begin(); // only one sizez
int n_row = n;
R_xlen_t n_col = *array_lengths.begin(); // only one sizez
R_xlen_t n_row = n;

return jsonify::from_json::simplify_matrix( array_of_array, n_col, n_row, r_type );
} else {
return array_of_array;
Expand Down Expand Up @@ -437,7 +451,7 @@ namespace from_json {

inline SEXP simplify_dataframe_fill_na(
Rcpp::List& out,
int& doc_len
R_xlen_t& doc_len
) {

// the number of rows is equal to the number of list elements?
Expand All @@ -454,10 +468,10 @@ namespace from_json {
std::unordered_map< std::string, int > column_lengths;

int struct_type;
int sexp_length;
R_xlen_t sexp_length;
int tp;
int st;
int ln;
R_xlen_t ln;

Rcpp::StringVector list_names;
std::vector< std::string > column_names;
Expand Down Expand Up @@ -569,7 +583,7 @@ namespace from_json {
// iff any column lenghts are different, it's a list
inline SEXP simplify_dataframe(
Rcpp::List& out,
int& doc_len
R_xlen_t& doc_len
) {

// the number of rows is equal to the number of list elements?
Expand All @@ -586,10 +600,10 @@ namespace from_json {
std::unordered_map< std::string, int > column_lengths;

int struct_type;
int sexp_length;
R_xlen_t sexp_length;
int tp;
int st;
int ln;
R_xlen_t ln;

Rcpp::StringVector list_names;

Expand All @@ -611,7 +625,7 @@ namespace from_json {
for( j = 0; j < list_size; j++ ) {
const char* this_name = list_names[j];
Rcpp::StringVector these_names = this_list.names();
int found_name = where_is( this_name, these_names );
R_xlen_t found_name = where_is( this_name, these_names );

if( found_name == -1 ) {
// can't simplify
Expand Down
20 changes: 10 additions & 10 deletions inst/include/jsonify/to_json/dates/dates.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ namespace jsonify {
namespace dates {

inline bool is_in( const char* x, Rcpp::CharacterVector v ) {
int n = v.size();
int i;
R_xlen_t n = v.size();
R_xlen_t i;
for( i = 0; i < n; i++ ) {
if( v[i] == x ) {
return true;
Expand All @@ -22,8 +22,8 @@ namespace dates {

inline Rcpp::StringVector date_to_string( Rcpp::IntegerVector& iv ) {

int i;
int n = iv.size();
R_xlen_t i;
R_xlen_t n = iv.size();
Rcpp::StringVector sv( n );

for ( i = 0; i < n; i++ ) {
Expand All @@ -37,8 +37,8 @@ namespace dates {

inline Rcpp::StringVector date_to_string( Rcpp::NumericVector& nv ) {

int i;
int n = nv.size();
R_xlen_t i;
R_xlen_t n = nv.size();
Rcpp::StringVector sv( n );

for ( i = 0; i < n; i++ ) {
Expand All @@ -52,8 +52,8 @@ namespace dates {

inline Rcpp::StringVector posixct_to_string( Rcpp::IntegerVector& iv ) {

int i;
int n = iv.size();
R_xlen_t i;
R_xlen_t n = iv.size();

Rcpp::StringVector sv( n );

Expand All @@ -75,8 +75,8 @@ namespace dates {

inline Rcpp::StringVector posixct_to_string( Rcpp::NumericVector& nv ) {

int i;
int n = nv.size();
R_xlen_t i;
R_xlen_t n = nv.size();

boost::local_time::tz_database tz_db;

Expand Down
Loading

0 comments on commit 0c0e7ef

Please sign in to comment.