-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JsonString from #492 Tests: протестировано CI Pull Request resolved: #855 commit_hash:12f966c020326dea3d4ee51dfe5bee24bbbba811
- Loading branch information
1 parent
98ad535
commit 7e2332f
Showing
12 changed files
with
204 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include <userver/logging/json_string.hpp> | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include <userver/formats/json/value.hpp> | ||
#include <userver/utest/assert_macros.hpp> | ||
|
||
USERVER_NAMESPACE_BEGIN | ||
|
||
TEST(JsonString, ConstructFromJson) { | ||
using formats::literals::operator""_json; | ||
|
||
auto json = R"({ | ||
"a": "foo", | ||
"b": { | ||
"c": "d", | ||
"e": [ | ||
1, | ||
2 | ||
] | ||
} | ||
})"_json; | ||
|
||
logging::JsonString json_string(json); | ||
|
||
EXPECT_EQ(json_string.GetView(), R"({"a":"foo","b":{"c":"d","e":[1,2]}})"); | ||
} | ||
|
||
TEST(JsonString, ConstructFromString) { | ||
std::string json = R"({"a":"foo", | ||
"b":{"c":"d","e": | ||
[1,2]}})"; | ||
|
||
logging::JsonString json_string(json); | ||
|
||
EXPECT_EQ(json_string.GetView(), R"({"a":"foo","b":{"c":"d","e":[1,2]}})"); | ||
} | ||
|
||
TEST(JsonString, ConstructNull) { | ||
logging::JsonString json_string; | ||
|
||
EXPECT_EQ(json_string.GetView(), R"(null)"); | ||
} | ||
|
||
USERVER_NAMESPACE_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#pragma once | ||
|
||
/// @file userver/logging/json_string.hpp | ||
/// @brief @copybrief logging::JsonString | ||
|
||
#include <string> | ||
#include <string_view> | ||
|
||
#include <userver/formats/json_fwd.hpp> | ||
|
||
USERVER_NAMESPACE_BEGIN | ||
|
||
namespace logging { | ||
|
||
/// One line json string. | ||
/// JSON log formats write such tags directly as a sub-JSON, without packing them in a string. | ||
class JsonString { | ||
public: | ||
/// @brief Constructs from provided json object. | ||
/// The generated json string is an one line string. | ||
/*implicit*/ JsonString(const formats::json::Value& value); | ||
|
||
/// @brief Constructs from provided json string. It is the user's | ||
/// responsibility to ensure that the input json string is valid. | ||
/// New lines will be removed during construction. | ||
explicit JsonString(std::string json) noexcept; | ||
|
||
/// @brief Constructs null json (see GetValue for details) | ||
JsonString() noexcept = default; | ||
|
||
JsonString(JsonString&&) noexcept = default; | ||
JsonString(const JsonString&) = default; | ||
|
||
JsonString& operator=(JsonString&&) noexcept = default; | ||
JsonString& operator=(const JsonString&) = default; | ||
|
||
/// @brief Returns view to json | ||
std::string_view GetView() const noexcept; | ||
|
||
private: | ||
std::string json_; | ||
}; | ||
|
||
void WriteToStream(const JsonString& value, formats::json::StringBuilder& sw); | ||
|
||
} // namespace logging | ||
|
||
USERVER_NAMESPACE_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include <userver/logging/json_string.hpp> | ||
|
||
#include <algorithm> | ||
|
||
#include <userver/formats/json/string_builder.hpp> | ||
#include <userver/formats/json/value.hpp> | ||
#include <userver/logging/log.hpp> | ||
#include <userver/utils/assert.hpp> | ||
|
||
USERVER_NAMESPACE_BEGIN | ||
|
||
namespace logging { | ||
|
||
namespace { | ||
|
||
constexpr std::string_view kNull = "null"; | ||
|
||
} // namespace | ||
|
||
JsonString::JsonString(const formats::json::Value& value) : json_{ToString(value)} { | ||
// ToString builds one line string with RapidJson | ||
UASSERT(json_.find_first_of("\n\r") == std::string::npos); | ||
} | ||
|
||
JsonString::JsonString(std::string json) noexcept : json_{std::move(json)} { | ||
#ifndef NDEBUG | ||
try { | ||
formats::json::FromString(json_); | ||
} catch (const formats::json::ParseException& error) { | ||
UASSERT_MSG(false, fmt::format("Invalid json: {}, error: {}", json_, error.what())); | ||
} | ||
#endif | ||
|
||
// Remove extra new lines from user provided json string for two reasons: | ||
// 1. To avoid additional escaping in TSKV format | ||
// 2. To ensure a single line log in JSON format | ||
json_.erase( | ||
std::remove_if(json_.begin(), json_.end(), [](auto ch) { return ch == '\n' || ch == '\r'; }), json_.end() | ||
); | ||
} | ||
|
||
std::string_view JsonString::GetView() const noexcept { | ||
if (json_.empty()) { | ||
return kNull; | ||
} | ||
return json_; | ||
} | ||
|
||
void WriteToStream(const JsonString& value, formats::json::StringBuilder& sw) { sw.WriteRawString(value.GetView()); } | ||
|
||
} // namespace logging | ||
|
||
USERVER_NAMESPACE_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters