From 9bb30e306a2060432a0dcd9f423b055f4251898a Mon Sep 17 00:00:00 2001 From: David Date: Fri, 29 May 2020 16:31:14 +1000 Subject: [PATCH] getting a bit messy --- inst/include/jsonify/from_json/from_json.hpp | 23 ++++++------ .../jsonify/from_json/simplify/simplify.hpp | 24 ++++++------- inst/include/jsonify/jsonify_types.hpp | 36 ++++++++++++++++--- 3 files changed, 55 insertions(+), 28 deletions(-) diff --git a/inst/include/jsonify/from_json/from_json.hpp b/inst/include/jsonify/from_json/from_json.hpp index bb0df08..067bcea 100644 --- a/inst/include/jsonify/from_json/from_json.hpp +++ b/inst/include/jsonify/from_json/from_json.hpp @@ -5,7 +5,6 @@ #include - #include "from_json_utils.hpp" #include "simplify/simplify.hpp" @@ -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; @@ -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; @@ -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() ); } @@ -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, @@ -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; @@ -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; @@ -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 ) { diff --git a/inst/include/jsonify/from_json/simplify/simplify.hpp b/inst/include/jsonify/from_json/simplify/simplify.hpp index ca2b7b2..559e511 100644 --- a/inst/include/jsonify/from_json/simplify/simplify.hpp +++ b/inst/include/jsonify/from_json/simplify/simplify.hpp @@ -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 { @@ -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 ); @@ -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; } diff --git a/inst/include/jsonify/jsonify_types.hpp b/inst/include/jsonify/jsonify_types.hpp index 8bf0077..14e4156 100644 --- a/inst/include/jsonify/jsonify_types.hpp +++ b/inst/include/jsonify/jsonify_types.hpp @@ -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 { @@ -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 { @@ -32,6 +62,4 @@ namespace traits { } // traits } // Rcpp - - #endif \ No newline at end of file