commit | c2787e86c49bb9cae0a16283478beb24888e3e2b | [log] [tgz] |
---|---|---|
author | acanthite <acanthite1855@gmail.com> | Mon Mar 30 00:40:38 2020 +0300 |
committer | acanthite <acanthite1855@gmail.com> | Mon Mar 30 00:53:56 2020 +0300 |
tree | a9c6adbe0590463fad78949dea908a3a751c7dad | |
parent | f1f21da504deed422b30a3018ccd630e40677c44 [diff] |
Make serializer respect all JSON escape characters JSON defines several characters which must be escaped when serializing. Currently serializer handles correctly only three characters: \", \n and \\. This commit adds the rest of the special characters: \r, \t, \f, \b, \/ Also added tests to make sure the serializer and deserializer work properly with all of them.
A Rust JSON5 serializer and deserializer which speaks Serde.
Deserialize a JSON5 string with from_str
. Go the other way with to_string
. The serializer is very basic at the moment, it just produces plain old JSON. See the Serde documentation for details on implementing Serialize
and Deserialize
. (Usually it's just a case of sprinkling in some derives.)
The Serde data model is mostly supported, with the exception of bytes and borrowed strings.
Read some config into a struct.
use json5; use serde_derive::Deserialize; #[derive(Deserialize, Debug, PartialEq)] struct Config { message: String, n: i32, } fn main() { let config = " { // A traditional message. message: 'hello world', // A number for some reason. n: 42, } "; assert_eq!( json5::from_str(config), Ok(Config { message: "hello world".to_string(), n: 42, }), ); }