Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cannot match JsonValue::String #181

Open
giovvv opened this issue Mar 26, 2020 · 1 comment
Open

cannot match JsonValue::String #181

giovvv opened this issue Mar 26, 2020 · 1 comment

Comments

@giovvv
Copy link

giovvv commented Mar 26, 2020

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
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants