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

Implement simple JSON serializer #411

Merged
merged 5 commits into from
May 25, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion aws/sdk/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ plugins {
val smithyVersion: String by project

val sdkOutputDir = buildDir.resolve("aws-sdk")
val runtimeModules = listOf("smithy-types", "smithy-xml", "smithy-http", "smithy-http-tower", "protocol-test-helpers")
val runtimeModules = listOf("smithy-types", "smithy-json", "smithy-xml", "smithy-http", "smithy-http-tower", "protocol-test-helpers")
val awsModules = listOf("aws-auth", "aws-endpoint", "aws-types", "aws-hyper", "aws-sig-auth", "aws-http")

buildscript {
Expand Down
1 change: 1 addition & 0 deletions rust-runtime/inlineable/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ are to allow this crate to be compilable and testable in isolation, no client co
"http" = "0.2.1"
"smithy-types" = { version = "0.0.1", path = "../smithy-types" }
"smithy-http" = { version = "0.0.1", path = "../smithy-http" }
"smithy-json" = { path = "../smithy-json" }
"smithy-xml" = { path = "../smithy-xml" }
"fastrand" = "1"

Expand Down
15 changes: 15 additions & 0 deletions rust-runtime/smithy-json/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "smithy-json"
version = "0.1.0"
authors = ["AWS Rust SDK Team <[email protected]>", "John DiSanti <[email protected]>"]
edition = "2018"

[dependencies]
itoa = "0.4"
ryu = "1.0"
smithy-types = { path = "../smithy-types" }

[dev-dependencies]
proptest = "1"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
79 changes: 79 additions & 0 deletions rust-runtime/smithy-json/src/escape.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* 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 {
'"' => "\\\"",
'\\' => "\\\\",
rcoh marked this conversation as resolved.
Show resolved Hide resolved
'\u{08}' => "\\b",
'\u{0C}' => "\\f",
'\n' => "\\n",
'\r' => "\\r",
'\t' => "\\t",
_ => unreachable!(),
});
last = index + 1;
}
escaped.push_str(&value[last..end]);
rcoh marked this conversation as resolved.
Show resolved Hide resolved
Cow::Owned(escaped)
}

#[cfg(test)]
mod test {
use super::escape_string;
use serde::Serialize;

#[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());
rcoh marked this conversation as resolved.
Show resolved Hide resolved
}

#[derive(Serialize)]
struct TestStruct {
value: String,
}

use proptest::proptest;
proptest! {
rcoh marked this conversation as resolved.
Show resolved Hide resolved
#[test]
fn matches_serde_json(s: String) {
let mut formatted = String::new();
formatted.push_str(r#"{"value":""#);
formatted.push_str(&escape_string(&s));
formatted.push_str("\"}");

assert_eq!(
serde_json::to_string(&TestStruct { value: s }).unwrap(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need to wrap the value in a structure here, you can call to_string on a string directly: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=dcc0f9d691e95c859419cc46cc2cc4e1

this applies here & can also simplify the float tests as well

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL. Thanks, will simplify.

formatted
)
}
}
}
9 changes: 9 additions & 0 deletions rust-runtime/smithy-json/src/lib.rs
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;
Loading