Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for uri fragments #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions jsonpointer.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
from collections import Mapping, Sequence

from itertools import tee
from requests.utils import unquote
import re
import copy

Expand Down Expand Up @@ -178,10 +179,14 @@ def __init__(self, pointer):
invalid_escape.group()))

parts = pointer.split('/')
if parts.pop(0) != '':
raise JsonPointerException('Location must start with /')
first_part = parts.pop(0)
if first_part not in ('', '#'):
raise JsonPointerException('Location must start with / or #')

parts = [unescape(part) for part in parts]
if first_part == '#':
parts = [unquote(part) for part in parts]

self.parts = parts

def to_last(self, doc):
Expand Down
28 changes: 28 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,34 @@ def test_example(self):
self.assertEqual(resolve_pointer(doc, "/m~0n"), 8)


def test_example_uri_fragment(self):
doc = {
"foo": ["bar", "baz"],
"": 0,
"a/b": 1,
"c%d": 2,
"e^f": 3,
"g|h": 4,
"i\\j": 5,
"k\"l": 6,
" ": 7,
"m~n": 8
}

self.assertEqual(resolve_pointer(doc, "#"), doc)
self.assertEqual(resolve_pointer(doc, "#/foo"), ["bar", "baz"])
self.assertEqual(resolve_pointer(doc, "#/foo/0"), "bar")
self.assertEqual(resolve_pointer(doc, "#/"), 0)
self.assertEqual(resolve_pointer(doc, "#/a~1b"), 1)
self.assertEqual(resolve_pointer(doc, "#/c%25d"), 2)
self.assertEqual(resolve_pointer(doc, "#/e%5Ef"), 3)
self.assertEqual(resolve_pointer(doc, "#/g%7Ch"), 4)
self.assertEqual(resolve_pointer(doc, "#/i%5Cj"), 5)
self.assertEqual(resolve_pointer(doc, "#/k%22l"), 6)
self.assertEqual(resolve_pointer(doc, "#/%20"), 7)
self.assertEqual(resolve_pointer(doc, "#/m~0n"), 8)


def test_eol(self):
doc = {
"foo": ["bar", "baz"]
Expand Down