A slash separates pointer tokens, so a key that literally contains a slash needs an escape - and so does the escape character itself. Today you decode the two JSON Pointer escapes, getting their tricky order right.
Decode the tilde-1 and tilde-0 escapes within each reference token.
Because / separates tokens, a key that literally contains a slash cannot be written
raw. JSON Pointer escapes it as ~1. That makes ~ itself special, so a literal
tilde is written ~0. These are the only two escapes, and they are decoded inside
each reference token after the pointer is split on slashes.
The order matters and is a classic trap. Decoding must be a single left-to-right
pass, or equivalently “replace ~1 then ~0” - never the reverse. Consider ~01:
scanning left to right, ~0 decodes to ~, then the 1 is literal, giving ~1. If
you naively replaced ~0 with ~ and then ~1 with / in two passes, that ~1
would wrongly become /. A single pass that looks at the character after each ~
avoids the mistake entirely, and an ~ followed by anything but 0 or 1 is an
invalid escape.
// decode each reference token left to right:// on '~', look at the next char:// '1' -> '/' '0' -> '~'// anything else (or end) -> invalid escape (error)// any other char -> itself// single left-to-right pass gets "~01" -> "~1" right