mhristache
mhristache

Reputation: 2188

Deserialize JSON with irregular content

I have the example below, where 2 "unusual" things can happen:

Is there a way to de-serialise an irregular JSON string using automatic deriving? Which is the easiest/better way to handle this kind of irregular JSON in Rust? Can I avoid writing a very complex match based code to check for every possible combination?

extern crate serialize;

static JSON: &'static str  = r#"
    {
        "status": {
            "status": "OK"
        },
        "data": {
            "container": {
                "key": "value",
                "list": [
                    {
                        "key1": "value1",
                        "key2": "value2"
                    },
                    {
                        "key1": "value1"
                    }
                ]
            }
        }
    }"#;
#[deriving(Decodable, Show)]
struct Top {
    status: Status,
    data: Data,
}

#[deriving(Decodable, Show)]
struct Data {
    container: Container,
}

#[deriving(Decodable, Show)]
struct Status {
    status: String,
}

#[deriving(Decodable, Show)]
struct Container {
    key: String,
    list: Vec<KeyVals>,
}

#[deriving(Decodable, Show)]
struct KeyVals {
    key1: String,
    key2: String,
}

fn main() {
    let result: Top = match serialize::json::decode(JSON) {
        Ok(x) => x,
        Err(why) => fail!("Failed decoding the JSON! Reason: {}", why),
    };
    println!("{}", result);
}

When running the code, it fails because the second element of the list is missing the key2 attribute.

task '<main>' failed at 'Failed decoding the JSON! Reason: MissingFieldError(key2)', hello.rs:56

Thank you

Upvotes: 3

Views: 534

Answers (1)

A.B.
A.B.

Reputation: 16630

Potentially existing data can be represented via an enum. In the simplest case, with an Option.

I believe using an enum will also solve your problem.

#[deriving(Encodable, Decodable)]
enum Status {
    Good(Container),
    Bad,
    VeryBad
}

If the Container also contains potentially existing data, then you can again use an enum to represent that.

Upvotes: 3

Related Questions