maciejhirsz / json-rust

JSON implementation in Rust
Apache License 2.0
563 stars 63 forks source link

cannot match JsonValue::String #181

Open giovvv opened 4 years ago

giovvv commented 4 years ago

When trying to match the elements of a Json array by their type, it seems it is not (easily) possible to do it for strings:

    let data = array!["fii", "foo", 4];
    for i in data.members() {
        println!("examining {}", i);
        match i {
            JsonValue::Number(_) => println!("  it's a number"),
            JsonValue::String(_) => println!("  it's a string"),
            _ if i.is_string()   => println!("  it's really a string"),
            _ => println!("  it's something else"),
        }

While the JsonValue::Number arm works as expected, the JsonValue::String arm is never entered. Only _ if i.is_string() works. Perhaps this is the expected behaviour (I'm a rust beginner so I am not sure), but certainly it is not very intuitive...

maciejhirsz commented 4 years ago

If your strings are short they will be inlined on stack and use JsonValue::Short instead of JsonValue::String.

That said, the next big release of the crate will remove the Short variant, and use Cow<str> for strings instead.