You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Dec 1, 2023. It is now read-only.
This issue was labelled with: in the Rust repository
Now it's impossible to implement Decodable for a map with different value types
because read_* methods lose the stack head anyway.
Decodable doesn't have a method returning the stack head as-is without any type casting.
Let's look a case where Json dictionary has values with int and String types.
extern crate serialize;
use serialize::{Decodable, Decoder};
use serialize::json::{Json, Boolean, String, Null, I64, U64, F64};
use serialize::json;
#[deriving(Show)]
struct Primitive(Json);
impl<S: Decoder<E>, E> Decodable<S, E> for Primitive {
fn decode(decoder: &mut S) -> Result<Primitive, E> {
match decoder.pop() {
n @ I64(_) => Primitive(n),
n @ U64(_) => Primitive(n),
n @ F64(_) => Primitive(n),
s @ String(_) => Primitive(s),
bad => fail!("bad {}", bad)
}
}
}
The text was updated successfully, but these errors were encountered:
Note: Decodable is not a generic trait. The decode function is generic instead.
Note2: You can derive the Decodable implementation for BTreeMap<String, Option<String>> or any other enum type that derives Decodable as Value.
Obviously Note2 does not work to decode arbitrary json formats, only those that also were encoded by a derived Encodable trait.
As the decoder could also be any other decoder (like a binary decoder), the type of the field to read might not be available. Implementing a generic pop is therefor not possible. Making the pop method from json::Decoder public and waiting for negative trait bounds might work (rust-lang/rfcs#586).
I'm going to close this now that this crate is deprecated in favor of serde. We're discontinuing feature development in rustc-serialize but will still continue to merge bug fixes if they arise.
Issue by yaitskov
Thursday Sep 18, 2014 at 20:14 GMT
For earlier discussion, see rust-lang/rust#17377
This issue was labelled with: in the Rust repository
Now it's impossible to implement Decodable for a map with different value types
because read_* methods lose the stack head anyway.
Decodable doesn't have a method returning the stack head as-is without any type casting.
Let's look a case where Json dictionary has values with int and String types.
The text was updated successfully, but these errors were encountered: