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

Support jsonc format #457

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ edition = "2018"
maintenance = { status = "actively-developed" }

[features]
default = ["toml", "json", "yaml", "ini", "ron", "json5", "convert-case", "async"]
default = ["toml", "json", "yaml", "ini", "ron", "json5", "jsonc", "convert-case", "async"]
json = ["serde_json"]
yaml = ["yaml-rust"]
ini = ["rust-ini"]
json5 = ["json5_rs", "serde/derive"]
jsonc = ["jsonc-parser"]
convert-case = ["convert_case"]
preserve_order = ["indexmap", "toml?/preserve_order", "serde_json?/preserve_order", "ron?/indexmap"]
async = ["async-trait"]
Expand All @@ -36,6 +37,7 @@ yaml-rust = { version = "0.4", optional = true }
rust-ini = { version = "0.20", optional = true }
ron = { version = "0.8", optional = true }
json5_rs = { version = "0.4", optional = true, package = "json5" }
jsonc-parser = { version = "0.23.0", optional = true }
indexmap = { version = "2.2", features = ["serde"], optional = true }
convert_case = { version = "0.6", optional = true }
pathdiff = "0.2"
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

- Set defaults
- Set explicit values (to programmatically override)
- Read from [JSON], [TOML], [YAML], [INI], [RON], [JSON5] files
- Read from [JSON], [TOML], [YAML], [INI], [RON], [JSON5], [JSONC] files
- Read from environment
- Loosely typed — Configuration values may be read in any supported type, as long as there exists a reasonable conversion
- Access nested fields using a formatted path — Uses a subset of JSONPath; currently supports the child ( `redis.port` ) and subscript operators ( `databases[0].name` )
Expand All @@ -22,6 +22,7 @@
[INI]: https://github.com/zonyitoo/rust-ini
[RON]: https://github.com/ron-rs/ron
[JSON5]: https://github.com/callum-oakley/json5-rs
[JSONC]: https://github.com/dprint/jsonc-parser

Please note this library

Expand All @@ -43,6 +44,7 @@ config = "0.14.0"
- `toml` - Adds support for reading TOML files
- `ron` - Adds support for reading RON files
- `json5` - Adds support for reading JSON5 files
- `jsonc` - Adds support for reading JSONC files

### Support for custom formats

Expand Down
61 changes: 61 additions & 0 deletions src/file/format/jsonc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use std::error::Error;

use crate::error::{ConfigError, Unexpected};
use crate::map::Map;
use crate::value::{Value, ValueKind};

use jsonc_parser::JsonValue;

pub fn parse(
uri: Option<&String>,
text: &str,
) -> Result<Map<String, Value>, Box<dyn Error + Send + Sync>> {
match jsonc_parser::parse_to_value(text, &Default::default())? {
Some(r) => match r {
JsonValue::String(ref value) => Err(Unexpected::Str(value.to_string())),
JsonValue::Number(value) => Err(Unexpected::Float(value.parse::<f64>().unwrap())),
JsonValue::Boolean(value) => Err(Unexpected::Bool(value)),
JsonValue::Object(o) => match from_jsonc_value(uri, JsonValue::Object(o)).kind {
ValueKind::Table(map) => Ok(map),
_ => unreachable!(),
},
JsonValue::Array(_) => Err(Unexpected::Seq),
JsonValue::Null => Err(Unexpected::Unit),
},
None => Err(Unexpected::Unit),
}
.map_err(|err| ConfigError::invalid_root(uri, err))
.map_err(|err| Box::new(err) as Box<dyn Error + Send + Sync>)
}

fn from_jsonc_value(uri: Option<&String>, value: JsonValue) -> Value {
let vk = match value {
JsonValue::Null => ValueKind::Nil,
JsonValue::String(v) => ValueKind::String(v.to_string()),
JsonValue::Number(number) => {
if let Ok(v) = number.parse::<i64>() {
ValueKind::I64(v)
} else if let Ok(v) = number.parse::<f64>() {
ValueKind::Float(v)
} else {
unreachable!();
}
}
JsonValue::Boolean(v) => ValueKind::Boolean(v),
JsonValue::Object(table) => {
let m = table
.into_iter()
.map(|(k, v)| (k, from_jsonc_value(uri, v)))
.collect();
ValueKind::Table(m)
}
JsonValue::Array(array) => {
let l = array
.into_iter()
.map(|v| from_jsonc_value(uri, v))
.collect();
ValueKind::Array(l)
}
};
Value::new(uri, vk)
}
17 changes: 17 additions & 0 deletions src/file/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ mod ron;
#[cfg(feature = "json5")]
mod json5;

#[cfg(feature = "jsonc")]
mod jsonc;

/// File formats provided by the library.
///
/// Although it is possible to define custom formats using [`Format`] trait it is recommended to use FileFormat if possible.
Expand Down Expand Up @@ -55,6 +58,10 @@ pub enum FileFormat {
/// JSON5 (parsed with json5)
#[cfg(feature = "json5")]
Json5,

/// JSONC (parsed with jsonc)
#[cfg(feature = "jsonc")]
Jsonc,
}

lazy_static! {
Expand All @@ -81,6 +88,12 @@ lazy_static! {
#[cfg(feature = "json5")]
formats.insert(FileFormat::Json5, vec!["json5"]);

#[cfg(all(feature = "jsonc", feature = "json"))]
formats.insert(FileFormat::Jsonc, vec!["jsonc"]);

#[cfg(all(feature = "jsonc", not(feature = "json")))]
formats.insert(FileFormat::Jsonc, vec!["jsonc", "json"]);
Comment on lines +91 to +95
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do I understand correctly that the jsonc parser also parses json even if that feature is disabled?

I think that's counter-intuitive and we should only parse jsonc with the jsonc parser.

Copy link
Contributor Author

@up9cloud up9cloud Sep 20, 2023

Choose a reason for hiding this comment

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

well, there are 2 reasons i added that.

  1. the file extension for jsonc is actually .json, not .jsonc, so the real question is that when i enable the feature jsonc it's about the format or the file extension?
  2. for the common usage, people would choose only one of them, not both. and I believe the jsonc format is more common for config files than the json format.


formats
};
}
Expand Down Expand Up @@ -117,13 +130,17 @@ impl FileFormat {
#[cfg(feature = "json5")]
FileFormat::Json5 => json5::parse(uri, text),

#[cfg(feature = "jsonc")]
FileFormat::Jsonc => jsonc::parse(uri, text),

#[cfg(all(
not(feature = "toml"),
not(feature = "json"),
not(feature = "yaml"),
not(feature = "ini"),
not(feature = "ron"),
not(feature = "json5"),
not(feature = "jsonc"),
))]
_ => unreachable!("No features are enabled, this library won't work without features"),
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//! - Environment variables
//! - String literals in well-known formats
//! - Another Config instance
//! - Files: TOML, JSON, YAML, INI, RON, JSON5 and custom ones defined with Format trait
//! - Files: TOML, JSON, YAML, INI, RON, JSON5, JSONC and custom ones defined with Format trait
//! - Manual, programmatic override (via a `.set` method on the Config instance)
//!
//! Additionally, Config supports:
Expand Down
4 changes: 4 additions & 0 deletions tests/Settings-enum-test.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
// foo
"bar": "bar is a lowercase param",
}
4 changes: 4 additions & 0 deletions tests/Settings-invalid.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"ok": true,
"error"
}
4 changes: 4 additions & 0 deletions tests/Settings.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ latitude = 10.3970522
favorite = false
reviews = 3866
rating = 4.5
[place.creator]
name = John Smith
username = jsmith
email = jsmith@localhost
23 changes: 23 additions & 0 deletions tests/Settings.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
// c
/* c */
"debug": true,
"debug_json": true,
"production": false,
"arr": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
"place": {
"name": "Torre di Pisa",
"longitude": 43.7224985,
"latitude": 10.3970522,
"favorite": false,
"reviews": 3866,
"rating": 4.5,
"creator": {
"name": "John Smith",
"username": "jsmith",
"email": "jsmith@localhost"
},
},
"FOO": "FOO should be overridden",
"bar": "I am bar",
}
51 changes: 34 additions & 17 deletions tests/file_ini.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,25 @@ use serde_derive::Deserialize;

use std::path::PathBuf;

use config::{Config, File, FileFormat};
use config::{Config, File, FileFormat, Map, Value};
use float_cmp::ApproxEqUlps;

#[derive(Debug, Deserialize, PartialEq)]
#[derive(Debug, Default, Deserialize, PartialEq)]
struct Place {
name: String,
longitude: f64,
latitude: f64,
favorite: bool,
telephone: Option<String>,
reviews: u64,
rating: Option<f32>,
creator: Map<String, Value>,
}

#[derive(Debug, Deserialize, PartialEq)]
#[derive(Debug, Default, Deserialize, PartialEq)]
struct Settings {
debug: f64,
production: Option<String>,
place: Place,
}

Expand All @@ -33,20 +37,33 @@ fn make() -> Config {
fn test_file() {
let c = make();
let s: Settings = c.try_deserialize().unwrap();
assert_eq!(
s,
Settings {
debug: 1.0,
place: Place {
name: String::from("Torre di Pisa"),
longitude: 43.722_498_5,
latitude: 10.397_052_2,
favorite: false,
reviews: 3866,
rating: Some(4.5),
},
}
);
assert!(s.debug.approx_eq_ulps(&1.0, 2));
assert_eq!(s.production, Some("false".to_string()));
assert_eq!(s.place.name, "Torre di Pisa");
assert!(s.place.longitude.approx_eq_ulps(&43.722_498_5, 2));
assert!(s.place.latitude.approx_eq_ulps(&10.397_052_2, 2));
assert!(!s.place.favorite);
assert_eq!(s.place.reviews, 3866);
assert_eq!(s.place.rating, Some(4.5));
assert_eq!(s.place.telephone, None);
if cfg!(feature = "preserve_order") {
assert_eq!(
s.place
.creator
.into_iter()
.collect::<Vec<(String, config::Value)>>(),
vec![
("name".to_string(), "John Smith".into()),
("username".into(), "jsmith".into()),
("email".into(), "jsmith@localhost".into()),
]
);
} else {
assert_eq!(
s.place.creator["name"].clone().into_string().unwrap(),
"John Smith".to_string()
);
}
}

#[test]
Expand Down
Loading
Loading