-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f6e4e91
Showing
4 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*.pyc | ||
dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Copyright (c) 2009 New England Biolabs <davisp@neb.com> | ||
|
||
Permission is hereby granted, free of charge, to any person | ||
obtaining a copy of this software and associated documentation | ||
files (the "Software"), to deal in the Software without | ||
restriction, including without limitation the rights to use, | ||
copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the | ||
Software is furnished to do so, subject to the following | ||
conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
r"""Canonical JSON serialization. | ||
Basic approaches for implementing canonical JSON serialization. | ||
Encoding basic Python object hierarchies:: | ||
>>> import jsonical | ||
>>> jsonical.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}]) | ||
'["foo",{"bar":["baz",null,1.0,2]}]' | ||
>>> print jsonical.dumps("\"foo\bar") | ||
"\"foo\bar" | ||
>>> print jsonical.dumps(u'\u1234') | ||
"\u1234" | ||
>>> print jsonical.dumps('\\') | ||
"\\" | ||
>>> print jsonical.dumps({"c": 0, "b": 0, "a": 0}) | ||
{"a":0,"b":0,"c":0} | ||
>>> from StringIO import StringIO | ||
>>> io = StringIO() | ||
>>> json.dump(['streaming API'], io) | ||
>>> io.getvalue() | ||
'["streaming API"]' | ||
Decoding JSON:: | ||
>>> import jsonical | ||
>>> jsonical.loads('["foo", {"bar":["baz", null, 1.0, 2]}]') | ||
[u'foo', {u'bar': [u'baz', None, Decimal('1.0'), 2]}] | ||
>>> jsonical.loads('"\\"foo\\bar"') | ||
u'"foo\x08ar' | ||
>>> from StringIO import StringIO | ||
>>> io = StringIO('["streaming API"]') | ||
>>> jsonical.load(io) | ||
[u'streaming API'] | ||
Using jsonical from the shell to canonicalize: | ||
$ echo '{"json":"obj","bar":2.333333}' | python -mjsonical | ||
{"bar":2.333333,"json":"obj"} | ||
$ echo '{1.2:3.4}' | python -mjson.tool | ||
Expecting property name: line 1 column 2 (char 2) | ||
""" | ||
import datetime | ||
import decimal | ||
import sys | ||
import types | ||
import unicodedata | ||
|
||
try: | ||
import json | ||
except ImportError: | ||
import simplejson as json | ||
|
||
class Encoder(json.JSONEncoder): | ||
def __init__(self, *args, **kwargs): | ||
kwargs.pop("sort_keys", None) | ||
super(Encoder, self).__init__(sort_keys=True, *args, **kwargs) | ||
|
||
def default(self, obj): | ||
"""This is slightly different than json.JSONEncoder.default(obj) | ||
in that it should returned the serialized representation of the | ||
passed object, not a serializable representation. | ||
""" | ||
if isinstance(obj, (datetime.date, datetime.time, datetime.datetime)): | ||
return obj.isoformat() | ||
elif isinstance(obj, decimal.Decimal): | ||
return str(obj) | ||
elif isinstance(obj, unicode): | ||
return unicodedata.normalize('NFD', obj) | ||
return super(Encoder, self).default(obj) | ||
|
||
def _iterencode_default(self, o, markers=None): | ||
yield self.default(o) | ||
|
||
def dump(obj, fp, indent=None): | ||
return json.dump(obj, fp, separators=(',', ':'), indent=indent, cls=Encoder) | ||
|
||
def dumps(obj, indent=None): | ||
return json.dumps(obj, separators=(',', ':'), indent=indent, cls=Encoder) | ||
|
||
class Decoder(json.JSONDecoder): | ||
def raw_decode(self, s, **kw): | ||
obj, end = super(Decoder, self).raw_decode(s, **kw) | ||
if isinstance(obj, types.StringTypes): | ||
obj = unicodedata.normalize('NFD', unicode(obj)) | ||
return obj, end | ||
|
||
def load(fp): | ||
return json.load(fp, cls=Decoder, parse_float=decimal.Decimal) | ||
|
||
def loads(s): | ||
return json.loads(s, cls=Decoder, parse_float=decimal.Decimal) | ||
|
||
def tool(): | ||
infile = sys.stdin | ||
outfile = sys.stdout | ||
if len(sys.argv) > 1: | ||
infile = open(sys.argv[1], 'rb') | ||
if len(sys.argv) > 2: | ||
outfile = open(sys.argv[2], 'wb') | ||
if len(sys.argv) > 3: | ||
raise SystemExit("{0} [infile [outfile]]".format(sys.argv[0])) | ||
try: | ||
obj = load(infile) | ||
except ValueError, e: | ||
raise SystemExit(e) | ||
dump(obj, outfile) | ||
outfile.write('\n') | ||
|
||
if __name__ == '__main__': | ||
tool() | ||
|
||
if len(sys.argv) == 1: | ||
infile = sys.stdin | ||
outfile = sys.stdout | ||
elif len(sys.argv) == 2: | ||
infile = sys.std |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# | ||
# Copyright 2009 New England Biolabs <davisp@neb.com> | ||
# | ||
# This file is part of the nebgb package released under the MIT license. | ||
# | ||
|
||
import os | ||
from distutils.core import setup | ||
|
||
long_desc = """ | ||
Canonical serializations for JSON. | ||
Similar to the basic API for the `json`/`simplejson` modules with | ||
a few restrictions on modifiying serialization. | ||
""" | ||
|
||
|
||
setup( | ||
name = "jsonical", | ||
description = "Canonical JSON", | ||
long_description = long_desc, | ||
author = "Paul Joseph Davis", | ||
author_email = "paul.joseph.davis@gmail.com", | ||
url = "http://github.com/davisp/jsonical", | ||
version = "0.0.1", | ||
license = "MIT", | ||
keywords = "JSON canonical serialization", | ||
platforms = ["any"], | ||
zip_safe = True, | ||
py_modules = ["jsonical"] | ||
) |