File: test_json_validity.py

package info (click to toggle)
gpsd 3.27-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 44,056 kB
  • sloc: ansic: 74,438; python: 16,521; sh: 890; cpp: 848; php: 225; makefile: 197; perl: 111; javascript: 26; xml: 11
file content (30 lines) | stat: -rwxr-xr-x 779 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/env python3
#
# Christian Gagneraud - 2012
# This code runs compatibly under Python 2 and 3.x for x >= 2.
# Preserve this property!

"""Simple python script that will parse json dictionaries on its input.

If it fails, it will print the offending line and an error message.
The goal is to check that GPSD outputs valid JSON.
"""

from __future__ import absolute_import, print_function, division

import json
import sys

success = True
lc = 0
for line in sys.stdin.readlines():
    lc += 1
    try:
        # Load the json dictionary, it should raise an error if it is malformed
        item = json.loads(line)
    except ValueError as e:
        success = False
        print("%d: %s" % (lc, line.strip()))
        print("%d: %s" % (lc, e))

exit(0 if success else 1)