Skip to content

Commit ff6f992

Browse files
Merge pull request #257 from transifex/fix-unescaped-json-content
fix the creation of json with unscaped content
2 parents 9175e5a + 790dded commit ff6f992

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

openformats/formats/json.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -534,11 +534,11 @@ def _compile_recursively(self, current_part):
534534
self.transcriber.copy_until(value_position)
535535
if key == self.CONTEXT_KEY and translation:
536536
context = translation.context
537-
self._compile_value(context, value, value_position)
537+
self._compile_value(self.escape(context), value, value_position)
538538
context_added = True
539539
elif key == self.DEVELOPER_COMMENT_KEY and translation:
540540
developer_comment = translation.developer_comment
541-
self._compile_value(developer_comment, value, value_position)
541+
self._compile_value(self.escape(developer_comment), value, value_position)
542542
developer_comments_added = True
543543
elif key == self.CHARACTER_LIMIT_KEY and translation:
544544
character_limit = translation.character_limit
@@ -557,13 +557,13 @@ def _compile_recursively(self, current_part):
557557
extra_elements = []
558558
if not context_added and translation and translation.context:
559559
extra_elements.append(u"\"{}{}\"{}\"".format(
560-
"context", key_value_separator, translation.context))
560+
"context", key_value_separator, self.escape(translation.context)))
561561
if not character_limit_added and translation and translation.character_limit:
562562
extra_elements.append(u"\"{}{}{}".format(
563563
"character_limit", key_value_separator, translation.character_limit))
564564
if not developer_comments_added and translation and translation.developer_comment:
565565
extra_elements.append(u"\"{}{}\"{}\"".format(
566-
"developer_comment", key_value_separator, translation.developer_comment))
566+
"developer_comment", key_value_separator, self.escape(translation.developer_comment)))
567567
if extra_elements:
568568
self.transcriber.add("," + line_separator + ("," + line_separator).join(extra_elements))
569569

openformats/tests/formats/structuredkeyvaluejson/test_keyvaluejson.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,3 +616,53 @@ def test_unicode(self):
616616
compiled = self.handler.compile(template, with_updated_char_limit)
617617
self.assertEqual(compiled, expected_compilation)
618618

619+
def test_unescaped(self):
620+
source = u"""
621+
{
622+
"a": {
623+
"string": "testtest",
624+
"context": "context",
625+
"developer_comment": "comments"
626+
},
627+
"b": {
628+
"string": "testtest2"
629+
}
630+
}
631+
"""
632+
633+
expected_compilation = u"""
634+
{
635+
"a": {
636+
"string": "testtest",
637+
"context": "other \\" context",
638+
"developer_comment": "other \\" comment"
639+
},
640+
"b": {
641+
"string": "testtest2",
642+
"context": "other \\" context",
643+
"developer_comment": "other \\" comment"
644+
}
645+
}
646+
"""
647+
template, stringset = self.handler.parse(source)
648+
649+
unescaped = [
650+
OpenString(
651+
key=stringset[0].key,
652+
string_or_strings=stringset[0].string,
653+
context=u'other " context',
654+
developer_comment=u'other " comment',
655+
character_limit=stringset[0].character_limit,
656+
),
657+
OpenString(
658+
key=stringset[1].key,
659+
string_or_strings=stringset[1].string,
660+
context=u'other " context',
661+
developer_comment=u'other " comment',
662+
character_limit=stringset[1].character_limit,
663+
)
664+
]
665+
666+
compiled = self.handler.compile(template, unescaped)
667+
self.maxDiff = None
668+
self.assertEqual(compiled, expected_compilation)

0 commit comments

Comments
 (0)