Duper - 下一代 JSON 扩展配置文件格式
Duper是一个仍然在积极开发中的配置文件格式,主体由Rust编写,向后兼容JSON,但支持更丰富的语法和类型,同时使输入更加人性化。
语法预览
以下是一段示例的Duper配置:
Product({
product_id: Uuid("1dd7b7aa-515e-405f-85a9-8ac812242609"),
name: "Wireless Bluetooth Headphones",
brand: "AudioTech",
price: Decimal("129.99"),
dimensions: (18.5, 15.2, 7.8), // In centimeters
weight: Kilograms(0.285),
in_stock: true,
specifications: {
battery_life: Duration("30h"),
noise_cancellation: true,
connectivity: ["Bluetooth 5.0", "3.5mm Jack"],
},
image_thumbnail: Png(b64"iVBORw0KGgoAAAANSUhEUgAAAGQ="),
tags: ["electronics", "audio", "wireless"],
release_date: PlainDate('2023-11-15'),
/* Warranty is optional */
warranty_period: null,
customer_ratings: {
latest_review: r#"Absolutely ""astounding""!! 😎"#,
average: 4.5,
count: 127,
},
created_at: Instant('2023-11-17T21:50:43+00:00'),
})可以看到,和JSON相比,有以下差异:
简单键名不需要写双引号
值支持类型标记
更多的数据类型和字面量(如元组,b64等)
类型
比起普通的配置文件语言,它支持更多的类型。例如你可以通过以下代码声明一个bytestring:
{
png_signature: b"\x89PNG\r\n\x1a\n",
ascii: b"Hello, World!",
ansi_reset: b"\x1b[0m",
}他也提供时间支持,有多种描述绝对时间或时间差的类型,基于RFC 9557标准。
例如:
{
// Allowed
precise_identifier: PlainDateTime('2007-03-31T10:35:10'),
subset: PlainYearMonth('1994-11-06T19:45:27-03:00'), // PlainYearMonth is a subset of Instant
}Duper的元组和数组类似,只不过将中括号替换为了小括号。
{
empty_tuple: (),
another_empty_tuple: (,),
single_element: (1),
another_single_element: (1,),
tuple_of_arrays: ([true, 1.0], ["x", "y", "z"]),
array_of_tuples: [(1, null), (3, 4.0, 5)],
nested: (((), ("hi"))),
multiline_tuple: (
"Vec",
"Cow",
"Arc",
),
}IDE支持
目前可以使用 Duper - Visual Studio Marketplace VSCode插件,在VSCode中获取Duper的语法高亮。
多语言支持
Rust
原生支持Rust,支持以下crate:
serde:通过
serde_duper整合serde库,可以实现优雅的序列化/反序列化,并通过serde的宏实现功能修改(例如用serde的identifier将Vec<u8>序列化为bytes)。tracing:通过
tracing_duper库支持tracing,无痛实现结构化输出。axum:通过
axum_dupercrate,直接支持在axum的路由使用Duper类型。
Python
按照 duper-python 包获取python上的duper支持。这个包提供了像json包一样的序列化/反序列化支持。
支持Pydantic包,实现类型化验证和序列化。
支持FastAPI包,直接在路由函数使用Duper类型。
Javascript
支持Node.js和Vite,提供stringify等函数,实现序列化/反序列化。
.NET (Alpha)
对C# .NET的支持还在alpha阶段,详情见:.NET guide | Duper
其他工具
duperq
duperq是一个结构化日志过滤器。如果你使用duper格式来结构化输出日志,你可以简单的使用这个命令行工具提供变量过滤条件进行过滤。
详情见:duperq | Duper
duperfmt
duperfmt是一个用于duper的formatter,底层使用duper-lsp。详情见:duperfmt | Duper
Comments