MCPcopy Create free account
hub / github.com/apache/thrift / readJSONString

Method readJSONString

lib/cpp/src/thrift/protocol/TJSONProtocol.cpp:748–802  ·  view source on GitHub ↗

Decodes a JSON string, including unescaping, and returns the string via str

Source from the content-addressed store, hash-verified

746
747// Decodes a JSON string, including unescaping, and returns the string via str
748uint32_t TJSONProtocol::readJSONString(std::string& str, bool skipContext) {
749 uint32_t result = (skipContext ? 0 : context_->read(reader_));
750 result += readJSONSyntaxChar(kJSONStringDelimiter);
751 std::vector<uint16_t> codeunits;
752 uint8_t ch;
753 str.clear();
754 while (true) {
755 ch = reader_.read();
756 ++result;
757 if (ch == kJSONStringDelimiter) {
758 break;
759 }
760 if (ch == kJSONBackslash) {
761 ch = reader_.read();
762 ++result;
763 if (ch == kJSONEscapeChar) {
764 uint16_t cp;
765 result += readJSONEscapeChar(&cp);
766 if (isHighSurrogate(cp)) {
767 codeunits.push_back(cp);
768 } else {
769 if (isLowSurrogate(cp)
770 && codeunits.empty()) {
771 throw TProtocolException(TProtocolException::INVALID_DATA,
772 "Missing UTF-16 high surrogate pair.");
773 }
774 codeunits.push_back(cp);
775 codeunits.push_back(0);
776 str += boost::locale::conv::utf_to_utf<char>(codeunits.data());
777 codeunits.clear();
778 }
779 continue;
780 } else {
781 size_t pos = kEscapeChars.find(ch);
782 if (pos == kEscapeChars.npos) {
783 throw TProtocolException(TProtocolException::INVALID_DATA,
784 "Expected control char, got '" + std::string((const char*)&ch, 1)
785 + "'.");
786 }
787 ch = kEscapeCharVals[pos];
788 }
789 }
790 if (!codeunits.empty()) {
791 throw TProtocolException(TProtocolException::INVALID_DATA,
792 "Missing UTF-16 low surrogate pair.");
793 }
794 str += ch;
795 }
796
797 if (!codeunits.empty()) {
798 throw TProtocolException(TProtocolException::INVALID_DATA,
799 "Missing UTF-16 low surrogate pair.");
800 }
801 return result;
802}
803
804// Reads a block of base64 characters, decoding it, and returns via str
805uint32_t TJSONProtocol::readJSONBase64(std::string& str) {

Callers

nothing calls this directly

Calls 9

isHighSurrogateFunction · 0.85
isLowSurrogateFunction · 0.85
stringFunction · 0.85
emptyMethod · 0.80
dataMethod · 0.80
findMethod · 0.80
TProtocolExceptionClass · 0.70
readMethod · 0.65
clearMethod · 0.65

Tested by

no test coverage detected