Problem solved by the enhancement
- Reading: Sometimes it is useful to defer parsing of JSON data to a later point, for example when it is not clear initially if the data is actually needed. Another use case is passing the JSON data to another library for parsing.
- Writing: Sometimes partial JSON data might have already been created by a different library, it would be useful to directly embed the JSON data using Struson.
Both cases can currently not be achieved easily and efficiently using Struson. It is necessary to use Struson's API to fully process the JSON data when reading, there is no way to defer it to a later point, or to insert existing JSON data without repeatedly calling Struson's API for each nested value (respectively using JsonReader::transfer_to).
Enhancement description
📢 This is only a proposal and not a planned feature yet. If you are interested in this, add a 👍 or feel free to comment (also with any suggestions and feedback).
Add methods to JsonReader and JsonWriter for reading and writing 'raw' JSON data.
Generally:
- Mark the new API as experimental initially, so that it is easier to change it later if necessary
Documentation should mention that not all JsonReader / JsonWriter implementation support it; if not supported a panic is triggered unconditionally by that implementation
Only add methods to JsonStreamReader and JsonStreamWriter, but not generally to JsonReader and JsonWriter
- ? Two APIs: One for reading / writing the whole value (as
&[u8]), and one for reading / writing it using Read / Write (respectively a custom subtrait for Write, to indicate when writing is done)
Reader:
- Intended for reading a single value (for array and object all the nested values as well)
- Should allow the user to provide their own
FnMut(&[u8]) -> Result so they have control over how / where the raw JSON data is collected (e.g. a Vec)
- ? Should maybe support two modes (selected by a
bool or enum?), one where JSON syntax is checked, and another faster one which only checks for delimiters (e.g. for string search for unescaped ", ignoring if other escape sequences are valid, or even if data is valid UTF-8)
- Should also consider reader settings (especially things like comments, also for the "unvalidated fast mode")
Writer:
- Intended for writing a single value (for array and object all the nested values as well)
- Document that syntax of JSON data might not be validated; however, users should not rely on this, e.g. to write more than one value
- Document that
JsonWriter might include JSON data as is, without formatting being applied
- Currently
JsonStreamWriter guarantees that the written data is valid UTF-8. If the new raw writing method supports &[u8] instead of str, should the method then be unsafe? Or at least it should explicitly mention that the user is responsible for providing only valid UTF-8 data.
Alternatives / workarounds
Use JsonReader::transfer_to to collect the JSON data from a reader, respectively to write JSON data to a writer. See the JsonReaderDeserializer and JsonWriterSerializer documentation for how this can be achieved.
Problem solved by the enhancement
Both cases can currently not be achieved easily and efficiently using Struson. It is necessary to use Struson's API to fully process the JSON data when reading, there is no way to defer it to a later point, or to insert existing JSON data without repeatedly calling Struson's API for each nested value (respectively using
JsonReader::transfer_to).Enhancement description
📢 This is only a proposal and not a planned feature yet. If you are interested in this, add a 👍 or feel free to comment (also with any suggestions and feedback).
Add methods to
JsonReaderandJsonWriterfor reading and writing 'raw' JSON data.Generally:
Documentation should mention that not allJsonReader/JsonWriterimplementation support it; if not supported a panic is triggered unconditionally by that implementationOnly add methods to
JsonStreamReaderandJsonStreamWriter, but not generally toJsonReaderandJsonWriter&[u8]), and one for reading / writing it usingRead/Write(respectively a custom subtrait forWrite, to indicate when writing is done)Reader:
FnMut(&[u8]) -> Resultso they have control over how / where the raw JSON data is collected (e.g. aVec)boolor enum?), one where JSON syntax is checked, and another faster one which only checks for delimiters (e.g. for string search for unescaped", ignoring if other escape sequences are valid, or even if data is valid UTF-8)Writer:
JsonWritermight include JSON data as is, without formatting being appliedJsonStreamWriterguarantees that the written data is valid UTF-8. If the new raw writing method supports&[u8]instead ofstr, should the method then beunsafe? Or at least it should explicitly mention that the user is responsible for providing only valid UTF-8 data.Alternatives / workarounds
Use
JsonReader::transfer_toto collect the JSON data from a reader, respectively to write JSON data to a writer. See theJsonReaderDeserializerandJsonWriterSerializerdocumentation for how this can be achieved.