The JSON Pointer spec states:
the characters '~' (%x7E) and '/' (%x2F) have special meanings in JSON Pointer
It is clear what '/' is used for, but I see no indication of what purpose the tilde serves (only mention that it needs to be escaped and how).
Although it uses JavaScript syntax, it's language-independent, since the resultant is plain text. JSON Pointer ( RFC 6901) is a feature from JSON Processing 1.1 API ( JSR 374 ). It defines a String that can be used for accessing values on a JSON document. It can be related to what XPath does for an XML document.
Tilde (~) notation: It is used to match the most recent patch version. Tilde ~ notation freezes the major version and minor version. As we know patch updates are bug fixes that’s why we can say ~ notation allows us to automatically accept bug fixes. Example: The ~1.2.0 will update all the future patch updates.
If the version number is prefixed with a tilde (~), it will only update the patch version in the future, without updating the major and minor versions when we run a npm update command. ~2.13.4 means npm will only update the releases from 2.13.4 to <2.14.0 .
In JSON Pointer, you need to use ~1
to encode that you want to have /
as part of the property name in the path. Because of that, ~
gets the special meaning as an indicator of the escape sequence an no longer expresses a real tilde. The real tilde is expressed as an escape sequence ~0
.
In other words (quote from JSON Pointer spec):
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).
It might be interesting to take a look at JSON Patch tests here: https://github.com/json-patch/json-patch-tests/blob/master/spec_tests.json#L200 (search for ~
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With