I have the Encodable trait for Dao struct right, but how do I implement the Decodable trait to read the BTreeMap back and assign it into the inner field of the struct?
pub struct Dao {
pub values: BTreeMap<String, Value>,
}
/// custom Encoder for Dao,
/// decodes directly the content of `values`, instead of `values` as field of this `Dao` struct
impl Encodable for Dao {
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
self.values.encode(s)
}
}
impl Decodable for Dao{
fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error> {
d.read_map(|a,b|{
let map: BTreeMap<String, Value> = Decodable::decode(a).unwrap();// this is invalid here
let dao = Dao{values: map};
Ok(dao)
})
}
}
The Json String for the BTreeMap
{
"active": true,
"barcode": null,
"client_id": null,
"created": "2015-11-19T17:10:42.246382+00:00",
"created_by": null,
"currency_id": null,
"description": "Second hand Iphone4s",
"help": null,
"info": null,
"is_service": false,
"name": "iphone4s",
"organization_id": null,
"owner_id": "3e51d5f9-5bff-4664-9946-47bf37973636",
"parent_product_id": null,
"price": 7000,
"priority": null,
"product_id": "f7521093-734d-488a-9f60-fc9f11f7e750",
"seq_no": null,
"tags": null,
"unit": null,
"updated": "2015-11-19T17:10:42.246382+00:00",
"updated_by": null,
"upfront_fee": 0,
"use_parent_price": false
}