From e3afb3c0f3b4b23b735da78da5c84cd657ed62e0 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 9 Nov 2015 18:34:28 +0100 Subject: [PATCH] Implement Serialization for FileHeaderData --- src/storage/json/parser.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/storage/json/parser.rs b/src/storage/json/parser.rs index f915e7fe..a033cdf3 100644 --- a/src/storage/json/parser.rs +++ b/src/storage/json/parser.rs @@ -1,5 +1,11 @@ use serde_json::{Value, from_str}; use serde_json::error::Result as R; +use serde_json::Serializer; +use serde::ser::Serialize; +use serde::ser::Serializer as Ser; + +use std::collections::HashMap; +use std::io::stdout; use super::super::parser::{FileHeaderParser, ParserError}; use super::super::file::{FileHeaderSpec, FileHeaderData}; @@ -35,6 +41,9 @@ impl<'a> FileHeaderParser<'a> for JsonHeaderParser<'a> { } fn write(&self, data: &FileHeaderData) -> Result { + let mut ser = Serializer::pretty(stdout()); + data.serialize(&mut ser); + Ok(String::from("")) } } @@ -65,3 +74,30 @@ fn visit_json(v: &Value) -> FileHeaderData { } } } + +impl Serialize for FileHeaderData { + + fn serialize(&self, ser: &mut S) -> Result<(), S::Error> + where S: Ser + { + match self { + &FileHeaderData::Null => { + let o : Option = None; + o.serialize(ser) + }, + &FileHeaderData::Bool(ref b) => b.serialize(ser), + &FileHeaderData::Integer(ref i) => i.serialize(ser), + &FileHeaderData::UInteger(ref u) => u.serialize(ser), + &FileHeaderData::Float(ref f) => f.serialize(ser), + &FileHeaderData::Text(ref s) => (&s[..]).serialize(ser), + &FileHeaderData::Array{values: ref vs} => vs.serialize(ser), + &FileHeaderData::Map{keys: ref ks} => ks.serialize(ser), + &FileHeaderData::Key{name: ref n, value: ref v} => { + let mut hm = HashMap::new(); + hm.insert(n, v); + hm.serialize(ser) + } + } + } + +}