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

Method writeJSONDouble

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

Convert the given double to a JSON string, which is either the number, "NaN" or "Infinity" or "-Infinity".

Source from the content-addressed store, hash-verified

537// Convert the given double to a JSON string, which is either the number,
538// "NaN" or "Infinity" or "-Infinity".
539uint32_t TJSONProtocol::writeJSONDouble(double num) {
540 uint32_t result = context_->write(*trans_);
541 std::string val;
542
543 bool special = false;
544 switch (std::fpclassify(num)) {
545 case FP_INFINITE:
546 if (std::signbit(num)) {
547 val = kThriftNegativeInfinity;
548 } else {
549 val = kThriftInfinity;
550 }
551 special = true;
552 break;
553 case FP_NAN:
554 val = kThriftNan;
555 special = true;
556 break;
557 default:
558 val = doubleToString(num);
559 break;
560 }
561
562 bool escapeNum = special || context_->escapeNum();
563 if (escapeNum) {
564 trans_->write(&kJSONStringDelimiter, 1);
565 result += 1;
566 }
567 if (val.length() > (std::numeric_limits<uint32_t>::max)())
568 throw TProtocolException(TProtocolException::SIZE_LIMIT);
569 trans_->write((const uint8_t*)val.c_str(), static_cast<uint32_t>(val.length()));
570 result += static_cast<uint32_t>(val.length());
571 if (escapeNum) {
572 trans_->write(&kJSONStringDelimiter, 1);
573 result += 1;
574 }
575 return result;
576}
577
578uint32_t TJSONProtocol::writeJSONObjectStart() {
579 uint32_t result = context_->write(*trans_);

Callers

nothing calls this directly

Calls 5

doubleToStringFunction · 0.85
TProtocolExceptionClass · 0.70
writeMethod · 0.65
lengthMethod · 0.65
escapeNumMethod · 0.45

Tested by

no test coverage detected