Skip to content

Commit

Permalink
experimenting with templates #69
Browse files Browse the repository at this point in the history
  • Loading branch information
dcooley committed May 29, 2020
1 parent 9bb30e3 commit e9c4ed8
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 184 deletions.
4 changes: 0 additions & 4 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
17 changes: 0 additions & 17 deletions inst/include/jsonify/from_json/api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,10 @@

#include <Rcpp.h>
#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
Expand Down
43 changes: 27 additions & 16 deletions inst/include/jsonify/from_json/from_json.hpp
Original file line number Diff line number Diff line change
@@ -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 <Rcpp.h>

Expand All @@ -16,18 +16,27 @@ 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,
bool simplify,
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;
Expand All @@ -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;
Expand All @@ -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() );
}
Expand All @@ -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,
Expand All @@ -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;
Expand All @@ -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: {
Expand All @@ -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 ) {
Expand All @@ -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 ) {
Expand Down
97 changes: 0 additions & 97 deletions inst/include/jsonify/from_json/parse_json.hpp

This file was deleted.

7 changes: 4 additions & 3 deletions inst/include/jsonify/from_json/simplify/simplify.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -86,7 +86,7 @@ namespace from_json {

for( const auto& child : array ) {

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

// bool
case rapidjson::kFalseType : {}
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion inst/include/jsonify/jsonify.hpp
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
Loading

0 comments on commit e9c4ed8

Please sign in to comment.