Skip to content

perf: Allow writing json without serde #100

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

Merged
merged 2 commits into from
Jul 21, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
441 changes: 431 additions & 10 deletions Cargo.lock

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions crates/json-write/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Changelog

The format is based on [Keep a Changelog].

[Keep a Changelog]: http://keepachangelog.com/en/1.0.0/

<!-- next-header -->
## [Unreleased] - ReleaseDate

Initial release

<!-- next-url -->
[Unreleased]: https://github.com/toml-rs/toml/compare/06332279463f17447e1218c834078535b5ed9ebd...HEAD
42 changes: 42 additions & 0 deletions crates/json-write/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[package]
name = "json-write"
version = "0.0.1"
description = """
A low-level interface for writing out JSON
"""
categories = ["encoding"]
keywords = ["encoding", "json", "no_std"]
repository.workspace = true
license.workspace = true
edition.workspace = true
rust-version.workspace = true
include.workspace = true

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs", "--generate-link-to-definition"]

[package.metadata.release]
pre-release-replacements = [
{file="CHANGELOG.md", search="Unreleased", replace="{{version}}", min=1},
{file="CHANGELOG.md", search="\\.\\.\\.HEAD", replace="...{{tag_name}}", exactly=1},
{file="CHANGELOG.md", search="ReleaseDate", replace="{{date}}", min=1},
{file="CHANGELOG.md", search="<!-- next-header -->", replace="<!-- next-header -->\n## [Unreleased] - ReleaseDate\n", exactly=1},
{file="CHANGELOG.md", search="<!-- next-url -->", replace="<!-- next-url -->\n[Unreleased]: https://github.com/assert-rs/libtest2/compare/{{tag_name}}...HEAD", exactly=1},
]

[features]
default = ["std"]
std = ["alloc"]
alloc = []

[dependencies]

[dev-dependencies]
proptest = "1.6.0"
serde = { version = "1.0.160", features = ["derive"] }
serde_json = { version = "1.0.96" }
snapbox = "0.6.0"

[lints]
workspace = true
1 change: 1 addition & 0 deletions crates/json-write/LICENSE-APACHE
1 change: 1 addition & 0 deletions crates/json-write/LICENSE-MIT
22 changes: 22 additions & 0 deletions crates/json-write/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# toml_writer

[![Latest Version](https://img.shields.io/crates/v/toml.svg)](https://crates.io/crates/toml)
[![Documentation](https://docs.rs/toml/badge.svg)](https://docs.rs/toml)

A low-level interface for writing out TOML

## License

Licensed under either of

* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or <http://www.apache.org/licenses/LICENSE-2.0>)
* MIT license ([LICENSE-MIT](LICENSE-MIT) or <http://opensource.org/licenses/MIT>)

at your option.

### Contribution

Unless you explicitly state otherwise, any contribution intentionally
submitted for inclusion in the work by you, as defined in the Apache-2.0
license, shall be dual-licensed as above, without any additional terms or
conditions.
53 changes: 53 additions & 0 deletions crates/json-write/src/key.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#[cfg(feature = "alloc")]
use alloc::borrow::Cow;
#[cfg(feature = "alloc")]
use alloc::string::String;

use crate::JsonWrite;

#[cfg(feature = "alloc")]
pub trait ToJsonKey {
fn to_json_key(&self) -> String;
}

#[cfg(feature = "alloc")]
impl<T> ToJsonKey for T
where
T: WriteJsonKey + ?Sized,
{
fn to_json_key(&self) -> String {
let mut result = String::new();
let _ = self.write_json_key(&mut result);
result
}
}

pub trait WriteJsonKey {
fn write_json_key<W: JsonWrite + ?Sized>(&self, writer: &mut W) -> core::fmt::Result;
}

impl WriteJsonKey for str {
fn write_json_key<W: JsonWrite + ?Sized>(&self, writer: &mut W) -> core::fmt::Result {
crate::value::write_json_str(self, writer)
}
}

#[cfg(feature = "alloc")]
impl WriteJsonKey for String {
fn write_json_key<W: JsonWrite + ?Sized>(&self, writer: &mut W) -> core::fmt::Result {
self.as_str().write_json_key(writer)
}
}

#[cfg(feature = "alloc")]
impl WriteJsonKey for Cow<'_, str> {
fn write_json_key<W: JsonWrite + ?Sized>(&self, writer: &mut W) -> core::fmt::Result {
self.as_ref().write_json_key(writer)
}
}

impl<V: WriteJsonKey + ?Sized> WriteJsonKey for &V {
fn write_json_key<W: JsonWrite + ?Sized>(&self, writer: &mut W) -> core::fmt::Result {
(*self).write_json_key(writer)
}
}
56 changes: 56 additions & 0 deletions crates/json-write/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//! A low-level interface for writing out JSON
//!
//! # Example
//!
//! ```rust
//! use json_write::JsonWrite as _;
//!
//! # fn main() -> std::fmt::Result {
//! let mut output = String::new();
//! output.open_object()?;
//! output.newline()?;
//!
//! output.space()?;
//! output.space()?;
//! output.key("key")?;
//! output.keyval_sep()?;
//! output.space()?;
//! output.value("value")?;
//! output.newline()?;
//!
//! output.close_object()?;
//! output.newline()?;
//!
//! assert_eq!(output, r#"{
//! "key": "value"
//! }
//! "#);
//! # Ok(())
//! # }
//! ```

#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![warn(clippy::std_instead_of_core)]
#![warn(clippy::std_instead_of_alloc)]
#![warn(clippy::print_stderr)]
#![warn(clippy::print_stdout)]

#[cfg(feature = "alloc")]
extern crate alloc;

mod key;
mod value;
mod write;

#[cfg(feature = "alloc")]
pub use key::ToJsonKey;
pub use key::WriteJsonKey;
#[cfg(feature = "alloc")]
pub use value::ToJsonValue;
pub use value::WriteJsonValue;
pub use write::JsonWrite;

#[doc = include_str!("../README.md")]
#[cfg(doctest)]
pub struct ReadmeDoctests;
Loading
Loading