use crate::support::text::truncate_with_ellipsis;
use crate::{Error, Result};
use jsonc_parser::ParseOptions;
use serde_json::Value;
use simple_fs::SPath;
pub fn parse_jsonc_to_serde_value(content: &str) -> Result<Option<serde_json::Value>> {
static OPTIONS: ParseOptions = ParseOptions {
allow_comments: true,
allow_trailing_commas: true,
allow_loose_object_property_names: false,
allow_single_quoted_strings: false,
allow_hexadecimal_numbers: false,
allow_unary_plus_numbers: false,
allow_missing_commas: false,
};
let json_value = jsonc_parser::parse_to_serde_value(content, &OPTIONS).map_err(|err| {
let content = truncate_with_ellipsis(content, 300, "...");
Error::custom(format!("Fail to parse json.\nCause: {err}\nJson Content:\n{content}"))
})?;
Ok(json_value)
}
pub fn load_json_to_serde_value(file: &SPath) -> Result<Option<serde_json::Value>> {
let content = simple_fs::read_to_string(file)?;
let value = parse_jsonc_to_serde_value(&content)?;
Ok(value)
}
pub fn into_values<T: serde::Serialize>(vals: Vec<T>) -> Result<Vec<Value>> {
let inputs: Vec<Value> = vals
.into_iter()
.map(|v| serde_json::to_value(v).map_err(Error::custom))
.collect::<Result<Vec<_>>>()?;
Ok(inputs)
}