Skip to content

Commit

Permalink
Merge pull request #65 from transifex/dumbjson_escaping
Browse files Browse the repository at this point in the history
Better handling of backslashes on JSON
  • Loading branch information
ollandos committed Dec 22, 2016
2 parents cac9092 + 5e02b03 commit bb00d39
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
16 changes: 16 additions & 0 deletions openformats/tests/util_tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,22 @@ def test_dict_within_list(self):
self._test_dfs('["a", {"b": "c"}]',
[("a", 2), ([("b", 8, "c", 13)], 6)])

# Escaping
def test_escape_quotes(self):
# Actual string looks like: 'hello \" world'
self._test_dfs('{"a": "hello \\" world"}',
[('a', 2, 'hello \\" world', 7)])

def test_double_backslashes(self):
# Actual string looks like: 'hello \\ world'
self._test_dfs('{"a": "hello \\\\ world"}',
[('a', 2, 'hello \\\\ world', 7)])

def test_double_backslashes_with_quotes(self):
# Actual string looks like: 'hello world\\'
self._test_dfs('{"a": "hello world\\\\"}',
[('a', 2, 'hello world\\\\', 7)])

# Utils
def _test_dfs(self, content, against):
dumb_json = DumbJson(content)
Expand Down
9 changes: 7 additions & 2 deletions openformats/utils/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,18 @@ def _iter_list(self):

def _find_next(self, symbols, start=0, require_whitespace=True):
symbols = {s for s in symbols}
after_backslash = False
for ptr in xrange(start, len(self.source)):
candidate = self.source[ptr]
if candidate == '\\':
after_backslash = not after_backslash
if candidate in symbols:
if (candidate == '"' and ptr > 0 and
self.source[ptr - 1] == '\\'):
if candidate == '"' and after_backslash:
after_backslash = False
continue
return candidate, ptr
if candidate != '\\':
after_backslash = False
if require_whitespace and not candidate.isspace():
newline_count = self.source.count('\n', 0, ptr)
raise ValueError(
Expand Down

0 comments on commit bb00d39

Please sign in to comment.