Skip to content

Commit 3a570ee

Browse files
committed
refactor(paws.common): split json_build_any into internal and exported functions with UTF-8 encoding
1 parent 0b1eed4 commit 3a570ee

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

paws.common/src/RcppExports.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ BEGIN_RCPP
6868
END_RCPP
6969
}
7070
// json_build_any
71-
std::string json_build_any(SEXP values);
71+
CharacterVector json_build_any(SEXP values);
7272
RcppExport SEXP _paws_common_json_build_any(SEXP valuesSEXP) {
7373
BEGIN_RCPP
7474
Rcpp::RObject rcpp_result_gen;

paws.common/src/json_builder.cpp

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ std::string type(SEXP object)
135135
}
136136

137137
// -------------------- Forward declaration --------------------
138-
// This function will be defined later and called recursively
139-
std::string json_build_any(SEXP values);
138+
// Internal function that builds JSON and returns std::string (used internally)
139+
std::string json_build_internal(SEXP values);
140140

141141
// -------------------- Helper function to safely convert to string --------------------
142142
// This function converts an SEXP to a string representation, handling various R types.
@@ -255,7 +255,7 @@ std::string json_build_list(SEXP values)
255255
for (R_xlen_t i = 0; i < n; i++)
256256
{
257257
// Recursively build JSON for each element
258-
std::string elem_json = json_build_any(VECTOR_ELT(values, i));
258+
std::string elem_json = json_build_internal(VECTOR_ELT(values, i));
259259
// Only add non-empty elements, excluding "empty" JSON array/object strings
260260
if (!elem_json.empty() && elem_json != "[]" && elem_json != "{}")
261261
{
@@ -325,7 +325,7 @@ std::string json_build_structure(SEXP values)
325325
}
326326

327327
// Recursively build JSON for the payload's value
328-
return json_build_any(payload_val);
328+
return json_build_internal(payload_val);
329329
}
330330

331331
SEXP names = Rf_getAttrib(values, symbol_cache.names_sym);
@@ -364,7 +364,7 @@ std::string json_build_structure(SEXP values)
364364
std::string loc_name = tag_get(val, "locationName"); // Check for locationName tag
365365
std::string name = loc_name.empty() ? key : loc_name; // Use locationName if present
366366

367-
std::string json_val = json_build_any(val); // Recursively build JSON for field value
367+
std::string json_val = json_build_internal(val); // Recursively build JSON for field value
368368
// Only add non-empty fields, excluding "empty" JSON array/object strings
369369
if (!json_val.empty() && json_val != "[]" && json_val != "{}")
370370
{
@@ -442,7 +442,7 @@ inline std::string json_build_map(SEXP values)
442442
SEXP val = element.second;
443443

444444
// Recursively build JSON for the value
445-
std::string json_val = json_build_any(val);
445+
std::string json_val = json_build_internal(val);
446446

447447
// Only add non-empty pairs to the final JSON string
448448
if (!json_val.empty() && json_val != "[]" && json_val != "{}")
@@ -469,17 +469,8 @@ inline std::string json_build_map(SEXP values)
469469
return result;
470470
}
471471

472-
/**
473-
* @brief Build Json Strings Using AWS Attributes for JSON Template
474-
*
475-
* @param object A list to be parsed into JSON string
476-
*
477-
* @return a JSON String
478-
*/
479-
//' @useDynLib paws.common _paws_common_json_build_any
480-
//' @importFrom Rcpp evalCpp
481-
// [[Rcpp::export]]
482-
std::string json_build_any(SEXP values)
472+
// Internal implementation that returns std::string (used by recursive calls)
473+
std::string json_build_internal(SEXP values)
483474
{
484475
// Determine the effective type of the R object for JSON building
485476
std::string t = type(values);
@@ -494,3 +485,20 @@ std::string json_build_any(SEXP values)
494485
// Default to scalar if none of the above, or if `type` returns "scalar"
495486
return json_build_scalar(values);
496487
}
488+
489+
/**
490+
* @brief Build Json Strings Using AWS Attributes for JSON Template
491+
*
492+
* @param object A list to be parsed into JSON string
493+
*
494+
* @return a JSON String
495+
*/
496+
//' @useDynLib paws.common _paws_common_json_build_any
497+
//' @importFrom Rcpp evalCpp
498+
// [[Rcpp::export]]
499+
CharacterVector json_build_any(SEXP values)
500+
{
501+
// Call internal implementation and UTF-8 encode result
502+
std::string result = json_build_internal(values);
503+
return CharacterVector::create(String(result, CE_UTF8));
504+
}

0 commit comments

Comments
 (0)