-
Notifications
You must be signed in to change notification settings - Fork 579
Implement RawValue type (alternative) #485
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
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This avoids a redundant UTF-8 check in the case of StrRead.
This is what serde_json::Number does in arbitrary precision mode. It is more robust because it fails fast in formats that are not JSON. In the previous implementation, it was too easy when mixing different data formats to end up with a RawValue holding something that is not valid JSON data: extern crate serde; extern crate serde_json; use serde::de::{Deserialize, IntoDeserializer, value::Error}; use serde_json::RawValue; fn main() -> Result<(), Error> { let bad = RawValue::deserialize("~~~".to_owned().into_deserializer())?; println!("{}", bad); Ok(()) } The new implementation detects in this case that we are not deserializing from a deserializer that speaks the language of serde_json::RawValue and will fail fast rather than producing an illegal RawValue.
Unclear in what situations comparing RawValues for equality would be meaningful.
Before: RawValue("{\"k\":\"v\"}") After: RawValue({"k":"v"})
Merged
I like where this is going! Anything I can pick up or help with? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR builds on #480 but eliminates the
Cow
in favor of distinct&RawValue
andBox<RawValue>
options.Documentation of
RawValue
:Reference to a range of bytes encompassing a single valid JSON value in the input data.
A
RawValue
can be used to defer parsing parts of a payload until later, or to avoid parsing it at all in the case that part of the payload just needs to be transferred verbatim into a different output object.When serializing, a value of this type will retain its original formatting and will not be minified or pretty-printed.
Example
Ownership
The typical usage of
RawValue
will be in the borrowed form:The borrowed form is suitable when deserializing through
serde_json::from_str
andserde_json::from_slice
which support borrowing from the input data without memory allocation.When deserializing through
serde_json::from_reader
you will need to use the boxed form ofRawValue
instead. This is almost as efficient but involves buffering the raw value from the I/O stream into memory.