Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return size of json written to buffer that is not resizable #1633

Open
elliot-spidertracks opened this issue Feb 26, 2025 · 1 comment
Open
Labels
enhancement New feature or request

Comments

@elliot-spidertracks
Copy link

Maybe I have missed something, but currently it doesn't seem like you can know how much data was serialized into a buffer if it's not resizable.
For example I want to pass in a std::span and on success be able to know where the serialization ended.
Locally I have tried just writing the location value in the error_ctx that is returned and this works nicely however I know that location is documented as being for internal use.

   template <opts Opts, class T, output_buffer Buffer>
      requires write_supported<Opts.format, T>
   [[nodiscard]] error_ctx write(T&& value, Buffer& buffer, is_context auto&& ctx)
   {
      if constexpr (resizable<Buffer>) {
         // A buffer could be size 1, to ensure we have sufficient memory we can't just check `empty()`
         if (buffer.size() < 2 * write_padding_bytes) {
            buffer.resize(2 * write_padding_bytes);
         }
      }
      size_t ix = 0; // overwrite index
      detail::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);
      }

      return {ctx.error, ctx.custom_error_message, ix}; //include location
   }

Any thoughts on making changes like this?

@stephenberry
Copy link
Owner

This is also an active issue here: #1285. This is definitely a feature that should be added. There is other internal refactoring that I'm finishing for v5.0.0, but soon after that I would like to tackle this problem. I'll leave this issue open as well, as I think it is quite important.

@stephenberry stephenberry added the enhancement New feature or request label Feb 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants