简介
TOML4CJ 旨在成为一个语义明显且易于阅读的最小化配置文件格式。TOML4CJ 被设计成可以无歧义地映射为哈希表。TOML4CJ 应该能很容易地被解析成各种语言中的数据结构。 参考toml语法规约地址: toml.io/cn/
该项目是仓颉语言实现 TOML4CJ v1.0.0 的语言库。
特性
- 🚀 语义明显易于阅读
- 🚀 能无歧义地映射为哈希表
- 💪 易于解析成各种语言中的数据结构
- 🛠️ 具备实用的原生类型
- 🌍 受到广泛支持
路线

架构
架构图:

- 通过 load 入口载入 toml 文件
- 区分语法使用不同 parser 进行解析
- 最后生成 DataModel 方便仓颉读取
源码目录:
bash
.
├── README.md
├── doc
│ ├── assets
│ ├── cjcov
│ ├── design.md
│ └── feature_api.md
├── res
│ ├── example.toml
│ └── multiline_string.toml
├── src
│ └── decoders
│ ├── decoder.cj
│ ├── exception.cj
│ ├── symbols.cj
│ ├── toml_tz.cj
│ └── package.cj
└── test
├── HLT
├── LLT
└── UT
doc
是库的设计文档、提案、库的使用文档、LLT 覆盖率src
是库源码目录test
是测试用例所在目录,包括 HLT 用例、LLT 用例和 UT 用例
接口说明
目前只实现对整数、浮点数、字符串、布尔类型字面量的解码。详情见API
class Decoder
swift
// 初始化 Decoder
public init()
public init(payload: String)
// 解码 toml 文件, pn 为文件路径
public func load(pn: String)
// 实现解码功能
public func decode()
使用说明
编译
cjpm build
功能示例
ini
key = "value"
bare_key = "value"
bare-key = "value"
1234 = "value"
css
import toml4cj.decoders.*
main() {
let decoder: Decoder = Decoder()
decoder.load("integer002.toml")
let a = decoder.decode()
println(a)
}
运行结果如下:
json
{"key":"value","bare_key":"value","bare-key":"value","1234":"value"}
DD一下: 欢迎大家关注公众号<程序猿百晓生>,可以了解到以下知识点。
erlang
`欢迎大家关注公众号<程序猿百晓生>,可以了解到以下知识点。`
1.OpenHarmony开发基础
2.OpenHarmony北向开发环境搭建
3.鸿蒙南向开发环境的搭建
4.鸿蒙生态应用开发白皮书V2.0 & V3.0
5.鸿蒙开发面试真题(含参考答案)
6.TypeScript入门学习手册
7.OpenHarmony 经典面试题(含参考答案)
8.OpenHarmony设备开发入门【最新版】
9.沉浸式剖析OpenHarmony源代码
10.系统定制指南
11.【OpenHarmony】Uboot 驱动加载流程
12.OpenHarmony构建系统--GN与子系统、部件、模块详解
13.ohos开机init启动流程
14.鸿蒙版性能优化指南
.......
TOML4CJ 提供读取 toml 文件数据的功能示例
用例执行需要 toml 文件
kotlin
import toml4cj.decoders.*
import std.os.posix.*
main () {
var path2: String = "${getcwd()}/test.properties"
let decoder: Decoder = Decoder()
try {
decoder.load(path2)
} catch (e: IllegalArgumentException) {
if (e.toString() == "IllegalArgumentException: The format of the file is not supported.") {
return 0
}
return 1
}
let a = decoder.decode()
println(a)
return 1
}
运行结果如下:
kotlin
return 1
TOML4CJ 提供解析 toml 文件数据的功能示例
(备注: 参考toml语法规约地址: toml.io/cn/ ) 用例执行需要toml文件
javascript
import toml4cj.decoders.*
import std.os.posix.*
let a = ##"{"int1":"+99","int2":"42","int3":"0","int4":"-17"}"##
let b = ##"{"int5":"1_000","int6":"5_349_221","int7":"53_49_221","int8":"1_2_3_4_5"}"##
main() {
var path2: String = getcwd()
let decoder: Decoder = Decoder()
decoder.load("${path2}/integer001.toml")
var json = decoder.decode()
if (json.toString() != a) {
return 1
}
decoder.load("${path2}/integer002.toml")
json = decoder.decode()
if (json.toString() != b) {
return 2
}
return 0
}
运行结果如下:
json
{"key":"value","bare_key":"value","bare-key":"value","1234":"value"}
约束与限制
在下述版本验证通过:
yaml
Cangjie Version: 0.53.4