Skip to content
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
48 changes: 38 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ default = ["toml", "json", "yaml", "ini", "ron", "json5", "convert-case", "async
json = ["serde_json"]
yaml = ["yaml-rust2"]
ini = ["rust-ini"]
json5 = ["json5_rs", "serde/derive"]
json5 = ["json5_rs", "dep:serde-untagged"]
convert-case = ["convert_case"]
preserve_order = ["indexmap", "toml?/preserve_order", "serde_json?/preserve_order", "ron?/indexmap"]
async = ["async-trait"]
Expand All @@ -145,6 +145,7 @@ indexmap = { version = "2.10.0", features = ["serde"], optional = true }
convert_case = { version = "0.6", optional = true }
pathdiff = "0.2"
winnow = "0.7.0"
serde-untagged = { version = "0.1.8", optional = true }

[dev-dependencies]
serde_derive = "1.0"
Expand Down
20 changes: 18 additions & 2 deletions src/file/format/json5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use crate::format;
use crate::map::Map;
use crate::value::{Value, ValueKind};

#[derive(serde::Deserialize, Debug)]
#[serde(untagged)]
#[derive(Debug)]
pub(crate) enum Val {
Null,
Boolean(bool),
Expand All @@ -16,6 +15,23 @@ pub(crate) enum Val {
Object(Map<String, Self>),
}

impl<'de> serde::de::Deserialize<'de> for Val {
fn deserialize<D>(d: D) -> Result<Self, D::Error>
where
D: serde::de::Deserializer<'de>,
{
serde_untagged::UntaggedEnumVisitor::new()
.bool(|value| Ok(Self::Boolean(value)))
.i64(|value| Ok(Self::Integer(value)))
.f64(|value| Ok(Self::Float(value)))
.string(|value| Ok(Val::String(value.to_owned())))
.none(|| Ok(Self::Null))
.seq(|value| value.deserialize().map(Val::Array))
.map(|value| value.deserialize().map(Val::Object))
Comment on lines +29 to +30
Copy link

Choose a reason for hiding this comment

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

Since this PR is partially motivated by performance, it is worth knowing about dtolnay/erased-serde#61 (which is used by serde-untagged). For very deeply nested config you would see O(n²) performance in the new implementation compared to O(n) in the previous one. Ultimately the best performance would be from a more verbose impl that resembles https://github.com/serde-rs/json/blob/v1.0.142/src/value/de.rs#L22, which completely eliminates the quadratic factor and also reduces linear factor from heap allocations inside erased-serde and constant factor from heap allocations inside UntaggedEnumVisitor.

.deserialize(d)
}
}

pub(crate) fn parse(
uri: Option<&String>,
text: &str,
Expand Down
2 changes: 0 additions & 2 deletions src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -676,8 +676,6 @@ impl ser::SerializeStructVariant for Unreachable {

#[cfg(test)]
mod test {
use serde::{Deserialize, Serialize};
#[cfg(not(feature = "json5"))]
use serde_derive::{Deserialize, Serialize};

use super::*;
Expand Down
Loading