/python-cson

Python library for CSON (schema-compressed JSON)

Primary LanguagePythonMIT LicenseMIT

python-cson

Build Status

Gitter

Python library for CSON (schema-compressed JSON)

Authors

Source

##Install

easy_install python-cson

##Examples

#Commandline usage
python-cson -f <outfile> <infile>

#Using STDIN
cat <infile> | python-cson -f <outfile>

#Printing output to STDOUT
python-cson <infile>
#!/usr/bin/python
import cson, json, optparse, os

parser = optparse.OptionParser('Usage: %prog -i <inputfilename>')
parser.add_option('-i', '--inputfilename', dest='inputfilename', type='string')

(options, args) = parser.parse_args()

# Assign new names to constants passed in from CLI options.
inputfilename  = options.inputfilename

if inputfilename:
    print "Processing " + inputfilename
    outName = os.path.splitext(inputfilename)[0]
    outFile = outName + ".json"
    with open(inputfilename, 'r') as infile:
        cson_data = infile.readlines()

        # interpret the CSON string
        json_data = cson.csons2json(''.join(cson_data), 1)

        # write the JSON string out to
        print "Creating " + outFile
        with open(outFile, 'w') as outfile:
            outfile.write(json_data)

       # decode JSON text for verfication
       obj = json.loads(json_data)
else:
    print "Requires input filename file"