Skip to content

Commit

Permalink
Merge pull request #17403 from dotnwat/json-cleanup
Browse files Browse the repository at this point in the history
json: minor clean-ups
  • Loading branch information
dotnwat authored Mar 27, 2024
2 parents 22776d1 + 60f6717 commit debdfea
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 43 deletions.
18 changes: 10 additions & 8 deletions src/v/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,21 @@ endforeach()
add_subdirectory(base)
link_libraries(v::base)

# New style modules.
add_subdirectory(hashing)
add_subdirectory(random)
add_subdirectory(bytes)
add_subdirectory(compression)
add_subdirectory(http)
add_subdirectory(container)
add_subdirectory(strings)
add_subdirectory(crypto)

# libraries
add_subdirectory(test_utils)
add_subdirectory(io)
add_subdirectory(ssx)
add_subdirectory(finjector)
add_subdirectory(hashing)
add_subdirectory(random)
add_subdirectory(bytes)
add_subdirectory(compression)
add_subdirectory(utils)
add_subdirectory(syschecks)
add_subdirectory(net)
Expand All @@ -109,7 +115,6 @@ add_subdirectory(features)
add_subdirectory(kafka)
add_subdirectory(reflection)
add_subdirectory(pandaproxy)
add_subdirectory(http)
add_subdirectory(cloud_storage_clients)
add_subdirectory(archival)
add_subdirectory(security)
Expand All @@ -122,9 +127,6 @@ add_subdirectory(resource_mgmt)
add_subdirectory(migrations)
add_subdirectory(wasm)
add_subdirectory(transform)
add_subdirectory(container)
add_subdirectory(strings)
add_subdirectory(crypto)

option(ENABLE_GIT_VERSION "Build with Git metadata" OFF)

Expand Down
1 change: 1 addition & 0 deletions src/v/compat/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "cluster/errc.h"
#include "cluster/partition_balancer_types.h"
#include "cluster/types.h"
#include "container/json.h"
#include "json/document.h"
#include "json/json.h"
#include "model/fundamental.h"
Expand Down
29 changes: 29 additions & 0 deletions src/v/container/include/container/json.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2024 Redpanda Data, Inc.
*
* Use of this software is governed by the Business Source License
* included in the file licenses/BSL.md
*
* As of the Change Date specified in that file, in accordance with
* the Business Source License, use of this software will be governed
* by the Apache License, Version 2.0
*/
#pragma once

#include "container/fragmented_vector.h"
#include "json/json.h"

namespace json {

template<typename T, size_t max_fragment_size>
void rjson_serialize(
json::Writer<json::StringBuffer>& w,
const fragmented_vector<T, max_fragment_size>& v) {
w.StartArray();
for (const auto& e : v) {
rjson_serialize(w, e);
}
w.EndArray();
}

} // namespace json
18 changes: 18 additions & 0 deletions src/v/json/json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,22 @@ void rjson_serialize(
rjson_serialize(w, std::string_view{path.native()});
}

ss::sstring minify(std::string_view json) {
json::Reader r;
json::StringStream in(json.data());
json::StringBuffer out;
json::Writer<json::StringBuffer> w{out};
r.Parse(in, w);
return ss::sstring(out.GetString(), out.GetSize());
}

ss::sstring prettify(std::string_view json) {
json::Reader r;
json::StringStream in(json.data());
json::StringBuffer out;
json::PrettyWriter<json::StringBuffer> w{out};
r.Parse(in, w);
return ss::sstring(out.GetString(), out.GetSize());
}

} // namespace json
39 changes: 4 additions & 35 deletions src/v/json/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

#pragma once

#include "container/fragmented_vector.h"
#include "json/_include_first.h"
#include "json/prettywriter.h"
#include "json/reader.h"
Expand All @@ -21,9 +20,7 @@
#include "net/unresolved_address.h"
#include "utils/named_type.h"

#include <seastar/net/inet_address.hh>
#include <seastar/net/ip.hh>
#include <seastar/net/socket_defs.hh>
#include <seastar/core/circular_buffer.hh>

#include <chrono>
#include <type_traits>
Expand All @@ -49,9 +46,6 @@ void rjson_serialize(json::Writer<json::StringBuffer>& w, double v);

void rjson_serialize(json::Writer<json::StringBuffer>& w, std::string_view s);

void rjson_serialize(
json::Writer<json::StringBuffer>& w, const ss::socket_address& v);

void rjson_serialize(
json::Writer<json::StringBuffer>& w, const net::unresolved_address& v);

Expand Down Expand Up @@ -95,18 +89,7 @@ void rjson_serialize(
w.EndArray();
}

template<typename T, size_t max_fragment_size>
void rjson_serialize(
json::Writer<json::StringBuffer>& w,
const fragmented_vector<T, max_fragment_size>& v) {
w.StartArray();
for (const auto& e : v) {
rjson_serialize(w, e);
}
w.EndArray();
}

template<typename T, size_t chunk_size = 128>
template<typename T, size_t chunk_size>
void rjson_serialize(
json::Writer<json::StringBuffer>& w,
const ss::chunked_fifo<T, chunk_size>& v) {
Expand Down Expand Up @@ -138,22 +121,8 @@ void rjson_serialize(
w.EndArray();
}

inline ss::sstring minify(std::string_view json) {
json::Reader r;
json::StringStream in(json.data());
json::StringBuffer out;
json::Writer<json::StringBuffer> w{out};
r.Parse(in, w);
return ss::sstring(out.GetString(), out.GetSize());
}
ss::sstring minify(std::string_view json);

inline ss::sstring prettify(std::string_view json) {
json::Reader r;
json::StringStream in(json.data());
json::StringBuffer out;
json::PrettyWriter<json::StringBuffer> w{out};
r.Parse(in, w);
return ss::sstring(out.GetString(), out.GetSize());
}
ss::sstring prettify(std::string_view json);

} // namespace json

0 comments on commit debdfea

Please sign in to comment.