test_json_validity.py 776 B

12345678910111213141516171819202122232425262728
  1. #!/usr/bin/env python
  2. #
  3. # Christian Gagneraud - 2012
  4. # Simple python script that will parse json dictionaries on its input,
  5. # If it fails, it will print the offending line and an error message.
  6. # The goal is to check that GPSD outputs valid JSON.
  7. #
  8. # This code runs compatibly under Python 2 and 3.x for x >= 2.
  9. # Preserve this property!
  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)