MCPcopy Create free account
hub / github.com/apache/impala / AppendValueAsJSON

Method AppendValueAsJSON

be/src/kudu/util/debug/trace_event_impl.cc:692–757  ·  view source on GitHub ↗

static

Source from the content-addressed store, hash-verified

690
691// static
692void TraceEvent::AppendValueAsJSON(unsigned char type,
693 TraceEvent::TraceValue value,
694 std::string* out) {
695 switch (type) {
696 case TRACE_VALUE_TYPE_BOOL:
697 *out += value.as_bool ? "true" : "false";
698 break;
699 case TRACE_VALUE_TYPE_UINT:
700 SubstituteAndAppend(out, "$0", static_cast<uint64_t>(value.as_uint));
701 break;
702 case TRACE_VALUE_TYPE_INT:
703 SubstituteAndAppend(out, "$0", static_cast<int64_t>(value.as_int));
704 break;
705 case TRACE_VALUE_TYPE_DOUBLE: {
706 // FIXME: base/json/json_writer.cc is using the same code,
707 // should be made into a common method.
708 std::string real;
709 double val = value.as_double;
710 if (MathLimits<double>::IsFinite(val)) {
711 real = strings::Substitute("$0", val);
712 // Ensure that the number has a .0 if there's no decimal or 'e'. This
713 // makes sure that when we read the JSON back, it's interpreted as a
714 // real rather than an int.
715 if (real.find('.') == std::string::npos &&
716 real.find('e') == std::string::npos &&
717 real.find('E') == std::string::npos) {
718 real.append(".0");
719 }
720 // The JSON spec requires that non-integer values in the range (-1,1)
721 // have a zero before the decimal point - ".52" is not valid, "0.52" is.
722 if (real[0] == '.') {
723 real.insert(0, "0");
724 } else if (real.length() > 1 && real[0] == '-' && real[1] == '.') {
725 // "-.1" bad "-0.1" good
726 real.insert(1, "0");
727 }
728 } else if (MathLimits<double>::IsNaN(val)){
729 // The JSON spec doesn't allow NaN and Infinity (since these are
730 // objects in EcmaScript). Use strings instead.
731 real = "\"NaN\"";
732 } else if (val < 0) {
733 real = "\"-Infinity\"";
734 } else {
735 real = "\"Infinity\"";
736 }
737 SubstituteAndAppend(out, "$0", real);
738 break;
739 }
740 case TRACE_VALUE_TYPE_POINTER:
741 // JSON only supports double and int numbers.
742 // So as not to lose bits from a 64-bit pointer, output as a hex string.
743 StringAppendF(out, "\"0x%" PRIx64 "\"", static_cast<uint64_t>(
744 reinterpret_cast<intptr_t>(
745 value.as_pointer)));
746 break;
747 case TRACE_VALUE_TYPE_STRING:
748 case TRACE_VALUE_TYPE_COPY_STRING:
749 *out += "\"";

Callers

nothing calls this directly

Calls 8

SubstituteAndAppendFunction · 0.85
SubstituteFunction · 0.85
StringAppendFFunction · 0.85
JsonEscapeFunction · 0.85
findMethod · 0.45
appendMethod · 0.45
insertMethod · 0.45
lengthMethod · 0.45

Tested by

no test coverage detected