Write out the contents of the string as JSON string, base64-encoding the string's contents, and escaping as appropriate
| 476 | // Write out the contents of the string as JSON string, base64-encoding |
| 477 | // the string's contents, and escaping as appropriate |
| 478 | uint32_t TJSONProtocol::writeJSONBase64(const std::string& str) { |
| 479 | uint32_t result = context_->write(*trans_); |
| 480 | result += 2; // For quotes |
| 481 | trans_->write(&kJSONStringDelimiter, 1); |
| 482 | uint8_t b[4]; |
| 483 | const auto* bytes = (const uint8_t*)str.c_str(); |
| 484 | if (str.length() > (std::numeric_limits<uint32_t>::max)()) |
| 485 | throw TProtocolException(TProtocolException::SIZE_LIMIT); |
| 486 | auto len = static_cast<uint32_t>(str.length()); |
| 487 | while (len >= 3) { |
| 488 | // Encode 3 bytes at a time |
| 489 | base64_encode(bytes, 3, b); |
| 490 | trans_->write(b, 4); |
| 491 | result += 4; |
| 492 | bytes += 3; |
| 493 | len -= 3; |
| 494 | } |
| 495 | if (len) { // Handle remainder |
| 496 | base64_encode(bytes, len, b); |
| 497 | trans_->write(b, len + 1); |
| 498 | result += len + 1; |
| 499 | } |
| 500 | trans_->write(&kJSONStringDelimiter, 1); |
| 501 | return result; |
| 502 | } |
| 503 | |
| 504 | // Convert the given integer type to a JSON number, or a string |
| 505 | // if the context requires it (eg: key in a map pair). |