Skip to content

Implement RawValue type #480

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 1 commit 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
23 changes: 22 additions & 1 deletion src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

//! Deserialize JSON data to a Rust data structure.

use std::borrow::Cow;
use std::io;
use std::marker::PhantomData;
use std::result;
Expand Down Expand Up @@ -946,6 +947,22 @@ impl<'de, R: Read<'de>> Deserializer<R> {
}
}
}

fn deserialize_raw_value<V>(&mut self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
if let None = try!(self.parse_whitespace()) {
return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
}

self.read.toggle_raw_buffering();
de::Deserializer::deserialize_any(&mut *self, de::IgnoredAny)?;
match self.read.toggle_raw_buffering().unwrap() {
Cow::Owned(byte_buf) => visitor.visit_byte_buf(byte_buf),
Cow::Borrowed(bytes) => visitor.visit_borrowed_bytes(bytes),
}
}
}

impl FromStr for Number {
Expand Down Expand Up @@ -1412,10 +1429,14 @@ 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>,
{
if name == ::raw::SERDE_STRUCT_NAME {
return self.deserialize_raw_value(visitor);
}

visitor.visit_newtype_struct(self)
}

Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ pub use self::ser::{
to_string, to_string_pretty, to_vec, to_vec_pretty, to_writer, to_writer_pretty, Serializer,
};
#[doc(inline)]
pub use self::value::{from_value, to_value, Map, Number, Value};
pub use self::value::{from_value, to_value, Map, Number, RawValue, Value};

// We only use our own error type; no need for From conversions provided by the
// standard library's try! macro. This reduces lines of LLVM IR by 4%.
Expand All @@ -388,4 +388,5 @@ pub mod value;

mod iter;
mod number;
mod raw;
mod read;
93 changes: 93 additions & 0 deletions src/raw.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
use std::borrow::Cow;
use std::fmt;

use serde::de::Visitor;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

/// Represents any valid JSON value as a series of raw bytes.
///
/// This type can be used to defer parsing parts of a payload until later,
/// or to embed it verbatim into another JSON payload.
///
/// When serializing, a value of this type will retain its original formatting
/// and will not be minified or pretty-printed.
///
/// When deserializing, this type can not be used with the `#[serde(flatten)]` attribute,
/// as it relies on the original input buffer.

#[derive(Debug, Clone, PartialEq)]
pub struct RawValue<'a>(Cow<'a, str>);

impl<'a> AsRef<str> for RawValue<'a> {
fn as_ref(&self) -> &str {
&self.0
}
}

impl<'a> fmt::Display for RawValue<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(&self.0)
}
}

/// Not public API. Should be pub(crate).
#[doc(hidden)]
pub const SERDE_STRUCT_NAME: &'static str = "$__serde_private_RawValue";

impl<'a> Serialize for RawValue<'a> {
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_newtype_struct(SERDE_STRUCT_NAME, &self.0)
}
}

impl<'a, 'de> Deserialize<'de> for RawValue<'a>
where
'de: 'a,
{
#[inline]
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct RawValueVisitor;

impl<'de> Visitor<'de> for RawValueVisitor {
type Value = RawValue<'de>;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "a deserializable RawValue")
}

fn visit_string<E>(self, s: String) -> Result<Self::Value, E>
where
E: ::serde::de::Error,
{
Ok(RawValue(Cow::Owned(s)))
}

fn visit_byte_buf<E>(self, b: Vec<u8>) -> Result<Self::Value, E>
where
E: ::serde::de::Error,
{
String::from_utf8(b)
.map(|s| RawValue(Cow::Owned(s)))
.map_err(|err| ::serde::de::Error::custom(err))
}

fn visit_borrowed_bytes<E>(self, b: &'de [u8]) -> Result<Self::Value, E>
where
E: ::serde::de::Error,
{
::std::str::from_utf8(b)
.map(|s| RawValue(Cow::Borrowed(s)))
.map_err(|err| ::serde::de::Error::custom(err))
}
}

deserializer.deserialize_newtype_struct(SERDE_STRUCT_NAME, RawValueVisitor)
}
}
54 changes: 51 additions & 3 deletions src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::borrow::Cow;
use std::ops::Deref;
use std::{char, cmp, io, str};

Expand Down Expand Up @@ -76,6 +77,13 @@ pub trait Read<'de>: private::Sealed {
/// string until the next quotation mark but discards the data.
#[doc(hidden)]
fn ignore_str(&mut self) -> Result<()>;

/// Switch raw buffering mode on/off. When switching off, returns a copy-on-write
/// buffer with the captured data.
///
/// This is used to deserialize `RawValue`.
#[doc(hidden)]
fn toggle_raw_buffering(&mut self) -> Option<Cow<'de, [u8]>>;
}

pub struct Position {
Expand Down Expand Up @@ -107,6 +115,7 @@ where
iter: LineColIterator<io::Bytes<R>>,
/// Temporary storage of peeked byte.
ch: Option<u8>,
raw_buffer: Option<Vec<u8>>,
}

/// JSON input source that reads from a slice of bytes.
Expand All @@ -117,6 +126,7 @@ pub struct SliceRead<'a> {
slice: &'a [u8],
/// Index of the *next* byte that will be returned by next() or peek().
index: usize,
raw_buffering_start_index: Option<usize>,
}

/// JSON input source that reads from a UTF-8 string.
Expand All @@ -142,6 +152,7 @@ where
IoRead {
iter: LineColIterator::new(reader.bytes()),
ch: None,
raw_buffer: None,
}
}
}
Expand Down Expand Up @@ -193,10 +204,20 @@ where
#[inline]
fn next(&mut self) -> io::Result<Option<u8>> {
match self.ch.take() {
Some(ch) => Ok(Some(ch)),
Some(ch) => {
if let Some(ref mut buf) = self.raw_buffer {
buf.push(ch);
}
Ok(Some(ch))
}
None => match self.iter.next() {
Some(Err(err)) => Err(err),
Some(Ok(ch)) => Ok(Some(ch)),
Some(Ok(ch)) => {
if let Some(ref mut buf) = self.raw_buffer {
buf.push(ch);
}
Ok(Some(ch))
}
None => Ok(None),
},
}
Expand All @@ -219,7 +240,11 @@ where

#[inline]
fn discard(&mut self) {
self.ch = None;
if let Some(ch) = self.ch.take() {
if let Some(ref mut buf) = self.raw_buffer {
buf.push(ch);
}
}
}

fn position(&self) -> Position {
Expand Down Expand Up @@ -274,6 +299,15 @@ where
}
}
}

fn toggle_raw_buffering(&mut self) -> Option<Cow<'de, [u8]>> {
if let Some(buffer) = self.raw_buffer.take() {
Some(Cow::Owned(buffer))
} else {
self.raw_buffer = Some(Vec::new());
None
}
}
}

//////////////////////////////////////////////////////////////////////////////
Expand All @@ -284,6 +318,7 @@ impl<'a> SliceRead<'a> {
SliceRead {
slice: slice,
index: 0,
raw_buffering_start_index: None,
}
}

Expand Down Expand Up @@ -437,6 +472,15 @@ impl<'a> Read<'a> for SliceRead<'a> {
}
}
}

fn toggle_raw_buffering(&mut self) -> Option<Cow<'a, [u8]>> {
if let Some(start_index) = self.raw_buffering_start_index.take() {
Some(Cow::Borrowed(&self.slice[start_index..self.index]))
} else {
self.raw_buffering_start_index = Some(self.index);
None
}
}
}

//////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -498,6 +542,10 @@ impl<'a> Read<'a> for StrRead<'a> {
fn ignore_str(&mut self) -> Result<()> {
self.delegate.ignore_str()
}

fn toggle_raw_buffering(&mut self) -> Option<Cow<'a, [u8]>> {
self.delegate.toggle_raw_buffering()
}
}

//////////////////////////////////////////////////////////////////////////////
Expand Down
Loading