Skip to content

serialize::json::Encoder emit_char serializes extra nulls #19719

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

Closed
emk opened this issue Dec 11, 2014 · 2 comments · Fixed by #19764
Closed

serialize::json::Encoder emit_char serializes extra nulls #19719

emk opened this issue Dec 11, 2014 · 2 comments · Fixed by #19764

Comments

@emk
Copy link
Contributor

emk commented Dec 11, 2014

The following code snippet:

extern crate serialize;

fn main() {
    let s = serialize::json::encode(&'c');
    println!("Encoded: {}, bytes: {}", s, s.as_bytes());
    assert_eq!("\"c\"", s.as_slice());
}

Produces the following output:

Encoded: "c", bytes: [34, 99, 0, 0, 0, 34]
task '<main>' panicked at 'assertion failed: `(left == right) && (right == left)` (left: `"c"`, right: `"c"`)', <anon>:6
playpen: application terminated with error code 101

The serialized string should be [34, 99, 34], and not [34, 99, 0, 0, 0, 34].

@lifthrasiir
Copy link
Contributor

fn escape_char(writer: &mut io::Writer, v: char) -> Result<(), io::IoError> {
    let mut buf = [0, .. 4];
    v.encode_utf8(&mut buf);
    escape_bytes(writer, &mut buf)
}

Ugh, this should be:

fn escape_char(writer: &mut io::Writer, v: char) -> Result<(), io::IoError> {
    let mut buf = [0, .. 4];
    let sz = v.encode_utf8(&mut buf).unwrap();
    escape_bytes(writer, buf[mut ..sz])
}

@emk
Copy link
Contributor Author

emk commented Dec 12, 2014

I didn't see that @lifthrasiir had prepared a pull request, so I made a patch of my own. Oh well, it was a nice learning experience, and it helps prepare me to work on some of the stranger issues with Encode/Decode, so it's all good. :-)

@lifthrasiir's patch looks good.

brson added a commit to brson/rust that referenced this issue Dec 16, 2014
alexcrichton added a commit to alexcrichton/rust that referenced this issue Dec 17, 2014
lnicola pushed a commit to lnicola/rust that referenced this issue May 5, 2025
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

Successfully merging a pull request may close this issue.

2 participants