Idiomatic escaping of Unicode sequences

I consume a 3rd-party API which renders Unicode sequences without braces.

For example, täna is delivered to clients as t\u00e4na.

When deserializing responses with serde_json, I predictably get:

error: incorrect unicode escape sequence

What is the most idiomatic way of escaping the input so that t\u00e4na becomes t\u{00e4}na and \u00dche becomes \u{00dc}he?

In JSON the syntax must be without {}, and serde_json correctly supports it that way.

Your error is coming from the Rust compiler. You've probably put the JSON in Rust's source code, in a Rust string without escaping the \ character.

It should be like this:

let s: String = serde_json::from_str("\"\\u00dche\"").unwrap();

otherwise the unicode sequence is parsed by Rust, not by serde_json.

2 Likes

You are 100% correct.

You have helped me see that I have been misinterpreting the error message. serde_json is failing for unrelated reasons.