diff --git a/R/RcppExports.R b/R/RcppExports.R index f855bc5..521f45e 100644 --- a/R/RcppExports.R +++ b/R/RcppExports.R @@ -5,10 +5,6 @@ rcpp_from_json <- function(json, simplify, fill_na) { .Call(`_jsonify_rcpp_from_json`, json, simplify, fill_na) } -rcpp_parse_json <- function(json) { - .Call(`_jsonify_rcpp_parse_json`, json) -} - rcpp_from_ndjson <- function(ndjson, simplify, fill_na) { .Call(`_jsonify_rcpp_from_ndjson`, ndjson, simplify, fill_na) } diff --git a/inst/include/jsonify/from_json/api.hpp b/inst/include/jsonify/from_json/api.hpp index d6f03a9..ebf4c03 100644 --- a/inst/include/jsonify/from_json/api.hpp +++ b/inst/include/jsonify/from_json/api.hpp @@ -3,27 +3,10 @@ #include #include "jsonify/from_json/from_json.hpp" -#include "jsonify/from_json/parse_json.hpp" namespace jsonify { namespace api { - inline SEXP parse_json(const char* json ) { - - rapidjson::Document doc; - doc.Parse(json); - - // Make sure there were no parse errors - if(doc.HasParseError()) { - Rcpp::stop("json parse error"); - } - - // If the input is a scalar value of type int, double, string, or bool, - // return Rcpp vector with length 1. - return jsonify::parse_json::parse_json< rapidjson::Document >( doc ); - - } - //' Parse JSON String //' //' Takes a JSON string as input, returns an R list of key-value pairs diff --git a/inst/include/jsonify/from_json/from_json.hpp b/inst/include/jsonify/from_json/from_json.hpp index 067bcea..3d4bb50 100644 --- a/inst/include/jsonify/from_json/from_json.hpp +++ b/inst/include/jsonify/from_json/from_json.hpp @@ -1,7 +1,7 @@ #ifndef R_JSONIFY_FROM_JSON_H #define R_JSONIFY_FROM_JSON_H -#include "jsonify/jsonify_types.hpp" +//#include "jsonify/jsonify_types.hpp" #include @@ -16,6 +16,15 @@ namespace from_json { template< typename T > SEXP parse_object( const T& json, bool simplify, bool fill_na ); template< typename T > SEXP parse_array( const T& json, bool simplify, bool fill_na ); + template< typename T > int getType( T& obj ); + template< typename T, N > N getObject( T& obj ); + template< typename T, N > N getArray( T& obj ); + template< typename T > R_xlen_t jsonLength( T& obj ); + + template< typename T > bool getBool( T& obj ); + template< typename T > double getDouble( T& obj ); + template< typename T > int getInt( T& obj ); + template< typename T > inline SEXP parse_array( const T& json, @@ -23,11 +32,11 @@ namespace from_json { bool fill_na ) { - R_xlen_t json_length = json.Size(); + R_xlen_t json_length = jsonLength( json ); Rcpp::List out( json_length ); R_xlen_t i = 0; - for ( const auto& child : Rcpp::getArray( json ) ) { + for ( const auto& child : getArray( json ) ) { out[ i++ ] = parse_json( child, simplify, fill_na ); // iterating here again makes another list } return out; @@ -40,7 +49,7 @@ namespace from_json { bool fill_na ) { - R_xlen_t json_length = Rcpp::getSize( json ); + R_xlen_t json_length = jsonLength( json ); if ( json_length == 0 ) { return R_NilValue; @@ -52,12 +61,12 @@ namespace from_json { // https://github.com/Tencent/rapidjson/issues/162#issuecomment-341824061 #if __cplusplus >= 201703L - for ( const auto& [key, value] : Rcpp::getObject( json ) ) { + for ( const auto& [key, value] : getObject( json ) ) { out[ i ] = parse_json( value, simplify, fill_na ); names[ i++ ] = std::string( key ); } #else - for ( const auto& key_value : Rcpp::getObject( json ) ) { + for ( const auto& key_value : getObject( json ) ) { out[ i ] = parse_json( key_value.value, simplify, fill_na ); names[ i++ ] = std::string( key_value.name.GetString() ); } @@ -67,7 +76,7 @@ namespace from_json { } // const rapidjson::Value& - template< typename T, typename N > + template< typename T> inline SEXP parse_json( const T& json, bool simplify, @@ -76,9 +85,9 @@ namespace from_json { std::unordered_set< int > dtypes; - R_xlen_t json_length = Rcpp::getSize( json ); + R_xlen_t json_length = jsonLength( json ); - switch( Rcpp::getType< >( json ) ) { + switch( getType( json ) ) { case rapidjson::kNullType: { return R_NA_VAL; @@ -87,17 +96,19 @@ namespace from_json { case rapidjson::kFalseType: {} case rapidjson::kTrueType: { //return Rcpp::wrap< bool >( json.GetBool() ); - return Rcpp::wrap< bool >( json ); + return Rcpp::wrap< bool >( getBool( json ) ); } case rapidjson::kStringType: { - return Rcpp::wrap< const char* >( json ); + Rcpp::wrap( Rcpp::String( json.GetString() ) ); + + //return Rcpp::wrap< const char* >( getChar( json ) ); } // numeric case rapidjson::kNumberType: { if( json.IsDouble() ) { - return Rcpp::wrap< double >( json ); + return Rcpp::wrap< double >( getDouble( json ) ); } else { - return Rcpp::wrap< int >( json ); + return Rcpp::wrap< int >( getInt( json ) ); } } case rapidjson::kObjectType: { @@ -108,7 +119,7 @@ namespace from_json { dtypes = get_dtypes( json ); if( simplify && !contains_object_or_array( dtypes ) ) { - return array_to_vector( Rcpp::getArray( json ), simplify ); + return array_to_vector( getArray( json ), simplify ); } else { Rcpp::List arr = parse_array( json, simplify, fill_na ); if( simplify ) { @@ -133,8 +144,8 @@ namespace from_json { bool fill_na ) { - int json_type = Rcpp::getType( json ); - R_xlen_t json_length = Rcpp::getSize( json ); + int json_type = getType( json ); + R_xlen_t json_length = jsonLength( json ); if(json_length == 0) { if( json_type == 4 ) { diff --git a/inst/include/jsonify/from_json/parse_json.hpp b/inst/include/jsonify/from_json/parse_json.hpp deleted file mode 100644 index 5044069..0000000 --- a/inst/include/jsonify/from_json/parse_json.hpp +++ /dev/null @@ -1,97 +0,0 @@ -#ifndef R_JSONIFY_PARSE_JSON_H -#define R_JSONIFY_PARSE_JSON_H - -#include - -#include "from_json_utils.hpp" -#include "simplify/simplify.hpp" - - -namespace jsonify { -namespace parse_json { - - template< typename T > SEXP parse_json( const T& json ); - template< typename T > SEXP parse_object( const T& json ); - template< typename T > SEXP parse_array( const T& json ); - - template< typename T > - inline SEXP parse_array( const T& json ) { - - R_xlen_t json_length = json.Size(); - Rcpp::List out( json_length ); - - R_xlen_t i = 0; - for ( auto& child : json.GetArray() ) { - out[ i++ ] = parse_json( child ); - } - return out; - } - - template< typename T > - inline SEXP parse_object( const T& json ) { - - R_xlen_t json_length = json.Size(); - - Rcpp::List out( json_length ); - Rcpp::CharacterVector names( json_length ); - R_xlen_t i = 0; - - // https://github.com/Tencent/rapidjson/issues/162#issuecomment-341824061 -#if __cplusplus >= 201703L - for ( auto& [key, value] : json.GetObject() ) { - out[ i ] = parse_json( value ); - names[ i++ ] = std::string( key ); - } -#else - for ( auto& key_value : json.GetObject() ) { - out[ i ] = parse_json( key_value.value ); - names[ i++ ] = std::string( key_value.name.GetString() ); - } -#endif - out.attr("names") = names; - return out; - } - - template< typename T > - inline SEXP parse_json( const T& json ) { - - switch( json.GetType() ) { - - case rapidjson::kNullType: { - return R_NA_VAL; - } - case rapidjson::kFalseType: {} - case rapidjson::kTrueType: { - return Rcpp::wrap< bool >( json.GetBool() ); - } - case rapidjson::kStringType: { - return Rcpp::wrap( std::string( json.GetString() ) ); - } - // numeric - case rapidjson::kNumberType: { - if(json.IsDouble()) { - // double - return Rcpp::wrap< double >( json.GetDouble() ); - } else { - // int - return Rcpp::wrap< int >( json.GetInt() ); - } - } - case rapidjson::kObjectType: { - return parse_object( json ); - } - case rapidjson::kArrayType: { - return parse_array( json ); - } - default: { - Rcpp::stop("jsonify - case not handled"); - } - } - return R_NilValue; - } - - -} // namespace from_json -} // namespace jsonify - -#endif diff --git a/inst/include/jsonify/from_json/simplify/simplify.hpp b/inst/include/jsonify/from_json/simplify/simplify.hpp index 559e511..db07a5f 100644 --- a/inst/include/jsonify/from_json/simplify/simplify.hpp +++ b/inst/include/jsonify/from_json/simplify/simplify.hpp @@ -2,7 +2,7 @@ #define R_JSONIFY_FROM_JSON_SIMPLIFY_H #include "rapidjson/document.h" -#include "jsonify/jsonify_types.hpp" +// #include "jsonify/jsonify_types.hpp" namespace jsonify { namespace from_json { @@ -86,7 +86,7 @@ namespace from_json { for( const auto& child : array ) { - switch( Rcpp::getType( child ) ) { + switch( child.GetType() ) { // bool case rapidjson::kFalseType : {} @@ -105,7 +105,8 @@ namespace from_json { // numeric case rapidjson::kNumberType: { - if( Rcpp::isType< double >( child ) ) { + //if( Rcpp::isType< double >( child ) ) { + if( child.IsDouble() ) { out[i] = Rcpp::wrap< double >( child ); update_rtype< REALSXP >( r_type ); } else { diff --git a/inst/include/jsonify/jsonify.hpp b/inst/include/jsonify/jsonify.hpp index 55b0b86..0470ec5 100644 --- a/inst/include/jsonify/jsonify.hpp +++ b/inst/include/jsonify/jsonify.hpp @@ -1,7 +1,7 @@ #ifndef R_JSONIFY_H #define R_JSONIFY_H -#include "jsonify_types.hpp" +// #include "jsonify_types.hpp" #include "rapidjson/writer.h" #include "rapidjson/stringbuffer.h" diff --git a/inst/include/jsonify/jsonify_types.hpp b/inst/include/jsonify/jsonify_types.hpp index 14e4156..d9b00e4 100644 --- a/inst/include/jsonify/jsonify_types.hpp +++ b/inst/include/jsonify/jsonify_types.hpp @@ -8,12 +8,12 @@ namespace Rcpp { 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 ); + // 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 { @@ -30,30 +30,22 @@ namespace Rcpp { 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 ) { - - } - + // 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(); - } + // + // inline R_xlen_t getSize( const rapidjson::Value& obj ) { + // return obj.Size(); + // } namespace traits { @@ -62,4 +54,44 @@ namespace traits { } // traits } // Rcpp +template < typename T > +inline R_xlen_t jsonLength( T& obj ) { + return obj.Size(); +} + +template< typename T > +inline rapidjson::Value::ConstArray getArray( T& obj ) { + return obj.GetArray(); +} + +template< typename T > +inline rapidjson::Value::ConstObject getObject( T& obj ) { + return obj.GetObject(); +} + +template< typename T > +inline int getType( const T& obj ) { + return obj.GetType(); +} + +template< typename T > +inline bool getBool( T& obj ) { + return obj.GetBool(); +} + +// template< typename T > +// inline const char* getChar( T& obj ) { +// return obj.GetString(); +// } + +template< typename T > +inline double getDouble( T& obj ) { + return obj.GetDouble(); +} + +template< typename T > +inline int getInt( T& obj ) { + return obj.GetInt(); +} + #endif \ No newline at end of file diff --git a/src/RcppExports.cpp b/src/RcppExports.cpp index 3a25767..1313ba2 100644 --- a/src/RcppExports.cpp +++ b/src/RcppExports.cpp @@ -18,17 +18,6 @@ BEGIN_RCPP return rcpp_result_gen; END_RCPP } -// rcpp_parse_json -SEXP rcpp_parse_json(const char * json); -RcppExport SEXP _jsonify_rcpp_parse_json(SEXP jsonSEXP) { -BEGIN_RCPP - Rcpp::RObject rcpp_result_gen; - Rcpp::RNGScope rcpp_rngScope_gen; - Rcpp::traits::input_parameter< const char * >::type json(jsonSEXP); - rcpp_result_gen = Rcpp::wrap(rcpp_parse_json(json)); - return rcpp_result_gen; -END_RCPP -} // rcpp_from_ndjson SEXP rcpp_from_ndjson(const char * ndjson, bool& simplify, bool& fill_na); RcppExport SEXP _jsonify_rcpp_from_ndjson(SEXP ndjsonSEXP, SEXP simplifySEXP, SEXP fill_naSEXP) { @@ -182,7 +171,6 @@ END_RCPP static const R_CallMethodDef CallEntries[] = { {"_jsonify_rcpp_from_json", (DL_FUNC) &_jsonify_rcpp_from_json, 3}, - {"_jsonify_rcpp_parse_json", (DL_FUNC) &_jsonify_rcpp_parse_json, 1}, {"_jsonify_rcpp_from_ndjson", (DL_FUNC) &_jsonify_rcpp_from_ndjson, 3}, {"_jsonify_rcpp_get_dtypes", (DL_FUNC) &_jsonify_rcpp_get_dtypes, 1}, {"_jsonify_rcpp_simplify_vector", (DL_FUNC) &_jsonify_rcpp_simplify_vector, 3}, diff --git a/src/from_json.cpp b/src/from_json.cpp index 63f6eae..7bc4153 100644 --- a/src/from_json.cpp +++ b/src/from_json.cpp @@ -7,13 +7,6 @@ SEXP rcpp_from_json(const char * json, bool& simplify, bool& fill_na ) { return jsonify::api::from_json( json, simplify, fill_na ); } - -// [[Rcpp::export]] -SEXP rcpp_parse_json(const char * json ) { - return jsonify::api::parse_json( json ); -} - - // [[Rcpp::export]] SEXP rcpp_from_ndjson(const char * ndjson, bool& simplify, bool& fill_na ) { return jsonify::api::from_ndjson( ndjson, simplify, fill_na );