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

How does serde_json serialize Vec<u8> into binary instead of an array? #981

Closed
silence-coding opened this issue Feb 8, 2023 · 1 comment

Comments

@silence-coding
Copy link

silence-coding commented Feb 8, 2023

How does serde_json serialize Vec into binary rather than an array? Serializing arrays takes up space exponentially.

use serde::{Serialize};
#[derive(Serialize)]
struct Test1{
    key: Vec<u8>
}
#[derive(Serialize)]
struct Test2{
    key: String
}

fn main() {
    let data = "xxxxxxxxxxxxxxxx";
    let test_vec = Test1 {
        key: data.as_bytes().to_vec(),
    };
    println!("{}", serde_json::to_vec(&test_vec).unwrap().len()); // 73

    let test2 = Test2 {
        key: data.to_string(),
    };
    println!("{}", serde_json::to_vec(&test2).unwrap().len()); //26
}
@dtolnay
Copy link
Member

dtolnay commented Feb 8, 2023

JSON does not have a binary data type. You would need to use a different format if you want binary bytes embedded into it.

@dtolnay dtolnay closed this as completed Feb 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants