@@ -143,8 +143,7 @@ def __init__(self, pointer):
143143 raise JsonPointerException ('location must starts with /' )
144144
145145 parts = map (unquote , parts )
146- parts = [part .replace ('~1' , '/' ) for part in parts ]
147- parts = [part .replace ('~0' , '~' ) for part in parts ]
146+ parts = [unescape (part ) for part in parts ]
148147 self .parts = parts
149148
150149
@@ -262,8 +261,7 @@ def path(self):
262261
263262 >>> ptr = JsonPointer('/~0/0/~1').path == '/~0/0/~1'
264263 """
265- parts = [part .replace ('~' , '~0' ) for part in self .parts ]
266- parts = [part .replace ('/' , '~1' ) for part in parts ]
264+ parts = [escape (part ) for part in self .parts ]
267265 return '' .join ('/' + part for part in parts )
268266
269267 def __eq__ (self , other ):
@@ -289,9 +287,7 @@ def from_parts(cls, parts):
289287 >>> JsonPointer.from_parts(['a', '~', '/', 0]).path == '/a/~0/~1/0'
290288 True
291289 """
292- parts = [str (part ) for part in parts ]
293- parts = [part .replace ('~' , '~0' ) for part in parts ]
294- parts = [part .replace ('/' , '~1' ) for part in parts ]
290+ parts = [escape (str (part )) for part in parts ]
295291 ptr = cls ('' .join ('/' + part for part in parts ))
296292 return ptr
297293
@@ -313,3 +309,10 @@ def pairwise(iterable):
313309 for _ in b :
314310 break
315311 return izip (a , b )
312+
313+
314+ def escape (s ):
315+ return s .replace ('~' , '~0' ).replace ('/' , '~1' )
316+
317+ def unescape (s ):
318+ return s .replace ('~1' , '/' ).replace ('~0' , '~' )
0 commit comments