Skip to content

Commit a382b95

Browse files
authored
perf: Allow writing json without serde (#100)
2 parents 2433ec2 + 278d8a3 commit a382b95

File tree

15 files changed

+1669
-12
lines changed

15 files changed

+1669
-12
lines changed

Cargo.lock

Lines changed: 431 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/json-write/CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Changelog
2+
3+
The format is based on [Keep a Changelog].
4+
5+
[Keep a Changelog]: http://keepachangelog.com/en/1.0.0/
6+
7+
<!-- next-header -->
8+
## [Unreleased] - ReleaseDate
9+
10+
Initial release
11+
12+
<!-- next-url -->
13+
[Unreleased]: https://github.com/toml-rs/toml/compare/06332279463f17447e1218c834078535b5ed9ebd...HEAD

crates/json-write/Cargo.toml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
[package]
2+
name = "json-write"
3+
version = "0.0.1"
4+
description = """
5+
A low-level interface for writing out JSON
6+
"""
7+
categories = ["encoding"]
8+
keywords = ["encoding", "json", "no_std"]
9+
repository.workspace = true
10+
license.workspace = true
11+
edition.workspace = true
12+
rust-version.workspace = true
13+
include.workspace = true
14+
15+
[package.metadata.docs.rs]
16+
all-features = true
17+
rustdoc-args = ["--cfg", "docsrs", "--generate-link-to-definition"]
18+
19+
[package.metadata.release]
20+
pre-release-replacements = [
21+
{file="CHANGELOG.md", search="Unreleased", replace="{{version}}", min=1},
22+
{file="CHANGELOG.md", search="\\.\\.\\.HEAD", replace="...{{tag_name}}", exactly=1},
23+
{file="CHANGELOG.md", search="ReleaseDate", replace="{{date}}", min=1},
24+
{file="CHANGELOG.md", search="<!-- next-header -->", replace="<!-- next-header -->\n## [Unreleased] - ReleaseDate\n", exactly=1},
25+
{file="CHANGELOG.md", search="<!-- next-url -->", replace="<!-- next-url -->\n[Unreleased]: https://github.com/assert-rs/libtest2/compare/{{tag_name}}...HEAD", exactly=1},
26+
]
27+
28+
[features]
29+
default = ["std"]
30+
std = ["alloc"]
31+
alloc = []
32+
33+
[dependencies]
34+
35+
[dev-dependencies]
36+
proptest = "1.6.0"
37+
serde = { version = "1.0.160", features = ["derive"] }
38+
serde_json = { version = "1.0.96" }
39+
snapbox = "0.6.0"
40+
41+
[lints]
42+
workspace = true

crates/json-write/LICENSE-APACHE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../LICENSE-APACHE

crates/json-write/LICENSE-MIT

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../LICENSE-MIT

crates/json-write/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# toml_writer
2+
3+
[![Latest Version](https://img.shields.io/crates/v/toml.svg)](https://crates.io/crates/toml)
4+
[![Documentation](https://docs.rs/toml/badge.svg)](https://docs.rs/toml)
5+
6+
A low-level interface for writing out TOML
7+
8+
## License
9+
10+
Licensed under either of
11+
12+
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or <http://www.apache.org/licenses/LICENSE-2.0>)
13+
* MIT license ([LICENSE-MIT](LICENSE-MIT) or <http://opensource.org/licenses/MIT>)
14+
15+
at your option.
16+
17+
### Contribution
18+
19+
Unless you explicitly state otherwise, any contribution intentionally
20+
submitted for inclusion in the work by you, as defined in the Apache-2.0
21+
license, shall be dual-licensed as above, without any additional terms or
22+
conditions.

crates/json-write/src/key.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#[cfg(feature = "alloc")]
2+
use alloc::borrow::Cow;
3+
#[cfg(feature = "alloc")]
4+
use alloc::string::String;
5+
6+
use crate::JsonWrite;
7+
8+
#[cfg(feature = "alloc")]
9+
pub trait ToJsonKey {
10+
fn to_json_key(&self) -> String;
11+
}
12+
13+
#[cfg(feature = "alloc")]
14+
impl<T> ToJsonKey for T
15+
where
16+
T: WriteJsonKey + ?Sized,
17+
{
18+
fn to_json_key(&self) -> String {
19+
let mut result = String::new();
20+
let _ = self.write_json_key(&mut result);
21+
result
22+
}
23+
}
24+
25+
pub trait WriteJsonKey {
26+
fn write_json_key<W: JsonWrite + ?Sized>(&self, writer: &mut W) -> core::fmt::Result;
27+
}
28+
29+
impl WriteJsonKey for str {
30+
fn write_json_key<W: JsonWrite + ?Sized>(&self, writer: &mut W) -> core::fmt::Result {
31+
crate::value::write_json_str(self, writer)
32+
}
33+
}
34+
35+
#[cfg(feature = "alloc")]
36+
impl WriteJsonKey for String {
37+
fn write_json_key<W: JsonWrite + ?Sized>(&self, writer: &mut W) -> core::fmt::Result {
38+
self.as_str().write_json_key(writer)
39+
}
40+
}
41+
42+
#[cfg(feature = "alloc")]
43+
impl WriteJsonKey for Cow<'_, str> {
44+
fn write_json_key<W: JsonWrite + ?Sized>(&self, writer: &mut W) -> core::fmt::Result {
45+
self.as_ref().write_json_key(writer)
46+
}
47+
}
48+
49+
impl<V: WriteJsonKey + ?Sized> WriteJsonKey for &V {
50+
fn write_json_key<W: JsonWrite + ?Sized>(&self, writer: &mut W) -> core::fmt::Result {
51+
(*self).write_json_key(writer)
52+
}
53+
}

crates/json-write/src/lib.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//! A low-level interface for writing out JSON
2+
//!
3+
//! # Example
4+
//!
5+
//! ```rust
6+
//! use json_write::JsonWrite as _;
7+
//!
8+
//! # fn main() -> std::fmt::Result {
9+
//! let mut output = String::new();
10+
//! output.open_object()?;
11+
//! output.newline()?;
12+
//!
13+
//! output.space()?;
14+
//! output.space()?;
15+
//! output.key("key")?;
16+
//! output.keyval_sep()?;
17+
//! output.space()?;
18+
//! output.value("value")?;
19+
//! output.newline()?;
20+
//!
21+
//! output.close_object()?;
22+
//! output.newline()?;
23+
//!
24+
//! assert_eq!(output, r#"{
25+
//! "key": "value"
26+
//! }
27+
//! "#);
28+
//! # Ok(())
29+
//! # }
30+
//! ```
31+
32+
#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
33+
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
34+
#![warn(clippy::std_instead_of_core)]
35+
#![warn(clippy::std_instead_of_alloc)]
36+
#![warn(clippy::print_stderr)]
37+
#![warn(clippy::print_stdout)]
38+
39+
#[cfg(feature = "alloc")]
40+
extern crate alloc;
41+
42+
mod key;
43+
mod value;
44+
mod write;
45+
46+
#[cfg(feature = "alloc")]
47+
pub use key::ToJsonKey;
48+
pub use key::WriteJsonKey;
49+
#[cfg(feature = "alloc")]
50+
pub use value::ToJsonValue;
51+
pub use value::WriteJsonValue;
52+
pub use write::JsonWrite;
53+
54+
#[doc = include_str!("../README.md")]
55+
#[cfg(doctest)]
56+
pub struct ReadmeDoctests;

0 commit comments

Comments
 (0)