Skip to content
This repository was archived by the owner on Dec 1, 2023. It is now read-only.
This repository was archived by the owner on Dec 1, 2023. It is now read-only.

Add pop method to json Decoder #44

Closed
@steveklabnik

Description

@steveklabnik

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.

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)
       }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions