Skip to content

Commit

Permalink
template interface #69
Browse files Browse the repository at this point in the history
  • Loading branch information
dcooley committed May 28, 2020
1 parent 54ae6e6 commit 5657962
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 20 deletions.
7 changes: 4 additions & 3 deletions inst/include/jsonify/from_json/from_json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ namespace from_json {
return out;
}

// const rapidjson::Value&
template< typename T >
inline SEXP parse_json(
const T& json,
Expand All @@ -90,14 +91,14 @@ namespace from_json {
return Rcpp::wrap< bool >( json );
}
case rapidjson::kStringType: {
return Rcpp::wrap( std::string( json.GetString() ) );
return Rcpp::wrap< const char* >( json );
}
// numeric
case rapidjson::kNumberType: {
if( json.IsDouble() ) {
return Rcpp::wrap< double >( json.GetDouble() );
return Rcpp::wrap< double >( json );
} else {
return Rcpp::wrap< int >( json.GetInt() );
return Rcpp::wrap< int >( json );
}
}
case rapidjson::kObjectType: {
Expand Down
8 changes: 4 additions & 4 deletions inst/include/jsonify/from_json/simplify/simplify.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ namespace from_json {
// bool
case rapidjson::kFalseType: {}
case rapidjson::kTrueType: {
out[i] = child.GetBool();
out[i] = Rcpp::wrap< bool >( child );
update_rtype< LGLSXP >( r_type );
break;
}

// string
case rapidjson::kStringType: {
out[i] = Rcpp::String( child.GetString() );
out[i] = Rcpp::wrap< const char * >( child );
update_rtype< STRSXP >( r_type );
break;
}
Expand All @@ -106,11 +106,11 @@ namespace from_json {
case rapidjson::kNumberType: {
if( child.IsDouble() ) {
// double
out[i] = child.GetDouble();
out[i] = Rcpp::wrap< double >( child );
update_rtype< REALSXP >( r_type );
} else {
// int
out[i] = child.GetInt();
out[i] = Rcpp::wrap< int >( child );
update_rtype< INTSXP >( r_type );
}
break;
Expand Down
20 changes: 7 additions & 13 deletions inst/include/jsonify/jsonify_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,23 @@

namespace Rcpp {

template< typename T > SEXP wrap( const rapidjson::Value& obj );
// template<> SEXP wrap( std::basic_string& obj );
template< typename T > SEXP wrap( const rapidjson::Value& obj );

namespace traits {

} // traits
} // Rcpp




#include <Rcpp.h>



namespace Rcpp {

template< typename T >
SEXP wrap( const rapidjson::Value& obj) {

const int RTYPE = Rcpp::traits::r_sexptype_traits< T >::rtype;

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


namespace traits {
Expand Down

0 comments on commit 5657962

Please sign in to comment.