2.7. Normalized Paths (规范化路径)
2.7. Normalized Paths (规范化路径)
Normalized Path 是值中节点位置的唯一表示, 在该值中唯一标识该节点. 具体而言, Normalized Path 是语法受限的 JSONPath 查询 (定义见下文), 例如 $['book'][3], 作用于该值时产生仅含 Normalized Path 所标识节点的 nodelist. 注意: Normalized Path 表示节点在_特定值_中的身份. 值中任一特定节点恰好由一个 Normalized Path 标识.
Nodelist 可紧凑地表示为 JSON string 数组, 各 string 为 Normalized Path.
Normalized Path 提供可预测格式, 简化 nodelist 的测试与后处理, 例如去除重复节点. 本文档示例中以 Normalized Path 作为 result path.
Normalized Path 使用 canonical 方括号记法, 而非点记法.
Normalized Path 中用单引号界定 string 成员名. 当 Normalized Path 出现在双引号界定的 string 内 (例如 JSON text 中) 时, 可减少需转义的字符数.
Normalized Path 中某些字符以一种且仅一种方式转义; 其余字符不转义.
Note: Normalized Path 是 singular query, 但并非所有 singular
query 都是 Normalized Path. 例如 $[-3] 是 singular query 但不是
Normalized Path. 与 $[-3] 等价的 Normalized Path 的 index 应等于
数组长度减 3. (若 $[-3] 要标识节点, 数组长度至少为 3.)
normalized-path = root-identifier *(normal-index-segment)
normal-index-segment = "[" normal-selector "]"
normal-selector = normal-name-selector / normal-index-selector
normal-name-selector = %x27 *normal-single-quoted %x27 ; 'string'
normal-single-quoted = normal-unescaped /
ESC normal-escapable
normal-unescaped = ; omit %x0-1F control codes
%x20-26 /
; omit 0x27 '
%x28-5B /
; omit 0x5C \
%x5D-D7FF /
; skip surrogate code points
%xE000-10FFFF
normal-escapable = %x62 / ; b BS backspace U+0008
%x66 / ; f FF form feed U+000C
%x6E / ; n LF line feed U+000A
%x72 / ; r CR carriage return U+000D
%x74 / ; t HT horizontal tab U+0009
"'" / ; ' apostrophe U+0027
"\" / ; \ backslash (reverse solidus) U+005C
(%x75 normal-hexchar)
; certain values u00xx U+00XX
normal-hexchar = "0" "0"
(
("0" %x30-37) / ; "00"-"07"
; omit U+0008-U+000A BS HT LF
("0" %x62) / ; "0b"
; omit U+000C-U+000D FF CR
("0" %x65-66) / ; "0e"-"0f"
("1" normal-HEXDIG)
)
normal-HEXDIG = DIGIT / %x61-66 ; "0"-"9", "a"-"f"
normal-index-selector = "0" / (DIGIT1 *DIGIT)
; non-negative decimal integer
因给定节点只能有一个 Normalized Path 标识, 语法规定哪些字符转义, 哪些不转义. 因此 normal-hexchar 的定义用于对无标准 JSON 转义 (如 \n) 的字符进行十六进制转义, 例如 U+000B LINE TABULATION.
2.7.1. Examples (示例)
| Path | Normalized Path | Comment |
|---|---|---|
| $.a | $['a'] | Object 值 |
| $[1] | $[1] | Array index |
| $[-3] | $[2] | 长度 5 的 array |
| 的负 index | ||
| $.a.b[1:2] | $['a']['b'][1] | 嵌套结构 |
| $["\u000B"] | $['\u000b'] | Unicode escape |
| $["\u0061"] | $['a'] | Unicode 字符 |
表 18: Normalized Path 示例