Jacky's blog
首页
  • 学习笔记

    • web
    • android
    • iOS
    • vue
  • 分类
  • 标签
  • 归档
收藏
  • tool
  • algo
  • python
  • java
  • server
  • growth
  • frida
  • blog
  • SP
  • more
GitHub (opens new window)

Jack Yang

编程; 随笔
首页
  • 学习笔记

    • web
    • android
    • iOS
    • vue
  • 分类
  • 标签
  • 归档
收藏
  • tool
  • algo
  • python
  • java
  • server
  • growth
  • frida
  • blog
  • SP
  • more
GitHub (opens new window)
  • shell

  • tool

  • 网络

  • algo

  • compute_base

  • blog

  • growth

  • java

  • C&C++

  • ai

  • secure

  • cms

  • english

  • 生活

  • 金融学

  • more

    • backend base
    • toml/json/yaml/ini
      • toml
        • 定义 list
      • yaml
      • ini
      • faq
        • toml vs ini
      • link
    • 正则表达式(Regex)完整指南
    • media base
    • 工作效率
    • 设计模式和思想
    • AST语法抽象树介绍
    • compress/decompress
    • 灰度发布与ABTest
    • sublime
    • vercel
    • ruby
    • rss
    • animation
    • 加解密技术
    • encode
    • 世界时间
    • 读kk大神聊房价
    • 效率秘籍
    • 沟通的艺术
  • other
  • more
Jacky
2024-05-30
目录

toml/json/yaml/ini

介绍配置文件的格式。 常见的有 json、yml、toml、ini 等配置

# toml

Tom's Obvious Minimal Language (opens new window). A config file format for humans. 支持更多的数据类型,包括字符串、整数、浮点数、布尔值、日期时间等

# 服务器配置
[server]
name = "example-server"
ip = "192.168.1.1"
enabled = true
port = 8080

# 数据库配置
[database]
server = "localhost"
port = 3306
username = "user"
password = "password"
timeout = 30.5
max_connections = 100

# 日志配置
[log]
level = "debug"
file = "/var/log/app.log"

# 嵌套表格(nested tables)
[owner]
name = "Tom Preston-Werner"
dob = 1979-05-27T07:32:00Z  # 日期时间类型

[database.replication]
enabled = true
mode = "async"
servers = ["192.168.1.2", "192.168.1.3"]

# array
[[album.tracks]]
title = "Overture of Efficiency"
duration = "4:30"

[[album.tracks]]
title = "Ballad of Simplicity"
duration = "5:20"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

# 定义 list

  1. 简单值的列表

如果你想定义一个包含简单值(如字符串、数字、布尔值等)的列表,可以像下面这样定义:

names = ["Alice", "Bob", "Charlie"]
ages = [25, 30, 35]
is_active = [true, false, true]
1
2
3
  1. 对象(表)的列表

TOML 支持定义一组对象(也称为表)的列表,每个对象可以拥有自己的字段

方法一: 使用表数组 ([[array_name]]), link (opens new window)

表数组是 TOML 的一种语法,用于定义包含对象的列表。在这种语法中,每个对象用 [[array_name]] 来定义

[[users]]
name = "Alice"
age = 25

[[users]]
name = "Bob"
age = 30

[[users]]
name = "Charlie"
age = 35
1
2
3
4
5
6
7
8
9
10
11

这表示 users 是一个表数组,包含 3 个对象,每个对象都有 name 和 age 字段

方法二: 对象列表的简写(嵌套对象的数组)

如果你需要将对象的定义放在一个数组中,可以直接嵌套对象作为数组的元素。这种方式更简洁,但适合数据量较少且结构较简单的情况

users = [
  { name = "Alice", age = 25 },
  { name = "Bob", age = 30 },
  { name = "Charlie", age = 35 }
]
1
2
3
4
5

这与方法一的功能相同,但在 TOML 中更加紧凑

  1. 嵌套列表

TOML 还允许定义嵌套的列表,例如一个列表中的每个元素又是一个列表:

matrix = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
]
1
2
3
4
5

总结

  • 简单值列表使用方括号 []
  • 对象列表可以使用 [[array_name]] 或嵌套对象数组的形式 {}

根据你的数据结构选择最合适的方式来定义 TOML 列表对象

# yaml

link (opens new window)

# ini

# faq

# toml vs ini

TOML(Tom's Obvious, Minimal Language)和 INI 文件确实有一些相似之处,但它们也有一些显著的不同点。让我们看看它们的相似和不同之处

相似之处:
  • 节的定义: 两者都使用方括号 [] 来定义节
  • 键值对: 两者都使用键值对的形式来配置参数
不同之处:
  • 数据类型:TOML 支持更多的数据类型,包括字符串、整数、浮点数、布尔值、日期时间等。而 INI 文件的值通常被视为字符串
  • 数组和表: TOML 支持数组和嵌套表(表是键值对的集合),而 INI 文件通常不直接支持这些复杂的数据结构
  • 语法细节: TOML 在语法上更严格,支持更复杂的嵌套结构和数据类型,而 INI 更为简单和宽松

以下是您的示例配置文件的 TOML 和 INI 格式的比较:

TOML 配置文件
[database]
server = "localhost"
port = 3306
username = "user"
password = "password"
1
2
3
4
5
INI 配置文件
[database]
server = localhost
port = 3306
username = user
password = password
1
2
3
4
5

# link

  • XML, JSON, TOML, YAML: Which One is the Secret Key to Your Data Success? (opens new window)
上次更新: 2024/12/01, 17:09:08
backend base
正则表达式(Regex)完整指南

← backend base 正则表达式(Regex)完整指南→

最近更新
01
npx 使用指南
10-12
02
cursor
09-28
03
inspect
07-20
更多文章>
Theme by Vdoing | Copyright © 2019-2025 Jacky | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式