/Lib/json/tests/test_separators.py

http://unladen-swallow.googlecode.com/ · Python · 42 lines · 34 code · 8 blank · 0 comment · 0 complexity · 7af6070f89d68561f087684b5caeac9a MD5 · raw file

  1. import textwrap
  2. from unittest import TestCase
  3. import json
  4. class TestSeparators(TestCase):
  5. def test_separators(self):
  6. h = [['blorpie'], ['whoops'], [], 'd-shtaeou', 'd-nthiouh', 'i-vhbjkhnth',
  7. {'nifty': 87}, {'field': 'yes', 'morefield': False} ]
  8. expect = textwrap.dedent("""\
  9. [
  10. [
  11. "blorpie"
  12. ] ,
  13. [
  14. "whoops"
  15. ] ,
  16. [] ,
  17. "d-shtaeou" ,
  18. "d-nthiouh" ,
  19. "i-vhbjkhnth" ,
  20. {
  21. "nifty" : 87
  22. } ,
  23. {
  24. "field" : "yes" ,
  25. "morefield" : false
  26. }
  27. ]""")
  28. d1 = json.dumps(h)
  29. d2 = json.dumps(h, indent=2, sort_keys=True, separators=(' ,', ' : '))
  30. h1 = json.loads(d1)
  31. h2 = json.loads(d2)
  32. self.assertEquals(h1, h)
  33. self.assertEquals(h2, h)
  34. self.assertEquals(d2, expect)