Simplify `JsonStreamReader` by always collecting string and number values in `Vec` / `String`
Problem solved by the enhancement
The current JsonStreamReader implementation tries to serve string and number values from the reader buffer, and only if that is not possibly (e.g. in case of escape sequences) falls back to collecting the value in a Vec (see JsonStreamReader::value_bytes_buf).
Enhancement description
Consider always collecting the value in a Vec / String and then depending on whether a borrowed or owned string is requested, either return a reference to this buffer, or replace the buffer with an empty one and return it (maybe shrinking / copying it in case its capacity is extremely larger than its length).
This would have the advantage that it will simplify the JsonStreamReader implementation, and possibly also allows removing the JSON reader data buffer and only relying on the underlying reader for buffering, if desired (see also https://github.com/Marcono1234/struson/discussions/19#discussioncomment-7420995).
However, it has to be checked if the performance is noticeably negatively affected by this approach.
Could probably have a 'bytes consumer' trait similar to this:
trait BytesConsumer {
fn hasSpaceFor(&self, bytesCount: usize) -> bool;
fn addByte(&mut self, b: u8);
fn addBytes(&mut self, b: &[u8]);
}
Where for Vec<u8> and DiscardingBytesConsumer the hasSpaceFor method always unconditionally returns true, but for &mut [u8] it checks how much space is left.
Could then use hasSpaceFor as loop condition before reading the bytes. That will hopefully allow reusing the same code for regular value reading and StringValueReader / transfer_to and skipping. Currently there is code duplication for this.
Regarding #21, could maybe have a 'drop guard' in the reading implementation which in case of an error or panic clears the Vec<u8> or &[u8] so that the incomplete UTF-8 data cannot be observed.