From 00337d088e16f000a1e15c8b3d64e962026ea34b Mon Sep 17 00:00:00 2001 From: Ben Navetta Date: Thu, 6 Aug 2015 23:30:08 -0400 Subject: [PATCH 1/2] implement serde::Deserialize for Value --- src/decoder/serde.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/decoder/serde.rs b/src/decoder/serde.rs index 5db9786..aeafd6a 100644 --- a/src/decoder/serde.rs +++ b/src/decoder/serde.rs @@ -280,3 +280,44 @@ impl de::Deserializer for UnitDeserializer { visitor.visit_none() } } + +// Based on https://github.com/serde-rs/serde/blob/199ed417bd6afc2071d17759b8c7e0ab8d0ba4cc/serde_json/src/value.rs#L265 +impl de::Deserialize for Value { + fn deserialize(deserializer: &mut D) -> Result { + struct ValueVisitor; + + impl de::Visitor for ValueVisitor { + type Value = Value; + + fn visit_bool(&mut self, value: bool) -> Result { + Ok(Value::Boolean(value)) + } + + fn visit_i64(&mut self, value: i64) -> Result { + Ok(Value::Integer(value)) + } + + fn visit_f64(&mut self, value: f64) -> Result { + Ok(Value::Float(value)) + } + + fn visit_str(&mut self, value: &str) -> Result { + Ok(Value::String(value.into())) + } + + fn visit_string(&mut self, value: String) -> Result { + Ok(Value::String(value)) + } + + fn visit_seq(&mut self, visitor: V) -> Result where V: de::SeqVisitor { + let values = try!(de::impls::VecVisitor::new().visit_seq(visitor)); + Ok(Value::Array(values)) + } + + fn visit_map(&mut self, visitor: V) -> Result where V: de::MapVisitor { + let values = try!(de::impls::BTreeMapVisitor::new().visit_map(visitor)); + Ok(Value::Table(values)) + } + } + } +} From 8a2282fecd01b889eadcb446c1ce896034ed0fde Mon Sep 17 00:00:00 2001 From: Ben Navetta Date: Thu, 6 Aug 2015 23:33:39 -0400 Subject: [PATCH 2/2] it helps to actually build with serde enabled --- src/decoder/serde.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/decoder/serde.rs b/src/decoder/serde.rs index aeafd6a..b12984d 100644 --- a/src/decoder/serde.rs +++ b/src/decoder/serde.rs @@ -283,7 +283,7 @@ impl de::Deserializer for UnitDeserializer { // Based on https://github.com/serde-rs/serde/blob/199ed417bd6afc2071d17759b8c7e0ab8d0ba4cc/serde_json/src/value.rs#L265 impl de::Deserialize for Value { - fn deserialize(deserializer: &mut D) -> Result { + fn deserialize(deserializer: &mut D) -> Result where D: de::Deserializer { struct ValueVisitor; impl de::Visitor for ValueVisitor { @@ -319,5 +319,7 @@ impl de::Deserialize for Value { Ok(Value::Table(values)) } } + + deserializer.visit(ValueVisitor) } }