JSON 指针中波浪号的用途
Purpose of tilde in JSON Pointer
JSON Pointer 规范指出:
the characters '~' (%x7E) and '/' (%x2F) have special meanings in
JSON Pointer
很清楚“/”的用途,但我看不到波浪号的用途(只提到它需要转义以及如何转义)。
在 JSON 指针中,您需要使用 ~1
来编码您希望将 /
作为路径中 属性 名称的一部分。因此,~
作为转义序列的指示符具有特殊含义,不再表示真正的波浪号。真正的波浪号表示为转义序列 ~0
.
换句话说(引自 JSON 指针规范):
Evaluation of each reference token begins by decoding any escaped
character sequence. This is performed by first transforming any
occurrence of the sequence '~1' to '/', and then transforming any
occurrence of the sequence '~0' to '~'. By performing the
substitutions in this order, an implementation avoids the error of
turning '~01' first into '~1' and then into '/', which would be
incorrect (the string '~01' correctly becomes '~1' after
transformation).
在此处查看 JSON 补丁测试可能会很有趣:https://github.com/json-patch/json-patch-tests/blob/master/spec_tests.json#L200(搜索 ~
)
JSON Pointer 规范指出:
the characters '~' (%x7E) and '/' (%x2F) have special meanings in JSON Pointer
很清楚“/”的用途,但我看不到波浪号的用途(只提到它需要转义以及如何转义)。
在 JSON 指针中,您需要使用 ~1
来编码您希望将 /
作为路径中 属性 名称的一部分。因此,~
作为转义序列的指示符具有特殊含义,不再表示真正的波浪号。真正的波浪号表示为转义序列 ~0
.
换句话说(引自 JSON 指针规范):
Evaluation of each reference token begins by decoding any escaped character sequence. This is performed by first transforming any occurrence of the sequence '~1' to '/', and then transforming any occurrence of the sequence '~0' to '~'. By performing the substitutions in this order, an implementation avoids the error of turning '~01' first into '~1' and then into '/', which would be incorrect (the string '~01' correctly becomes '~1' after transformation).
在此处查看 JSON 补丁测试可能会很有趣:https://github.com/json-patch/json-patch-tests/blob/master/spec_tests.json#L200(搜索 ~
)