JSON reading

This commit is contained in:
Laurenz 2022-09-25 17:35:56 +02:00
parent ddd3b6a82b
commit fffb55f79a
8 changed files with 85 additions and 1 deletions

1
Cargo.lock generated
View File

@ -1061,6 +1061,7 @@ dependencies = [
"rustybuzz",
"same-file",
"serde",
"serde_json",
"siphasher",
"subsetter",
"svg2pdf",

View File

@ -41,6 +41,7 @@ syntect = { version = "5", default-features = false, features = ["default-syntax
rex = { git = "https://github.com/laurmaedje/ReX" }
lipsum = { git = "https://github.com/reknih/lipsum" }
csv = "1"
serde_json = "1"
# PDF export
miniz_oxide = "0.5"

View File

@ -97,6 +97,7 @@ pub fn new() -> Scope {
std.def_fn("symbol", utility::symbol);
std.def_fn("lorem", utility::lorem);
std.def_fn("csv", utility::csv);
std.def_fn("json", utility::json);
// Predefined colors.
std.define("black", Color::BLACK);

View File

@ -41,3 +41,46 @@ fn format_csv_error(error: csv::Error) -> String {
_ => "failed to parse csv file".into(),
}
}
/// Read structured data from a JSON file.
pub fn json(vm: &mut Vm, args: &mut Args) -> SourceResult<Value> {
let Spanned { v: path, span } =
args.expect::<Spanned<EcoString>>("path to json file")?;
let path = vm.locate(&path).at(span)?;
let data = vm.world.file(&path).at(span)?;
let value: serde_json::Value =
serde_json::from_slice(&data).map_err(format_json_error).at(span)?;
Ok(convert_json(value))
}
/// Convert a JSON value to a Typst value.
fn convert_json(value: serde_json::Value) -> Value {
match value {
serde_json::Value::Null => Value::None,
serde_json::Value::Bool(v) => Value::Bool(v),
serde_json::Value::Number(v) => match v.as_i64() {
Some(int) => Value::Int(int),
None => Value::Float(v.as_f64().unwrap_or(f64::NAN)),
},
serde_json::Value::String(v) => Value::Str(v.into()),
serde_json::Value::Array(v) => {
Value::Array(v.into_iter().map(convert_json).collect())
}
serde_json::Value::Object(v) => Value::Dict(
v.into_iter()
.map(|(key, value)| (key.into(), convert_json(value)))
.collect(),
),
}
}
/// Format the user-facing JSON error message.
fn format_json_error(error: serde_json::Error) -> String {
assert!(error.is_syntax() || error.is_eof());
format!(
"failed to parse json file: syntax error in line {}",
error.line()
)
}

View File

Before

Width:  |  Height:  |  Size: 8.4 KiB

After

Width:  |  Height:  |  Size: 8.4 KiB

4
tests/res/bad.json Normal file
View File

@ -0,0 +1,4 @@
{
"valid": true,
"invalid": True
}

20
tests/res/zoo.json Normal file
View File

@ -0,0 +1,20 @@
[
{
"name": "Debby",
"species": "Rhinoceros",
"weight": 1900,
"length": 390
},
{
"name": "Fluffy",
"species": "Tiger",
"weight": 115,
"length": 310
},
{
"name": "Sleepy",
"species": "Dolphin",
"weight": 150,
"length": 180
}
]

View File

@ -1,6 +1,9 @@
// Test reading structured CSV data.
// Test reading structured data.
// Ref: false
---
// Test reading CSV data.
// Ref: true
#set page(width: auto)
#let data = csv("/res/zoo.csv")
#let cells = data(0).map(strong) + data.slice(1).flatten()
@ -13,3 +16,14 @@
---
// Error: 6-20 failed to parse csv file: found 3 instead of 2 fields in line 3
#csv("/res/bad.csv")
---
// Test reading JSON data.
#let data = json("/res/zoo.json")
#test(data.len(), 3)
#test(data(0).name, "Debby")
#test(data(2).weight, 150)
---
// Error: 7-22 failed to parse json file: syntax error in line 3
#json("/res/bad.json")