toml4cj:TOML v1.0.0 解析库,语义清晰易读,支持多类型解码

一个TOML格式解析库

分支7Tags0

TOML4CJ

简介

TOML4CJ 致力于打造一种语义直观、阅读友好的极简配置文件格式。其设计理念是能够明确无误地转换为哈希表结构,并能轻松解析为各类编程语言的数据对象。 语法规范参考地址:https://toml.io/cn/

本项目是仓颉编程语言对 TOML4CJ v1.0.0 标准的实现库。

核心优势

  • 🚀 采用直观语义设计,阅读门槛低
  • 🚀 支持零歧义的哈希表转换
  • 💪 可轻松解析为多语言数据结构
  • 🛠️ 提供丰富的原生类型支持
  • 🌍 具备跨平台兼容性

发展路线

系统架构

架构示意图:

  • 通过 load 入口加载 toml 配置文件

  • 采用差异化解析器处理不同语法结构

  • 最终生成标准化的 DataModel 供仓颉语言调用

源码结构:

.
├── 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 测试及单元测试

接口说明

当前仅支持整数、浮点数、字符串和布尔类型字面量的解码功能。具体参见API文档

class Decoder

// 初始化 Decoder
public init()
public init(payload: String)

// 解码 toml 文件, pn 为文件路径
public func load(pn: String)

// 实现解码功能
public func decode()

Usage Instructions

Compilation

   cjpm build

Feature Examples

key = "value"
bare_key = "value"
bare-key = "value"
1234 = "value"
import toml4cj.decoders.*

main() {
    let decoder: Decoder = Decoder()
    decoder.load("integer002.toml")
    let a = decoder.decode()
    println(a)
}

The execution results are as follows:

{"key":"value","bare_key":"value","bare-key":"value","1234":"value"}

TOML4CJ 提供读取 TOML 文件数据的功能示例

使用本功能需准备 TOML 配置文件

import toml4cj.decoders.*
import std.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
}

The execution results are as follows:

return 1

TOML4CJ 提供解析 TOML 文件数据的功能示例

(备注:参考 TOML 语法规范地址:https://toml.io/cn/
用例执行需提供 TOML 文件

import toml4cj.decoders.*
import std.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
}

The execution results are as follows:

{"key":"value","bare_key":"value","bare-key":"value","1234":"value"}

Constraints and Limitations

Verified in the following versions:

Cangjie Version: 1.0.0

Open Source License

This project is based on the MIT License. Feel free to enjoy and contribute to open source.

Contribution

We welcome PRs, Issues, and any form of contribution from you.

项目介绍

一个TOML格式解析库

定制我的领域