Skip to content

Commit

Permalink
OBJECT STREAMED AS RANGE
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Mar 12, 2024
1 parent 876c29e commit b3cc8b4
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions nano/lib/object_stream_adapters.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <nano/lib/object_stream.hpp>
#include <nano/lib/utility.hpp>

#include <ostream>
#include <sstream>
Expand Down Expand Up @@ -35,16 +36,36 @@ struct object_stream_formatter
}
};

enum class streamed_format
{
basic,
json
};

inline nano::object_stream_config to_object_stream_config (streamed_format format)
{
switch (format)
{
case streamed_format::basic:
return nano::object_stream_config::default_config ();
case streamed_format::json:
return nano::object_stream_config::json_config ();
default:
debug_assert (false);
return nano::object_stream_config::default_config ();
}
}

template <class Streamable>
auto streamed (Streamable const & value)
auto streamed (Streamable const & value, streamed_format format = streamed_format::basic)
{
return object_stream_formatter{ value, nano::object_stream_config::default_config () };
return object_stream_formatter{ value, to_object_stream_config (format) };
}

template <class Streamable>
auto streamed_as_json (Streamable const & value)
auto streamed_range (Streamable const & value, streamed_format format = streamed_format::basic)
{
return object_stream_formatter{ value, nano::object_stream_config::json_config () };
return object_stream_formatter{ [&] (nano::array_stream & ars) { ars.write (value); }, to_object_stream_config (format) };
}

/**
Expand Down Expand Up @@ -109,7 +130,7 @@ template <nano::object_or_array_streamable Value>
std::string to_json (Value const & value)
{
std::stringstream ss;
ss << nano::streamed_as_json (value);
ss << nano::streamed (value, nano::streamed_format::json);
return ss.str ();
}
}
Expand Down

0 comments on commit b3cc8b4

Please sign in to comment.