Skip to content

Commit

Permalink
getting a bit messy
Browse files Browse the repository at this point in the history
  • Loading branch information
dcooley committed May 29, 2020
1 parent 5657962 commit 9bb30e3
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 28 deletions.
23 changes: 11 additions & 12 deletions inst/include/jsonify/from_json/from_json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#include <Rcpp.h>


#include "from_json_utils.hpp"
#include "simplify/simplify.hpp"

Expand All @@ -28,7 +27,7 @@ namespace from_json {
Rcpp::List out( json_length );

R_xlen_t i = 0;
for ( const auto& child : json.GetArray() ) {
for ( const auto& child : Rcpp::getArray( json ) ) {
out[ i++ ] = parse_json( child, simplify, fill_na ); // iterating here again makes another list
}
return out;
Expand All @@ -41,7 +40,7 @@ namespace from_json {
bool fill_na
) {

R_xlen_t json_length = json.Size();
R_xlen_t json_length = Rcpp::getSize( json );

if ( json_length == 0 ) {
return R_NilValue;
Expand All @@ -53,12 +52,12 @@ namespace from_json {

// https://github.com/Tencent/rapidjson/issues/162#issuecomment-341824061
#if __cplusplus >= 201703L
for ( const auto& [key, value] : json.GetObject() ) {
for ( const auto& [key, value] : Rcpp::getObject( json ) ) {
out[ i ] = parse_json( value, simplify, fill_na );
names[ i++ ] = std::string( key );
}
#else
for ( const auto& key_value : json.GetObject() ) {
for ( const auto& key_value : Rcpp::getObject( json ) ) {
out[ i ] = parse_json( key_value.value, simplify, fill_na );
names[ i++ ] = std::string( key_value.name.GetString() );
}
Expand All @@ -68,7 +67,7 @@ namespace from_json {
}

// const rapidjson::Value&
template< typename T >
template< typename T, typename N >
inline SEXP parse_json(
const T& json,
bool simplify,
Expand All @@ -77,9 +76,9 @@ namespace from_json {

std::unordered_set< int > dtypes;

R_xlen_t json_length = json.Size();
R_xlen_t json_length = Rcpp::getSize( json );

switch( json.GetType() ) {
switch( Rcpp::getType< >( json ) ) {

case rapidjson::kNullType: {
return R_NA_VAL;
Expand Down Expand Up @@ -109,10 +108,10 @@ namespace from_json {
dtypes = get_dtypes( json );

if( simplify && !contains_object_or_array( dtypes ) ) {
return array_to_vector( json.GetArray(), simplify );
return array_to_vector( Rcpp::getArray( json ), simplify );
} else {
Rcpp::List arr = parse_array( json, simplify, fill_na );
if( simplify) {
if( simplify ) {
return jsonify::from_json::simplify( arr, dtypes, json_length, fill_na );
} else {
return arr;
Expand All @@ -134,8 +133,8 @@ namespace from_json {
bool fill_na
) {

int json_type = json.GetType();
R_xlen_t json_length = json.Size();
int json_type = Rcpp::getType( json );
R_xlen_t json_length = Rcpp::getSize( json );

if(json_length == 0) {
if( json_type == 4 ) {
Expand Down
24 changes: 12 additions & 12 deletions inst/include/jsonify/from_json/simplify/simplify.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define R_JSONIFY_FROM_JSON_SIMPLIFY_H

#include "rapidjson/document.h"
#include "jsonify/jsonify_types.hpp"

namespace jsonify {
namespace from_json {
Expand Down Expand Up @@ -85,10 +86,10 @@ namespace from_json {

for( const auto& child : array ) {

switch( child.GetType() ) {
switch( Rcpp::getType( child ) ) {

// bool
case rapidjson::kFalseType: {}
case rapidjson::kFalseType : {}
case rapidjson::kTrueType: {
out[i] = Rcpp::wrap< bool >( child );
update_rtype< LGLSXP >( r_type );
Expand All @@ -102,17 +103,16 @@ namespace from_json {
break;
}

// numeric
// numeric
case rapidjson::kNumberType: {
if( child.IsDouble() ) {
// double
out[i] = Rcpp::wrap< double >( child );
update_rtype< REALSXP >( r_type );
} else {
// int
out[i] = Rcpp::wrap< int >( child );
update_rtype< INTSXP >( r_type );
}
if( Rcpp::isType< double >( child ) ) {
out[i] = Rcpp::wrap< double >( child );
update_rtype< REALSXP >( r_type );
} else {
// int
out[i] = Rcpp::wrap< int >( child );
update_rtype< INTSXP >( r_type );
}
break;
}

Expand Down
36 changes: 32 additions & 4 deletions inst/include/jsonify/jsonify_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@

namespace Rcpp {

// template<> SEXP wrap( std::basic_string& obj );
template< typename T > SEXP wrap( const rapidjson::Value& obj );
template< typename T > bool isType( const rapidjson::Value& obj );
template< typename T, typename N > T getType( const N& obj );
//rapidjson::Type getType( const rapidjson::Value& obj );
R_xlen_t getSize( const rapidjson::Value& obj );
rapidjson::Value::ConstArray getArray( const rapidjson::Value& obj );
rapidjson::Value::ConstObject getObject( const rapidjson::Value& obj );

namespace traits {

Expand All @@ -20,10 +25,35 @@ namespace traits {
namespace Rcpp {

template< typename T >
SEXP wrap( const rapidjson::Value& obj) {
SEXP wrap( const rapidjson::Value& obj ) {
auto b = obj.Get< T >();
return Rcpp::wrap( b );
}

template< typename T >
bool isType( const rapidjson::Value& obj ) {
return obj.Is< T >();
}

template< typename T, typename N > T getType( const N& object ) {

}

// inline rapidjson::Type getType( const rapidjson::Value& obj ) {
// return obj.GetType();
// }

inline rapidjson::Value::ConstArray getArray( const rapidjson::Value& obj ) {
return obj.GetArray();
}

inline rapidjson::Value::ConstObject getObject( const rapidjson::Value& obj ) {
return obj.GetObject();
}

inline R_xlen_t getSize( const rapidjson::Value& obj ) {
return obj.Size();
}


namespace traits {
Expand All @@ -32,6 +62,4 @@ namespace traits {
} // traits
} // Rcpp



#endif

0 comments on commit 9bb30e3

Please sign in to comment.