mirror of
https://github.com/serde-rs/json.git
synced 2025-10-02 15:26:00 +00:00
Cfg gate everything having to do with RawValue
This commit is contained in:
parent
7e373b6125
commit
f96b59d6b3
@ -41,3 +41,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 = []
|
||||
|
@ -947,6 +947,7 @@ 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>,
|
||||
@ -1426,10 +1427,14 @@ impl<'de, 'a, R: Read<'de>> de::Deserializer<'de> for &'a mut Deserializer<R> {
|
||||
where
|
||||
V: de::Visitor<'de>,
|
||||
{
|
||||
if name == ::raw::SERDE_STRUCT_NAME {
|
||||
return self.deserialize_raw_value(visitor);
|
||||
#[cfg(feature = "raw_value")]
|
||||
{
|
||||
if name == ::raw::SERDE_STRUCT_NAME {
|
||||
return self.deserialize_raw_value(visitor);
|
||||
}
|
||||
}
|
||||
|
||||
let _ = name;
|
||||
visitor.visit_newtype_struct(self)
|
||||
}
|
||||
|
||||
|
10
src/lib.rs
10
src/lib.rs
@ -364,7 +364,11 @@ 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, RawValue, Value};
|
||||
pub use self::value::{from_value, to_value, Map, Number, Value};
|
||||
|
||||
#[cfg(feature = "raw_value")]
|
||||
#[doc(inline)]
|
||||
pub use self::value::RawValue;
|
||||
|
||||
// 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%.
|
||||
@ -388,5 +392,7 @@ pub mod value;
|
||||
|
||||
mod iter;
|
||||
mod number;
|
||||
mod raw;
|
||||
mod read;
|
||||
|
||||
#[cfg(feature = "raw_value")]
|
||||
mod raw;
|
||||
|
37
src/read.rs
37
src/read.rs
@ -9,11 +9,14 @@
|
||||
use std::ops::Deref;
|
||||
use std::{char, cmp, io, str};
|
||||
|
||||
#[cfg(feature = "raw_value")]
|
||||
use serde::de::Visitor;
|
||||
|
||||
use iter::LineColIterator;
|
||||
|
||||
use error::{Error, ErrorCode, Result};
|
||||
|
||||
#[cfg(feature = "raw_value")]
|
||||
use raw::{BorrowedRawDeserializer, OwnedRawDeserializer};
|
||||
|
||||
/// Trait used by the deserializer for iterating over input. This is manually
|
||||
@ -83,11 +86,13 @@ pub trait Read<'de>: private::Sealed {
|
||||
/// Switch raw buffering mode on.
|
||||
///
|
||||
/// This is used when deserializing `RawValue`.
|
||||
#[cfg(feature = "raw_value")]
|
||||
#[doc(hidden)]
|
||||
fn begin_raw_buffering(&mut self);
|
||||
|
||||
/// Switch raw buffering mode off and provides the raw buffered data to the
|
||||
/// given visitor.
|
||||
#[cfg(feature = "raw_value")]
|
||||
#[doc(hidden)]
|
||||
fn end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value>
|
||||
where
|
||||
@ -123,6 +128,7 @@ where
|
||||
iter: LineColIterator<io::Bytes<R>>,
|
||||
/// Temporary storage of peeked byte.
|
||||
ch: Option<u8>,
|
||||
#[cfg(feature = "raw_value")]
|
||||
raw_buffer: Option<Vec<u8>>,
|
||||
}
|
||||
|
||||
@ -134,6 +140,7 @@ pub struct SliceRead<'a> {
|
||||
slice: &'a [u8],
|
||||
/// Index of the *next* byte that will be returned by next() or peek().
|
||||
index: usize,
|
||||
#[cfg(feature = "raw_value")]
|
||||
raw_buffering_start_index: usize,
|
||||
}
|
||||
|
||||
@ -142,6 +149,7 @@ pub struct SliceRead<'a> {
|
||||
// Able to elide UTF-8 checks by assuming that the input is valid UTF-8.
|
||||
pub struct StrRead<'a> {
|
||||
delegate: SliceRead<'a>,
|
||||
#[cfg(feature = "raw_value")]
|
||||
data: &'a str,
|
||||
}
|
||||
|
||||
@ -161,6 +169,7 @@ where
|
||||
IoRead {
|
||||
iter: LineColIterator::new(reader.bytes()),
|
||||
ch: None,
|
||||
#[cfg(feature = "raw_value")]
|
||||
raw_buffer: None,
|
||||
}
|
||||
}
|
||||
@ -214,16 +223,22 @@ where
|
||||
fn next(&mut self) -> io::Result<Option<u8>> {
|
||||
match self.ch.take() {
|
||||
Some(ch) => {
|
||||
if let Some(ref mut buf) = self.raw_buffer {
|
||||
buf.push(ch);
|
||||
#[cfg(feature = "raw_value")]
|
||||
{
|
||||
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)) => {
|
||||
if let Some(ref mut buf) = self.raw_buffer {
|
||||
buf.push(ch);
|
||||
#[cfg(feature = "raw_value")]
|
||||
{
|
||||
if let Some(ref mut buf) = self.raw_buffer {
|
||||
buf.push(ch);
|
||||
}
|
||||
}
|
||||
Ok(Some(ch))
|
||||
}
|
||||
@ -247,7 +262,13 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "raw_value"))]
|
||||
#[inline]
|
||||
fn discard(&mut self) {
|
||||
self.ch = None;
|
||||
}
|
||||
|
||||
#[cfg(feature = "raw_value")]
|
||||
fn discard(&mut self) {
|
||||
if let Some(ch) = self.ch.take() {
|
||||
if let Some(ref mut buf) = self.raw_buffer {
|
||||
@ -309,10 +330,12 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "raw_value")]
|
||||
fn begin_raw_buffering(&mut self) {
|
||||
self.raw_buffer = Some(Vec::new());
|
||||
}
|
||||
|
||||
#[cfg(feature = "raw_value")]
|
||||
fn end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
@ -331,6 +354,7 @@ impl<'a> SliceRead<'a> {
|
||||
SliceRead {
|
||||
slice: slice,
|
||||
index: 0,
|
||||
#[cfg(feature = "raw_value")]
|
||||
raw_buffering_start_index: 0,
|
||||
}
|
||||
}
|
||||
@ -486,10 +510,12 @@ impl<'a> Read<'a> for SliceRead<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "raw_value")]
|
||||
fn begin_raw_buffering(&mut self) {
|
||||
self.raw_buffering_start_index = self.index;
|
||||
}
|
||||
|
||||
#[cfg(feature = "raw_value")]
|
||||
fn end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value>
|
||||
where
|
||||
V: Visitor<'a>,
|
||||
@ -507,6 +533,7 @@ impl<'a> StrRead<'a> {
|
||||
pub fn new(s: &'a str) -> Self {
|
||||
StrRead {
|
||||
delegate: SliceRead::new(s.as_bytes()),
|
||||
#[cfg(feature = "raw_value")]
|
||||
data: s,
|
||||
}
|
||||
}
|
||||
@ -561,10 +588,12 @@ impl<'a> Read<'a> for StrRead<'a> {
|
||||
self.delegate.ignore_str()
|
||||
}
|
||||
|
||||
#[cfg(feature = "raw_value")]
|
||||
fn begin_raw_buffering(&mut self) {
|
||||
self.delegate.begin_raw_buffering()
|
||||
}
|
||||
|
||||
#[cfg(feature = "raw_value")]
|
||||
fn end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value>
|
||||
where
|
||||
V: Visitor<'a>,
|
||||
|
15
src/ser.rs
15
src/ser.rs
@ -460,6 +460,7 @@ where
|
||||
match name {
|
||||
#[cfg(feature = "arbitrary_precision")]
|
||||
::number::SERDE_STRUCT_NAME => Ok(Compound::Number { ser: self }),
|
||||
#[cfg(feature = "raw_value")]
|
||||
::raw::SERDE_STRUCT_NAME => Ok(Compound::RawValue { ser: self }),
|
||||
_ => self.serialize_map(Some(len)),
|
||||
}
|
||||
@ -571,6 +572,7 @@ pub enum Compound<'a, W: 'a, F: 'a> {
|
||||
},
|
||||
#[cfg(feature = "arbitrary_precision")]
|
||||
Number { ser: &'a mut Serializer<W, F> },
|
||||
#[cfg(feature = "raw_value")]
|
||||
RawValue { ser: &'a mut Serializer<W, F> },
|
||||
}
|
||||
|
||||
@ -608,6 +610,7 @@ where
|
||||
}
|
||||
#[cfg(feature = "arbitrary_precision")]
|
||||
Compound::Number { .. } => unreachable!(),
|
||||
#[cfg(feature = "raw_value")]
|
||||
Compound::RawValue { .. } => unreachable!(),
|
||||
}
|
||||
}
|
||||
@ -624,6 +627,7 @@ where
|
||||
}
|
||||
#[cfg(feature = "arbitrary_precision")]
|
||||
Compound::Number { .. } => unreachable!(),
|
||||
#[cfg(feature = "raw_value")]
|
||||
Compound::RawValue { .. } => unreachable!(),
|
||||
}
|
||||
}
|
||||
@ -707,6 +711,7 @@ where
|
||||
}
|
||||
#[cfg(feature = "arbitrary_precision")]
|
||||
Compound::Number { .. } => unreachable!(),
|
||||
#[cfg(feature = "raw_value")]
|
||||
Compound::RawValue { .. } => unreachable!(),
|
||||
}
|
||||
}
|
||||
@ -748,6 +753,7 @@ where
|
||||
}
|
||||
#[cfg(feature = "arbitrary_precision")]
|
||||
Compound::Number { .. } => unreachable!(),
|
||||
#[cfg(feature = "raw_value")]
|
||||
Compound::RawValue { .. } => unreachable!(),
|
||||
}
|
||||
}
|
||||
@ -774,6 +780,7 @@ where
|
||||
}
|
||||
#[cfg(feature = "arbitrary_precision")]
|
||||
Compound::Number { .. } => unreachable!(),
|
||||
#[cfg(feature = "raw_value")]
|
||||
Compound::RawValue { .. } => unreachable!(),
|
||||
}
|
||||
}
|
||||
@ -790,6 +797,7 @@ where
|
||||
}
|
||||
#[cfg(feature = "arbitrary_precision")]
|
||||
Compound::Number { .. } => unreachable!(),
|
||||
#[cfg(feature = "raw_value")]
|
||||
Compound::RawValue { .. } => unreachable!(),
|
||||
}
|
||||
}
|
||||
@ -822,6 +830,7 @@ where
|
||||
Err(invalid_number())
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "raw_value")]
|
||||
Compound::RawValue { ref mut ser, .. } => {
|
||||
if key == ::raw::SERDE_STRUCT_FIELD_NAME {
|
||||
try!(value.serialize(RawValueStrEmitter(&mut *ser)));
|
||||
@ -839,6 +848,7 @@ where
|
||||
Compound::Map { .. } => ser::SerializeMap::end(self),
|
||||
#[cfg(feature = "arbitrary_precision")]
|
||||
Compound::Number { .. } => Ok(()),
|
||||
#[cfg(feature = "raw_value")]
|
||||
Compound::RawValue { .. } => Ok(()),
|
||||
}
|
||||
}
|
||||
@ -861,6 +871,7 @@ where
|
||||
Compound::Map { .. } => ser::SerializeStruct::serialize_field(self, key, value),
|
||||
#[cfg(feature = "arbitrary_precision")]
|
||||
Compound::Number { .. } => unreachable!(),
|
||||
#[cfg(feature = "raw_value")]
|
||||
Compound::RawValue { .. } => unreachable!(),
|
||||
}
|
||||
}
|
||||
@ -883,6 +894,7 @@ where
|
||||
}
|
||||
#[cfg(feature = "arbitrary_precision")]
|
||||
Compound::Number { .. } => unreachable!(),
|
||||
#[cfg(feature = "raw_value")]
|
||||
Compound::RawValue { .. } => unreachable!(),
|
||||
}
|
||||
}
|
||||
@ -897,6 +909,7 @@ fn invalid_number() -> Error {
|
||||
Error::syntax(ErrorCode::InvalidNumber, 0, 0)
|
||||
}
|
||||
|
||||
#[cfg(feature = "raw_value")]
|
||||
fn invalid_raw_value() -> Error {
|
||||
Error::syntax(ErrorCode::ExpectedSomeValue, 0, 0)
|
||||
}
|
||||
@ -1420,8 +1433,10 @@ impl<'a, W: io::Write, F: Formatter> ser::Serializer for NumberStrEmitter<'a, W,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "raw_value")]
|
||||
struct RawValueStrEmitter<'a, W: 'a + io::Write, F: 'a + Formatter>(&'a mut Serializer<W, F>);
|
||||
|
||||
#[cfg(feature = "raw_value")]
|
||||
impl<'a, W: io::Write, F: Formatter> ser::Serializer for RawValueStrEmitter<'a, W, F> {
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
@ -118,6 +118,7 @@ impl<'de> Deserialize<'de> for Value {
|
||||
let number: NumberFromString = visitor.next_value()?;
|
||||
Ok(Value::Number(number.value))
|
||||
}
|
||||
#[cfg(feature = "raw_value")]
|
||||
Some(KeyClass::RawValue) => {
|
||||
let value = visitor.next_value_seed(::raw::RawValueFromString)?;
|
||||
::from_str(value.as_ref()).map_err(de::Error::custom)
|
||||
@ -310,12 +311,16 @@ impl<'de> serde::Deserializer<'de> for Value {
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
if name == ::raw::SERDE_STRUCT_NAME {
|
||||
return visitor.visit_map(::raw::OwnedRawDeserializer {
|
||||
raw_value: Some(self.to_string()),
|
||||
});
|
||||
#[cfg(feature = "raw_value")]
|
||||
{
|
||||
if name == ::raw::SERDE_STRUCT_NAME {
|
||||
return visitor.visit_map(::raw::OwnedRawDeserializer {
|
||||
raw_value: Some(self.to_string()),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
let _ = name;
|
||||
visitor.visit_newtype_struct(self)
|
||||
}
|
||||
|
||||
@ -845,12 +850,16 @@ impl<'de> serde::Deserializer<'de> for &'de Value {
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
if name == ::raw::SERDE_STRUCT_NAME {
|
||||
return visitor.visit_map(::raw::OwnedRawDeserializer {
|
||||
raw_value: Some(self.to_string()),
|
||||
});
|
||||
#[cfg(feature = "raw_value")]
|
||||
{
|
||||
if name == ::raw::SERDE_STRUCT_NAME {
|
||||
return visitor.visit_map(::raw::OwnedRawDeserializer {
|
||||
raw_value: Some(self.to_string()),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
let _ = name;
|
||||
visitor.visit_newtype_struct(self)
|
||||
}
|
||||
|
||||
@ -1308,6 +1317,7 @@ enum KeyClass {
|
||||
Map(String),
|
||||
#[cfg(feature = "arbitrary_precision")]
|
||||
Number,
|
||||
#[cfg(feature = "raw_value")]
|
||||
RawValue,
|
||||
}
|
||||
|
||||
@ -1336,6 +1346,7 @@ impl<'de> Visitor<'de> for KeyClassifier {
|
||||
match s {
|
||||
#[cfg(feature = "arbitrary_precision")]
|
||||
::number::SERDE_STRUCT_FIELD_NAME => Ok(KeyClass::Number),
|
||||
#[cfg(feature = "raw_value")]
|
||||
::raw::SERDE_STRUCT_FIELD_NAME => Ok(KeyClass::RawValue),
|
||||
_ => Ok(KeyClass::Map(s.to_owned())),
|
||||
}
|
||||
@ -1348,6 +1359,7 @@ impl<'de> Visitor<'de> for KeyClassifier {
|
||||
match s.as_str() {
|
||||
#[cfg(feature = "arbitrary_precision")]
|
||||
::number::SERDE_STRUCT_FIELD_NAME => Ok(KeyClass::Number),
|
||||
#[cfg(feature = "raw_value")]
|
||||
::raw::SERDE_STRUCT_FIELD_NAME => Ok(KeyClass::RawValue),
|
||||
_ => Ok(KeyClass::Map(s)),
|
||||
}
|
||||
|
@ -118,6 +118,8 @@ use serde::ser::Serialize;
|
||||
use error::Error;
|
||||
pub use map::Map;
|
||||
pub use number::Number;
|
||||
|
||||
#[cfg(feature = "raw_value")]
|
||||
pub use raw::RawValue;
|
||||
|
||||
pub use self::index::Index;
|
||||
|
@ -230,6 +230,7 @@ impl serde::Serializer for Serializer {
|
||||
match name {
|
||||
#[cfg(feature = "arbitrary_precision")]
|
||||
::number::SERDE_STRUCT_NAME => Ok(SerializeMap::Number { out_value: None }),
|
||||
#[cfg(feature = "raw_value")]
|
||||
::raw::SERDE_STRUCT_NAME => Ok(SerializeMap::RawValue { out_value: None }),
|
||||
_ => self.serialize_map(Some(len)),
|
||||
}
|
||||
@ -265,6 +266,7 @@ pub enum SerializeMap {
|
||||
},
|
||||
#[cfg(feature = "arbitrary_precision")]
|
||||
Number { out_value: Option<Value> },
|
||||
#[cfg(feature = "raw_value")]
|
||||
RawValue { out_value: Option<Value> },
|
||||
}
|
||||
|
||||
@ -360,6 +362,7 @@ impl serde::ser::SerializeMap for SerializeMap {
|
||||
}
|
||||
#[cfg(feature = "arbitrary_precision")]
|
||||
SerializeMap::Number { .. } => unreachable!(),
|
||||
#[cfg(feature = "raw_value")]
|
||||
SerializeMap::RawValue { .. } => unreachable!(),
|
||||
}
|
||||
}
|
||||
@ -382,6 +385,7 @@ impl serde::ser::SerializeMap for SerializeMap {
|
||||
}
|
||||
#[cfg(feature = "arbitrary_precision")]
|
||||
SerializeMap::Number { .. } => unreachable!(),
|
||||
#[cfg(feature = "raw_value")]
|
||||
SerializeMap::RawValue { .. } => unreachable!(),
|
||||
}
|
||||
}
|
||||
@ -391,6 +395,7 @@ impl serde::ser::SerializeMap for SerializeMap {
|
||||
SerializeMap::Map { map, .. } => Ok(Value::Object(map)),
|
||||
#[cfg(feature = "arbitrary_precision")]
|
||||
SerializeMap::Number { .. } => unreachable!(),
|
||||
#[cfg(feature = "raw_value")]
|
||||
SerializeMap::RawValue { .. } => unreachable!(),
|
||||
}
|
||||
}
|
||||
@ -601,6 +606,7 @@ impl serde::ser::SerializeStruct for SerializeMap {
|
||||
Err(invalid_number())
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "raw_value")]
|
||||
SerializeMap::RawValue { ref mut out_value } => {
|
||||
if key == ::raw::SERDE_STRUCT_FIELD_NAME {
|
||||
*out_value = Some(value.serialize(RawValueEmitter)?);
|
||||
@ -619,6 +625,7 @@ impl serde::ser::SerializeStruct for SerializeMap {
|
||||
SerializeMap::Number { out_value, .. } => {
|
||||
Ok(out_value.expect("number value was not emitted"))
|
||||
}
|
||||
#[cfg(feature = "raw_value")]
|
||||
SerializeMap::RawValue { out_value, .. } => {
|
||||
Ok(out_value.expect("raw value was not emitted"))
|
||||
}
|
||||
@ -826,12 +833,15 @@ impl ser::Serializer for NumberValueEmitter {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "raw_value")]
|
||||
struct RawValueEmitter;
|
||||
|
||||
#[cfg(feature = "raw_value")]
|
||||
fn invalid_raw_value() -> Error {
|
||||
Error::syntax(ErrorCode::ExpectedSomeValue, 0, 0)
|
||||
}
|
||||
|
||||
#[cfg(feature = "raw_value")]
|
||||
impl ser::Serializer for RawValueEmitter {
|
||||
type Ok = Value;
|
||||
type Error = Error;
|
||||
|
@ -44,7 +44,7 @@ use serde_bytes::{ByteBuf, Bytes};
|
||||
|
||||
use serde_json::{
|
||||
from_reader, from_slice, from_str, from_value, to_string, to_string_pretty, to_value, to_vec,
|
||||
to_writer, Deserializer, Number, RawValue, Value,
|
||||
to_writer, Deserializer, Number, Value,
|
||||
};
|
||||
|
||||
macro_rules! treemap {
|
||||
@ -2040,8 +2040,11 @@ fn test_integer128() {
|
||||
]);
|
||||
}
|
||||
|
||||
#[cfg(feature = "raw_value")]
|
||||
#[test]
|
||||
fn test_raw_value() {
|
||||
use serde_json::RawValue;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct Wrapper<'a> {
|
||||
a: i8,
|
||||
|
Loading…
x
Reference in New Issue
Block a user