Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Purpose of tilde in JSON Pointer

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).

like image 798
Brett Zamir Avatar asked Jul 17 '15 19:07

Brett Zamir


People also ask

What is a JSON pointer?

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.

What is the difference between tilde (~) and ~ notation?

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.

What does tilde in npm version number mean?

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 .


1 Answers

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 ~)

like image 52
warpech Avatar answered Oct 12 '22 15:10

warpech