0
0
mirror of https://github.com/OpenVPN/openvpn3.git synced 2024-09-20 20:13:05 +02:00

jsonhelper : added json::format_compact() methods for formatting JSON into a compact representation without extraneous whitespace.

This commit is contained in:
James Yonan 2016-11-11 10:38:19 -08:00 committed by Lev Stipakov
parent ff804f93f0
commit 760ae4a5f1
No known key found for this signature in database
GPG Key ID: 88670BE258B9C258

View File

@ -27,6 +27,8 @@
#include <openvpn/common/exception.hpp> #include <openvpn/common/exception.hpp>
#include <openvpn/common/string.hpp> #include <openvpn/common/string.hpp>
#include <openvpn/common/file.hpp> #include <openvpn/common/file.hpp>
#include <openvpn/buffer/bufstr.hpp>
#include <openvpn/buffer/bufstream.hpp>
namespace openvpn { namespace openvpn {
@ -297,6 +299,22 @@ namespace openvpn {
return get_dict(root, name, optional, ""); return get_dict(root, name, optional, "");
} }
static void format_compact(const Json::Value& root, Buffer& buf)
{
Json::StreamWriterBuilder json_builder;
json_builder.settings_["indentation"] = "";
BufferStreamOut os(buf);
std::unique_ptr<Json::StreamWriter> sw(json_builder.newStreamWriter());
sw->write(root, &os);
}
static std::string format_compact(const Json::Value& root, const size_t size_hint=256)
{
BufferPtr bp = new BufferAllocated(size_hint, BufferAllocated::GROW);
format_compact(root, *bp);
return buf_to_string(*bp);
}
private: private:
static std::string fmt_name(const std::string& name, const std::string& title) static std::string fmt_name(const std::string& name, const std::string& title)
{ {