From 76ff976e83ba270146b9c806dccace72cc3bdad6 Mon Sep 17 00:00:00 2001 From: Christian Kreibich Date: Mon, 11 Apr 2022 22:09:04 -0700 Subject: [PATCH] Avoid whitespace around function type strings in JSON rendering Callable types were rendered with a trailing "\n" in to_json() output. Tweaking the Describe() calls to stop producing the newline is prone to test failures, so this focuses on the JSON string production to suppress it, which doesn't affect any tests. --- src/Val.cc | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/Val.cc b/src/Val.cc index 3d3290a200..1d0055f71b 100644 --- a/src/Val.cc +++ b/src/Val.cc @@ -420,8 +420,9 @@ static void BuildJSON(threading::formatter::JSON::NullDoubleWriter& writer, Val* } rapidjson::Value j; + auto tag = val->GetType()->Tag(); - switch ( val->GetType()->Tag() ) + switch ( tag ) { case TYPE_BOOL: writer.Bool(val->AsBool()); @@ -475,8 +476,15 @@ static void BuildJSON(threading::formatter::JSON::NullDoubleWriter& writer, Val* ODesc d; d.SetStyle(RAW_STYLE); val->Describe(&d); - writer.String(util::json_escape_utf8( - std::string(reinterpret_cast(d.Bytes()), d.Len()))); + std::string desc(reinterpret_cast(d.Bytes()), d.Len()); + + // None of our function types should have surrounding + // whitespace, but ODesc might produce it due to its + // many output modes and flags. Strip it. + if ( tag == TYPE_FUNC ) + desc = util::strstrip(desc); + + writer.String(util::json_escape_utf8(desc)); break; }