# vim: set fileencoding=utf-8 :
import os, json, sys
global tabstops;
global doc;
try:
    tabstops = int(os.environ.get('JSON_INDENT'));
except:
    tabstops = 0;

def stdin():
    return sys.stdin.read();

def parse():
    return convert(stdin());

def convert(data):
    return json.loads(data,'utf-8');

def stringify(document,indent=-1):
    stops=tabstops;
    if isinstance(indent, (int)) and indent > -1:
        stops=indent;
    try:
        if stops == 0:
            return json.dumps(document,separators=(',',':'))
        else:
            return json.dumps(document, indent=stops)
    except ValueError as e:
        sys.stderr.write(str(e));
        sys.exit(1);

def compact(document):
    return stringify(document,0);
