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

Serde vec to Bson::Binary? #113

Closed
JohanGoversTech opened this issue Jan 9, 2019 · 1 comment
Closed

Serde vec to Bson::Binary? #113

JohanGoversTech opened this issue Jan 9, 2019 · 1 comment

Comments

@JohanGoversTech
Copy link

Why convert a Vec<u8> not in a Bson::Binary?

#[test]
fn test_byte_array() {
	
	#[derive(Serialize, Deserialize, PartialEq, Debug)]
    struct Foo {
        a: Vec<u8>,
    }
	
	let foo = Foo { a: vec![100u8; 400]};
	
	let e = Encoder::new();
    let r = match foo.serialize(e) {
        Ok(b) => b,
        Err(e) => panic!("Failed to serialize: {}", e),
    };
    
    println!("result {:?}", r);
}

Result in the error:
thread 'test_byte_array' panicked at 'Failed to serialize: BSON does not support unsigned type', test.rs:88:19
I would expect a Bson::Binary element to be created.

@JohanGoversTech
Copy link
Author

I found the solution, you have to use serde_bytes

#[test]
fn test_byte_array() {
	
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    struct Foo {
        #[serde(with = "serde_bytes")]
        a: Vec<u8>,
    }
	
    let foo = Foo { a: vec![100u8; 400]};
	
    let e = Encoder::new();
    let r = match foo.serialize(e) {
        Ok(b) => b,
        Err(e) => panic!("Failed to serialize: {}", e),
    };
    
    println!("result {:?}", r);
}

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

1 participant