test_json_validity.py 779 B

12345678910111213141516171819202122232425262728293031
  1. #!/usr/bin/env python3
  2. #
  3. # Christian Gagneraud - 2012
  4. # This code runs compatibly under Python 2 and 3.x for x >= 2.
  5. # Preserve this property!
  6. """Simple python script that will parse json dictionaries on its input.
  7. If it fails, it will print the offending line and an error message.
  8. The goal is to check that GPSD outputs valid JSON.
  9. """
  10. from __future__ import absolute_import, print_function, division
  11. import json
  12. import sys
  13. success = True
  14. lc = 0
  15. for line in sys.stdin.readlines():
  16. lc += 1
  17. try:
  18. # Load the json dictionary, it should raise an error if it is malformed
  19. item = json.loads(line)
  20. except ValueError as e:
  21. success = False
  22. print("%d: %s" % (lc, line.strip()))
  23. print("%d: %s" % (lc, e))
  24. exit(0 if success else 1)