MCPcopy Create free account
hub / github.com/awslabs/aws-lambda-cpp / json_escape

Function json_escape

src/runtime.cpp:467–511  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

465}
466
467static std::string json_escape(std::string const& in)
468{
469 constexpr char last_non_printable_character = 31;
470 std::string out;
471 out.reserve(in.length()); // most strings will end up identical
472 for (char ch : in) {
473 if (ch > last_non_printable_character && ch != '\"' && ch != '\\') {
474 out.append(1, ch);
475 }
476 else {
477 out.append(1, '\\');
478 switch (ch) {
479 case '\\':
480 out.append(1, '\\');
481 break;
482 case '"':
483 out.append(1, '"');
484 break;
485 case '\b':
486 out.append(1, 'b');
487 break;
488 case '\f':
489 out.append(1, 'f');
490 break;
491 case '\n':
492 out.append(1, 'n');
493 break;
494 case '\r':
495 out.append(1, 'r');
496 break;
497 case '\t':
498 out.append(1, 't');
499 break;
500 default:
501 // escape and print as unicode codepoint
502 constexpr int printed_unicode_length = 6; // 4 hex + letter 'u' + \0
503 std::array<char, printed_unicode_length> buf;
504 snprintf(buf.data(), buf.size(), "u%04x", ch);
505 out.append(buf.data(), buf.size() - 1); // add only five, discarding the null terminator.
506 break;
507 }
508 }
509 }
510 return out;
511}
512
513AWS_LAMBDA_RUNTIME_API
514invocation_response invocation_response::success(std::string payload, std::string content_type)

Callers 1

failureMethod · 0.85

Calls 1

sizeMethod · 0.45

Tested by

no test coverage detected