Skip to content

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 17 commits into from
Sep 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ compiletest_rs = "0.3"
serde_bytes = "0.10"
serde_derive = "1.0"

[package.metadata.docs.rs]
features = ["raw_value"]

[package.metadata.playground]
features = ["raw_value"]


### FEATURES #################################################################

Expand All @@ -41,3 +47,6 @@ preserve_order = ["indexmap"]
# allows JSON numbers of arbitrary size/precision to be read into a Number and
# written back to a JSON string without loss of precision.
arbitrary_precision = []

# Provide a RawValue type that can hold unprocessed JSON during deserialization.
raw_value = []
21 changes: 20 additions & 1 deletion src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,17 @@ impl<'de, R: Read<'de>> Deserializer<R> {
}
}
}

#[cfg(feature = "raw_value")]
fn deserialize_raw_value<V>(&mut self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
self.parse_whitespace()?;
self.read.begin_raw_buffering();
self.ignore_value()?;
self.read.end_raw_buffering(visitor)
}
}

impl FromStr for Number {
Expand Down Expand Up @@ -1412,10 +1423,18 @@ impl<'de, 'a, R: Read<'de>> de::Deserializer<'de> for &'a mut Deserializer<R> {

/// Parses a newtype struct as the underlying value.
#[inline]
fn deserialize_newtype_struct<V>(self, _name: &str, visitor: V) -> Result<V::Value>
fn deserialize_newtype_struct<V>(self, name: &str, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
#[cfg(feature = "raw_value")]
{
if name == ::raw::TOKEN {
return self.deserialize_raw_value(visitor);
}
}

let _ = name;
visitor.visit_newtype_struct(self)
}

Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,3 +389,6 @@ pub mod value;
mod iter;
mod number;
mod read;

#[cfg(feature = "raw_value")]
mod raw;
15 changes: 5 additions & 10 deletions src/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,7 @@ use error::ErrorCode;
#[cfg(feature = "arbitrary_precision")]
/// Not public API. Should be pub(crate).
#[doc(hidden)]
pub const SERDE_STRUCT_FIELD_NAME: &'static str = "$__serde_private_number";

#[cfg(feature = "arbitrary_precision")]
/// Not public API. Should be pub(crate).
#[doc(hidden)]
pub const SERDE_STRUCT_NAME: &'static str = "$__serde_private_Number";
pub const TOKEN: &'static str = "$serde_json::private::Number";

/// Represents a JSON number, whether integer or floating point.
#[derive(Clone, PartialEq)]
Expand Down Expand Up @@ -346,8 +341,8 @@ impl Serialize for Number {
{
use serde::ser::SerializeStruct;

let mut s = serializer.serialize_struct(SERDE_STRUCT_NAME, 1)?;
s.serialize_field(SERDE_STRUCT_FIELD_NAME, &self.n)?;
let mut s = serializer.serialize_struct(TOKEN, 1)?;
s.serialize_field(TOKEN, &self.n)?;
s.end()
}
}
Expand Down Expand Up @@ -426,7 +421,7 @@ impl<'de> de::Deserialize<'de> for NumberKey {
where
E: de::Error,
{
if s == SERDE_STRUCT_FIELD_NAME {
if s == TOKEN {
Ok(())
} else {
Err(de::Error::custom("expected field with custom name"))
Expand Down Expand Up @@ -638,7 +633,7 @@ impl<'de> Deserializer<'de> for NumberFieldDeserializer {
where
V: de::Visitor<'de>,
{
visitor.visit_borrowed_str(SERDE_STRUCT_FIELD_NAME)
visitor.visit_borrowed_str(TOKEN)
}

forward_to_deserialize_any! {
Expand Down
Loading