Skip to content

Commit

Permalink
Add to_json streaming version
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-sparus committed Jun 11, 2024
1 parent 09fc7eb commit d822e87
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions immer/extra/persist/json/json_with_pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,34 @@

namespace immer::persist {

/**
* Type T must provide a callable free function get_pools_types(const T&).
*/
template <class Archive = cereal::JSONOutputArchive,
class T,
Policy<T> Policy = default_policy>
auto to_json_with_pool(const T& value0, const Policy& policy = Policy{})
void to_json_with_pool_stream(auto& os,
const T& value0,
const Policy& policy = Policy{})
{
auto pools = detail::generate_output_pools(policy.get_pool_types(value0));
using Pools = std::decay_t<decltype(pools)>;
auto ar = immer::persist::json_immer_output_archive<
Archive,
Pools,
decltype(policy.get_output_wrap_fn()),
decltype(policy.get_pool_name_fn(value0))>{
pools, policy.get_output_wrap_fn(), os};
policy.save(ar, value0);
// Calling finalize explicitly, as it might throw on saving the pools,
// for example if pool names are not unique.
ar.finalize();
}

template <class Archive = cereal::JSONOutputArchive,
class T,
Policy<T> Policy = default_policy>
std::string to_json_with_pool(const T& value0, const Policy& policy = Policy{})
{
auto os = std::ostringstream{};
{
auto pools =
detail::generate_output_pools(policy.get_pool_types(value0));
using Pools = std::decay_t<decltype(pools)>;
auto ar = immer::persist::json_immer_output_archive<
Archive,
Pools,
decltype(policy.get_output_wrap_fn()),
decltype(policy.get_pool_name_fn(value0))>{
pools, policy.get_output_wrap_fn(), os};
policy.save(ar, value0);
// Calling finalize explicitly, as it might throw on saving the pools,
// for example if pool names are not unique.
ar.finalize();
}
to_json_with_pool_stream<Archive>(os, value0, policy);
return os.str();
}

Expand Down

0 comments on commit d822e87

Please sign in to comment.