Skip to content

json5 fails to parse null values #689

@grindvoll

Description

@grindvoll

Using the json5 format, null values are not handled in JSON input. The json format handles this correctly.

The following code fails:

    const JSON: &str = r#"
    {
        "name": "string",
        "optional": null
    }
    "#;

    let file = config::File::from_str(JSON, config::FileFormat::Json5);
    let config = Config::builder().add_source(file).build().unwrap();
    // called `Result::unwrap()` on an `Err` value: invalid type: unit value, expected a boolean, integer, float, string, null, array or map

The error seems to be caused by a missing handler for unit in the Deserialize implementation for the json5 Val type:

// json5.rs, line 18:
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))
            .unit(|| Ok(Self::Null))  // Adding this allows parsing of null values !
            .seq(|value| value.deserialize().map(Val::Array))
            .map(|value| value.deserialize().map(Val::Object))
            .deserialize(d)
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions