SOML 是什么?真的可以取代 YAML?

SOML(Sindre's Objectively Marvelous Language)是一种新的配置文件格式,设计目标是结合 YAML 的可读性和 JSON 的严格性,减少配置解析中的隐式类型转换和歧义。

# Application configuration
app-name: 'order-service'
version: '1.2.0'
debug: false

# Numeric types
http-port: 8080
max-connections: 1_000
request-timeout-seconds: 30.5
ratio: 0.85
max-value: infinity

# These are different types
string-number: '123'
integer-number: 123
float-number: 123.0

# Keys are always strings, even if they look like numbers or keywords
404: 'Not Found'
2024-01-01: 'New Year'
true: 'This is a string key'

# Date, instant and duration are native types
created-date: 2026-11-16
last-deployed: 2026-11-16T10:30:00+08:00
request-timeout: 30s
retry-interval: 1.5s
connection-ttl: 1h30m

# Nested objects
server: {
  host: '0.0.0.0'
  port: 8080
  tls: {
    enabled: true
    certificate: '/etc/service/server.crt'
    private-key: '/etc/service/server.key'
  }
}

# Arrays
allowed-origins: [
  'https://example.com'
  'https://admin.example.com'
]

retry-delays: [1s, 3s, 5s]

# Array of objects
database-pools: [
  {
    name: 'primary'
    host: 'db-primary.internal'
    port: 5432
    max-connections: 50
  }
  {
    name: 'replica'
    host: 'db-replica.internal'
    port: 5432
    max-connections: 20
  }
]

# Escaped strings
message: "Line one\nLine two"
unicode-example: "\u{4e2d}\u{6587}"

# Multi-line string
description: '''
  Order service configuration.
  Handles order creation, payment
  and asynchronous notifications.
  '''

# Multi-line comment
/*
  This section contains monitoring
  and logging configuration.
*/
monitoring: {
  enabled: true
  metrics-path: '/metrics'
  log-level: 'info'
}

与 YAML 相比,主要有五点区别:

  1. 类型更加严格。 YAML 会根据内容推断类型,例如 yes、no、日期、数字可能被自动转换。SOML 明确规定类型由语法决定,'123' 是字符串,123 是整数,不会猜测。

  2. 缩进不再决定结构。 YAML 依赖缩进表达层级,空格错误可能改变配置含义。SOML 使用 {}、[] 和换行表达结构,缩进仅用于排版,复制粘贴也不容易破坏结构。

  3. 数据类型更加明确。 SOML 将整数和浮点数分开,还原生支持日期、时间瞬间(Instant)和时间间隔(Duration),例如 timeout: 30s,不需要全部写成字符串。

  4. 解析规则更加严格。 重复键、非法数字、模糊类型等都会直接报错,不允许解析器自行猜测。例如 404 作为键始终是字符串,不会被转换成整数。

  5. 增加标准化序列化能力。 SOML 不仅定义了统一的格式化规则,还定义了规范化输出(Canonical Form),相同的数据在不同语言和工具中可以生成完全一致的字节内容,适合哈希、签名、缓存和比较。

总结: YAML 优势在于生态成熟、表达灵活,但其隐式类型推断和缩进规则容易产生歧义。SOML 则试图在保持人类可读性的同时,让配置文件具备更严格、确定、跨语言一致的解析行为。不过,目前 SOML 规范仍处于 Draft 阶段,生态成熟度远不能与 YAML 相比,距离大规模生产应用还有较长的路。

如果你有魔法,你可以看到一个评论框~