write_json(msg, buffer): number of chars written? #2137
-
|
I'm trying to figure out if there is a way to know how many characters So what would be the right way to get serialized char count out? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Under the hood of size_t ix = 0; // overwrite index
to<Opts.format, std::remove_cvref_t<T>>::template op<Opts>(std::forward<T>(value), ctx, buffer, ix);
if constexpr (resizable<Buffer>) {
buffer.resize(ix);
}So, you can use this API yourself to figure out how many bytes have been written with But, if you pass it an oversized raw buffer there is already an API that hands you back the number of bytes written: template <auto Opts, class T, raw_buffer Buffer>
requires write_supported<T, Opts.format>
[[nodiscard]] glz::expected<size_t, error_ctx> write(T&& value, Buffer&& buffer, is_context auto&& ctx)
{
size_t ix = 0;
to<Opts.format, std::remove_cvref_t<T>>::template op<Opts>(std::forward<T>(value), ctx, buffer, ix);
if (bool(ctx.error)) [[unlikely]] {
return glz::unexpected(error_ctx{ctx.error});
}
return {ix};
} |
Beta Was this translation helpful? Give feedback.
Under the hood of
glz::writecalls it is doing this:So, you can use this API yourself to figure out how many bytes have been written with
ixBut, if you pass it an oversized raw buffer there is already an API that hands you back the number of bytes written: