Commit 2dab0753 authored by SVN-Git Migration's avatar SVN-Git Migration

Imported Upstream version 1.4

parents
Authors of the demjson python module.
* Deron Meranda <deron.meranda@gmail.com>
Change history for demjson python module.
Version 1.4 released 2008-12-17
* Changed license to LGPL 3 (GNU Lesser General Public License) or
later. Older versions still retain their original licenses.
* No changes other than relicensing were made.
Version 1.3 released 2008-03-19
* Change the default value of escape_unicode to False rather than
True.
* Parsing JSON strings was not strict enough. Prohibit multi-line
string literals in strict mode. Also prohibit control characters
U+0000 through U+001F inside string literals unless they are
\u-escaped.
* When in non-strict mode where object keys may be JavaScript
identifiers, allow those identifiers to contain '$' and '_'. Also
introduce a method, decode_javascript_identifier(), which converts
a JavaScript identifier into a Python string type, or can be
overridden by a subclass to do something different.
* Use the Python decimal module if available for representing
numbers that can not fit into a float without loosing precision.
Also encode decimal numbers into JSON and use them as a source
for NaN and Infinity values if necessary.
* Allow Python complex types to be encoded into JSON if their
imaginary part is zero.
* When parsing JSON numbers try to keep whole numbers as integers
rather than floats; e.g., '1e+3' will be 1000 rather than 1000.0.
Also make sure that overflows and underflows (even for the larger
Decimal type) always result in Infinity or -Infinity values.
* Handle more Python collection types when creating JSON; such as
deque, set, array, and defaultdict. Also fix a bug where UserDict
was not properly handled because of it's unusual iter() behavior.
Version 1.2 released 2007-11-08
* Changed license to GPL 3 or later. Older versions still retain
their original licenses.
* Lint Validator: Added a "jsonlint" command-line utility for
validating JSON data files, and/or reformatting them.
* Major performance enhancements. The most signifant of the many
changes was to use a new strategy during encoding to use lists and
fast list operations rather than slow string concatenation.
* Floating-Point Precision: Fixed a bug which could cause loss of
precision (e.g., number of significant digits) when encoding
floating-point numbers into their JSON representation. Also, the
bundled test suite now properly tests floating-point encoding
allowing for slight rounding errors which may naturally occur on
some platforms.
* Very Large Hex Numbers: Fixed a bug when decoding very large
hexadecimal integers which could result in the wrong value for
numbers larger than 0xffffffff. Note that the language syntax
allows such huge numbers, and since Python supports them too this
module will decode such numbers. However in practice very few
other JSON or Javascript implementations support arbitrary-size
integers. Also hex numbers are not valid when in strict mode.
* According to the JSON specification a document must start with
either an object or an array type. When in strict mode if the
first non-whitespace object is any other type it should be
considered to be an invalid document. The previous version
erroneously decoded any JSON value (e.g., it considered the
document "1" to be valid when it should not have done so.
Non-strict mode still allows any type, as well as by setting
the new behavior flag 'allow_any_type_at_start'.
* Exception Handling: Minor improvements in exception handling by
removing most cases where unbounded catching was performed (i.e.,
an "except:" with no specified exception types), excluding during
module initialization. This will make the module more
caller-friendly, for instance by not catching and "hiding"
KeyboardInterrupt or other asyncrhonous exceptions.
* Identifier Parsing: The parser allows a more expanded syntax for
Javascript identifiers which is more compliant with the ECMAscript
standard. This will allow, for example, underscores and dollar
signs to appear in identifiers. Also, to provide further
information to the caller, rather than converting identifiers into
Python strings they are converted to a special string-subclass.
Thus they will look just like strings (and pass the
"isinstance(x,basestring)" test), but the caller can do a type test
to see if the value originated from Javascript identifiers or string literals.
Note this only affects the non-strict (non-JSON) mode.
* Fixed a liberal parsing bug which would successfully decode JSON
["a" "b"] into Python ['a', 'b'], rather than raising a syntax
error for the missing comma.
* Fixed a bug in the encode_default() method which raised the
wrong kind of error. Thanks to Nicolas Bonardelle.
* Added more test cases to the bundled self-test program (see
test/test_demjson.py). There are now over 180 individual
test cases being checked.
Version 1.1 released 2006-11-06
* Extensive self testing code is now included, conforming to the
standard Python unittest framework. See the INSTALL.txt file for
instructions.
* Corrected character encoding sanity check which would erroneously
complain if the input contained a newline or tab character within
the first two characters.
* The decode() and encode() top-level functions now allow additional
keyword arguments to turn specific behaviors on or off that
previously could only be done using the JSON class directly. The
keyword arguments look like 'allow_comments=True'. Read the
function docstrings for more information on this enhancement.
* The decoding of supplementary Unicode character escape sequences
(such as "\ud801\udc02") was broken on some versions of Python.
These are now decoded explicitly without relying on Python so they
always work.
* Some Unicode encoding and decoding with UCS-4 or UTF-32 were not
handled correctly.
* Encoding of pseudo-string types from the UserString module are
now correctly encoded as if real strings.
* Improved simulation of nan, inf, and neginf classes used if the
Python interpreter doesn't support IEEE 754 floating point math.
* Updated the documentation to describe why this module does not
permit multi-line string literals.
Version 1.0 released 2006-08-10
* Initial public release
== Basic installation
This software is published in the Python Package Index (PYPI), formerly
known as the Python Cheeseshop. If you have the setuptools installed,
you may be able to install this module with just:
easy_install demjson
Otherwise, you can install it by downloading the distrubtion and
unpacking it in some temporary directory. Then inside that
directory, type:
python setup.py install
Optionally, for a minimal installation, you can also just copy the
"demjson.py" file into your own project's directory.
== jsonlint command
You can optionally install the script jsonlint by copying it to
any directory on your PATH, such as into /usr/local/bin under
Unix/Linux. Make sure the script is set as being executable.
== Running self tests
Self tests are included which conform to the Python unittest
framework. To run these tests, do
cd test
PYTHONPATH=.. python test_demjson.py
This diff is collapsed.
News announcements regarding the demjson python module.
See the file CHANGES.txt for more details.
2008-12-17 Release 1.4, license changed to LGPL 3 (no code changes).
2008-03-19 Release 1.3, bug fixes, numeric improvements, Decimal, more collections types.
2007-11-08 Release 1.2, bug fixes, speed improvements, jsonlint, license now GPL 3.
2006-11-06 Release 1.1, bug fix and minor enhancements.
2006-08-10 Release 1.0, initial release.
Metadata-Version: 1.0
Name: demjson
Version: 1.4
Summary: encoder, decoder, and lint/validator for JSON (JavaScript Object Notation) compliant with RFC 4627
Home-page: http://deron.meranda.us/python/demjson/
Author: Deron Meranda
Author-email: deron.meranda@gmail.com
License: GNU LGPL 3.0
Download-URL: http://deron.meranda.us/python/demjson/dist/demjson-1.4.tar.gz
Description: This module provides classes and functions for encoding or decoding data
represented in the language-neutral JSON format (which is often used as a
simpler substitute for XML in Ajax web applications). This implementation tries
to be as compliant to the JSON specification (RFC 4627) as possible, while
still providing many optional extensions to allow less restrictive JavaScript
syntax. It includes complete Unicode support, including UTF-32, BOM, and
surrogate pair processing. It can also support JavaScript's
NaN and Infinity numeric types as well as it's 'undefined' type.
It also includes a lint-like JSON syntax validator which tests JSON text
for strict compliance to the standard.
Keywords: JSON,jsonlint,JavaScript,UTF-32
Platform: UNKNOWN
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL)
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content
'demjson' is a Python language module for encoding and decoding JSON data.
REQUIRES PYTHON 2.3 OR HIGHER, DOES NOT WORK IN PYTHON 3000 (3.x)
To install, type:
python setup.py install
or optionally just copy the file "demjson.py" to whereever you want.
See INSTALL.txt for mo detailed instructions.
In addition to the module "demjson", there is also an included script
"jsonlint" which is meant to be used as a command-line tool. You can
invoke "jsonlint --help" for its usage isntructions.
The module is self-documented, so within the python interpreter type:
import demjson
help(demjson)
or from a command line:
pydoc demjson
This software is Free Software and is licensed under the terms of the
GNU LGPL (GNU Lesser General Public License). More information is
found at the top of the demjson.py source file and the included
LICENSE.txt file.
Written by Deron Meranda
http://deron.meranda.us/python/demjson/
In general thanks to the entire Python community for providing such
a wonderful language. Thanks also to the folks at json.org for
developing the JSON data format and pursuing an RFC standard for it.
Reported bugs:
* Adam Braun -- reported bug in 1.0 release regarding newlines being
rejected during decoding if at the beginning of the input.
* Nicolas Bonardelle -- reported bug in 1.1 release where the wrong
kind of exception could be raised by encode_default().
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Usage: jsonlint [-v] [-s|-S] [-f|-F] [-e codec] inputfile.json ...
The return status will be 0 if the file is legal JSON, or
non-zero otherwise. Use -v to see the warning details.
Options are:
-v | --verbose Show details of lint checking
-s | --strict Be strict in what is considered legal JSON (the default)
-S | --nonstrict Be loose in what is considered legal JSON
-f | --format Reformat the JSON (if legal) to stdout
-F | --format-compactly
Reformat the JSON simlar to -f, but do so compactly by
removing all unnecessary whitespace
-e codec | --encoding=codec
--input-encoding=codec
--output-encoding=codec
Set the input and output character encoding codec (e.g.,
ascii, utf8, utf-16). The -e will set both the input and
output encodings to the same thing. If not supplied, the
input encoding is guessed according to the JSON
specification. The output encoding defaults to UTF-8, and is
used when reformatting (via the -f or -F options).
When reformatting, all members of objects (associative arrays) are
always output in lexigraphical sort order. The default output codec
is UTF-8, unless the -e option is provided. Any Unicode characters
will be output as literal characters if the encoding permits,
otherwise they will be \u-escaped. You can use "-e ascii" to force
all Unicode characters to be escaped.
#!/usr/bin/env python
# -*- coding: utf8 -*-
r"""A JSON syntax validator and formatter tool.
Requires demjson module.
"""
__author__ = "Deron Meranda <http://deron.meranda.us/>"
__date__ = "2008-12-17"
__version__ = "1.4"
__credits__ = """Copyright (c) 2006-2008 Deron E. Meranda <http://deron.meranda.us/>
Licensed under GNU LGPL (GNU Lesser General Public License) version
3.0 or later. See LICENSE.txt included with this software.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import sys
try:
import demjson
except ImportError:
sys.stderr.write("Can not import the demjson Python module.\n")
sys.stderr.write("Try running: easy_install demjson\n")
sys.exit(1)
def lintcheck_data( jsondata, verbose=False, strict=True, reformat=False, input_encoding=None, output_encoding=None, escape_unicode=True, pfx='' ):
success = False
try:
data = demjson.decode(jsondata, strict=strict, encoding=input_encoding)
except demjson.JSONError, err:
success = False
if verbose:
sys.stderr.write('%s%s\n' % (pfx, err.pretty_description()) )
except UnicodeDecodeError, err:
success = False
if verbose:
sys.stderr.write('%sFile is not text: %s\n' % (pfx, str(err) ))
else:
success = True
if reformat == 'compactly':
newjson = demjson.encode(data, compactly=True, encoding=output_encoding, escape_unicode=escape_unicode)
sys.stdout.write(newjson)
elif reformat:
newjson = demjson.encode(data, compactly=False, encoding=output_encoding, escape_unicode=escape_unicode)
sys.stdout.write(newjson)
sys.stdout.write('\n')
elif verbose:
sys.stdout.write('%sok\n' % pfx)
return success
def lintcheck( filename, verbose=False, strict=True, reformat=False, input_encoding=None, output_encoding=None, escape_unicode=True ):
pfx = '%s: ' % filename
try:
fp = open( filename, 'rb' )
jsondata = fp.read()
fp.close()
except IOError, err:
sys.stderr.write('%s: %s\n' % (pfx, str(err)) )
return False
return lintcheck_data( jsondata, verbose=verbose, strict=strict, reformat=reformat,
input_encoding=input_encoding, output_encoding=output_encoding,
escape_unicode=escape_unicode, pfx=pfx )
main_usage = """Usage: jsonlint [-v] [-s|-S] [-f|-F] [-e codec] inputfile.json ...
The return status will be 0 if the file is legal JSON, or
non-zero otherwise. Use -v to see the warning details.
Options are:
-v | --verbose Show details of lint checking
-s | --strict Be strict in what is considered legal JSON (the default)
-S | --nonstrict Be loose in what is considered legal JSON
-f | --format Reformat the JSON (if legal) to stdout
-F | --format-compactly
Reformat the JSON simlar to -f, but do so compactly by
removing all unnecessary whitespace
-e codec | --encoding=codec
--input-encoding=codec
--output-encoding=codec
Set the input and output character encoding codec (e.g.,
ascii, utf8, utf-16). The -e will set both the input and
output encodings to the same thing. If not supplied, the
input encoding is guessed according to the JSON
specification. The output encoding defaults to UTF-8, and is
used when reformatting (via the -f or -F options).
When reformatting, all members of objects (associative arrays) are
always output in lexigraphical sort order. The default output codec
is UTF-8, unless the -e option is provided. Any Unicode characters
will be output as literal characters if the encoding permits,
otherwise they will be \u-escaped. You can use "-e ascii" to force
all Unicode characters to be escaped.
"""
def main( args ):
import getopt
success = True
verbose = False
strict = True
reformat = False
input_encoding = None
output_encoding = 'utf-8'
escape_unicode = False
try:
opts, args = getopt.getopt( args,
'vfFe:sS',
['verbose',
'format','format-compactly',
'strict','nonstrict',
'encoding=',
'input-encoding=','output-encoding=','help'] )
except getopt.GetoptError:
sys.stderr.write( main_usage )
return 1
for opt, val in opts:
if opt in ('-h', '--help'):
sys.stdout.write( main_usage )
return 0
elif opt in ('-v', '--verbose'):
verbose = True
elif opt in ('-s', '--strict'):
strict = True
elif opt in ('-S', '--nonstrict'):
strict = False
elif opt in ('-f', '--format'):
reformat = True
elif opt in ('-F', '--format-compactly'):
reformat = 'compactly'
elif opt in ('-e','--encoding'):
input_encoding = val
output_encoding = val
escape_unicode = False
elif opt in ('--output-encoding'):
output_encoding = val
escape_unicode = False
elif opt in ('--input-encoding'):
input_encoding = val
else:
sys.stderr.write('Unknown option %r\n' % opt)
return 1
if args:
for fn in args:
if not lintcheck( fn, verbose=verbose, reformat=reformat,
strict=strict,
input_encoding=input_encoding,
output_encoding=output_encoding,
escape_unicode=escape_unicode ):
success = False
else:
jsondata = sys.stdin.read()
if not lintcheck_data( jsondata, verbose=verbose, reformat=reformat,
encoding=encoding, escape_unicode=escape_unicode, pfx='<stdin>: ' ):
success = False
if not success:
return 1
return 0
if __name__ == '__main__':
args = sys.argv[1:]
rc = main( args )
sys.exit(rc)
# -*- python -*-
from distutils.core import setup
name = 'demjson'
version = '1.4'
setup( name=name,
version=version,
py_modules=[name],
author='Deron Meranda',
author_email='deron.meranda@gmail.com',
url='http://deron.meranda.us/python/%s/'%name,
download_url='http://deron.meranda.us/python/%(name)s/dist/%(name)s-%(version)s.tar.gz'\
% {'name':name, 'version':version},
description='encoder, decoder, and lint/validator for JSON (JavaScript Object Notation) compliant with RFC 4627',
long_description="""This module provides classes and functions for encoding or decoding data
represented in the language-neutral JSON format (which is often used as a
simpler substitute for XML in Ajax web applications). This implementation tries
to be as compliant to the JSON specification (RFC 4627) as possible, while
still providing many optional extensions to allow less restrictive JavaScript
syntax. It includes complete Unicode support, including UTF-32, BOM, and
surrogate pair processing. It can also support JavaScript's
NaN and Infinity numeric types as well as it's 'undefined' type.
It also includes a lint-like JSON syntax validator which tests JSON text
for strict compliance to the standard.""",
license='GNU LGPL 3.0',
keywords=['JSON','jsonlint','JavaScript','UTF-32'],
platforms=[],
classifiers=['Development Status :: 5 - Production/Stable',
'Intended Audience :: Developers',
'License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL)',
'Operating System :: OS Independent',
'Programming Language :: Python',
'Topic :: Software Development :: Libraries :: Python Modules',
'Topic :: Internet :: WWW/HTTP :: Dynamic Content'
]
)
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment