-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement simple JSON serializer (#411)
* Implement simple JSON serializer * CR feedback * Add debug assertion to `append_string_unchecked`
- Loading branch information
Showing
6 changed files
with
530 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "smithy-json" | ||
version = "0.1.0" | ||
authors = ["AWS Rust SDK Team <aws-sdk-rust@amazon.com>", "John DiSanti <jdisanti@amazon.com>"] | ||
edition = "2018" | ||
|
||
[dependencies] | ||
itoa = "0.4" | ||
ryu = "1.0" | ||
smithy-types = { path = "../smithy-types" } | ||
|
||
[dev-dependencies] | ||
proptest = "1" | ||
serde_json = "1.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0. | ||
*/ | ||
|
||
use std::borrow::Cow; | ||
|
||
const ESCAPES: &[char] = &['"', '\\', '\u{08}', '\u{0C}', '\n', '\r', '\t']; | ||
|
||
/// Escapes a string for embedding in a JSON string value. | ||
pub fn escape_string(value: &str) -> Cow<str> { | ||
if !value.contains(ESCAPES) { | ||
return Cow::Borrowed(value); | ||
} | ||
|
||
let mut escaped = String::new(); | ||
let (mut last, end) = (0, value.len()); | ||
for (index, chr) in value | ||
.char_indices() | ||
.filter(|(_index, chr)| ESCAPES.contains(chr)) | ||
{ | ||
escaped.push_str(&value[last..index]); | ||
escaped.push_str(match chr { | ||
'"' => "\\\"", | ||
'\\' => "\\\\", | ||
'\u{08}' => "\\b", | ||
'\u{0C}' => "\\f", | ||
'\n' => "\\n", | ||
'\r' => "\\r", | ||
'\t' => "\\t", | ||
_ => unreachable!(), | ||
}); | ||
last = index + 1; | ||
} | ||
escaped.push_str(&value[last..end]); | ||
Cow::Owned(escaped) | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::escape_string; | ||
|
||
#[test] | ||
fn escape() { | ||
assert_eq!("", escape_string("").as_ref()); | ||
assert_eq!("foo", escape_string("foo").as_ref()); | ||
assert_eq!("foo\\r\\n", escape_string("foo\r\n").as_ref()); | ||
assert_eq!("foo\\r\\nbar", escape_string("foo\r\nbar").as_ref()); | ||
assert_eq!(r#"foo\\bar"#, escape_string(r#"foo\bar"#).as_ref()); | ||
assert_eq!(r#"\\foobar"#, escape_string(r#"\foobar"#).as_ref()); | ||
assert_eq!( | ||
r#"\bf\fo\to\r\n"#, | ||
escape_string("\u{08}f\u{0C}o\to\r\n").as_ref() | ||
); | ||
assert_eq!("\\\"test\\\"", escape_string("\"test\"").as_ref()); | ||
} | ||
|
||
use proptest::proptest; | ||
proptest! { | ||
#[test] | ||
fn matches_serde_json(s: String) { | ||
assert_eq!( | ||
serde_json::to_string(&s).unwrap(), | ||
format!(r#""{}""#, escape_string(&s)) | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0. | ||
*/ | ||
|
||
//! JSON Abstractions for Smithy | ||
|
||
mod escape; | ||
pub mod serialize; |
Oops, something went wrong.