Skip to content
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

Added support for feature = preserve_order #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ readme = "README.md"
keywords = ["serde"]
license = "MIT"

[features]
preserve_order = ["indexmap"]

[dependencies]
serde = "^1.0.0"
ordered-float = "^1.0.1"
indexmap = { version = "1.0.2", optional = true, features = ["serde-1"] }

[dev-dependencies]
serde_derive = "^1.0.0"
5 changes: 2 additions & 3 deletions src/de.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use serde::de;
use std::collections::BTreeMap;
use std::error::Error;
use std::fmt;

use Value;
use {Value, MapImpl};

#[derive(Debug)]
pub enum Unexpected {
Expand Down Expand Up @@ -274,7 +273,7 @@ impl<'de> de::Visitor<'de> for ValueVisitor {
}

fn visit_map<V: de::MapAccess<'de>>(self, mut visitor: V) -> Result<Value, V::Error> {
let mut values = BTreeMap::new();
let mut values = MapImpl::new();
while let Some((key, value)) = try!(visitor.next_entry()) {
values.insert(key, value);
}
Expand Down
32 changes: 28 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@
#[macro_use]
extern crate serde;
extern crate ordered_float;
#[cfg(feature = "preserve_order")]
extern crate indexmap;

#[cfg(test)]
#[macro_use]
extern crate serde_derive;

use std::collections::BTreeMap;
#[cfg(feature = "preserve_order")]
pub(crate) use indexmap::IndexMap as MapImpl;

#[cfg(not(feature = "preserve_order"))]
pub(crate) use std::collections::BTreeMap as MapImpl;

use std::cmp::Ordering;
use std::hash::{Hash, Hasher};
use serde::Deserialize;
Expand Down Expand Up @@ -44,7 +51,7 @@ pub enum Value {
Option(Option<Box<Value>>),
Newtype(Box<Value>),
Seq(Vec<Value>),
Map(BTreeMap<Value, Value>),
Map(MapImpl<Value, Value>),
Bytes(Vec<u8>),
}

Expand Down Expand Up @@ -72,7 +79,17 @@ impl Hash for Value {
Value::Option(ref v) => v.hash(hasher),
Value::Newtype(ref v) => v.hash(hasher),
Value::Seq(ref v) => v.hash(hasher),
Value::Map(ref v) => v.hash(hasher),
Value::Map(ref v) => {
#[cfg(feature = "preserve_order")] {
for (key, value) in v.iter() {
key.hash(hasher);
value.hash(hasher);
}
}
#[cfg(not(feature = "preserve_order"))] {
v.hash(hasher)
}
}
Value::Bytes(ref v) => v.hash(hasher),
}
}
Expand Down Expand Up @@ -125,7 +142,14 @@ impl Ord for Value {
(&Value::Option(ref v0), &Value::Option(ref v1)) => v0.cmp(v1),
(&Value::Newtype(ref v0), &Value::Newtype(ref v1)) => v0.cmp(v1),
(&Value::Seq(ref v0), &Value::Seq(ref v1)) => v0.cmp(v1),
(&Value::Map(ref v0), &Value::Map(ref v1)) => v0.cmp(v1),
(&Value::Map(ref v0), &Value::Map(ref v1)) => {
#[cfg(feature = "preserve_order")] {
v0.iter().cmp(v1.iter())
}
#[cfg(not(feature = "preserve_order"))] {
v0.cmp(v1)
}
},
(&Value::Bytes(ref v0), &Value::Bytes(ref v1)) => v0.cmp(v1),
(ref v0, ref v1) => v0.discriminant().cmp(&v1.discriminant()),
}
Expand Down
15 changes: 7 additions & 8 deletions src/ser.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use serde::ser;
use std::collections::BTreeMap;
use std::error::Error;
use std::fmt;

use Value;
use {Value, MapImpl};

#[derive(Debug)]
pub enum SerializerError {
Expand Down Expand Up @@ -224,15 +223,15 @@ impl ser::Serializer for Serializer {
self,
_len: Option<usize>
) -> Result<Self::SerializeMap, Self::Error> {
Ok(SerializeMap { map: BTreeMap::new(), key: None })
Ok(SerializeMap { map: MapImpl::new(), key: None })
}

fn serialize_struct(
self,
_name: &'static str,
_len: usize
) -> Result<Self::SerializeStruct, Self::Error> {
Ok(SerializeStruct(BTreeMap::new()))
Ok(SerializeStruct(MapImpl::new()))
}

fn serialize_struct_variant(
Expand All @@ -242,7 +241,7 @@ impl ser::Serializer for Serializer {
_variant: &'static str,
_len: usize
) -> Result<Self::SerializeStructVariant, Self::Error> {
Ok(SerializeStructVariant(BTreeMap::new()))
Ok(SerializeStructVariant(MapImpl::new()))
}
}

Expand Down Expand Up @@ -339,7 +338,7 @@ impl ser::SerializeTupleVariant for SerializeTupleVariant {
}

struct SerializeMap {
map: BTreeMap<Value, Value>,
map: MapImpl<Value, Value>,
key: Option<Value>,
}

Expand Down Expand Up @@ -373,7 +372,7 @@ impl ser::SerializeMap for SerializeMap {
}
}

struct SerializeStruct(BTreeMap<Value, Value>);
struct SerializeStruct(MapImpl<Value, Value>);

impl ser::SerializeStruct for SerializeStruct {
type Ok = Value;
Expand All @@ -398,7 +397,7 @@ impl ser::SerializeStruct for SerializeStruct {
}
}

struct SerializeStructVariant(BTreeMap<Value, Value>);
struct SerializeStructVariant(MapImpl<Value, Value>);

impl ser::SerializeStructVariant for SerializeStructVariant {
type Ok = Value;
Expand Down