jsonlib-python3

JSON serializer/deserializer for Python
Download

jsonlib-python3 Ranking & Summary

Advertisement

  • Rating:
  • License:
  • GPL
  • Price:
  • FREE
  • Publisher Name:
  • John Millikin
  • Publisher web site:
  • https://launchpad.net/jsonlib

jsonlib-python3 Tags


jsonlib-python3 Description

JSON serializer/deserializer for Python JSON is a lightweight data-interchange format. It is often used for exchanging data between a web server and user agent.jsonlib-python3 is a Python module that aims to produce a library for serializing and deserializing JSON that conforms strictly to RFC 4627.Usagejsonlib has two functions of interest, read and write. It also defines some exception: ReadError, WriteError, and UnknownSerializerError.For compatibility with the standard library, read is aliased to loads and write is aliased to dumps. They do not have the same set of advanced parameters, but may be used interchangeably for simple invocations.DeserializationTo deserialize a JSON expression, call the jsonlib.read function with an instance of str or bytes.>>> import jsonlib>>> jsonlib.read (b'')Floating-point valuesBy default, jsonlib will parse values such as "1.1" into an instance of decimal.Decimal. To use the built-in value type float instead, set the use_float parameter to True. Please note that this may cause a loss of precision when parsing some values.>>> jsonlib.read ('', use_float = True)>>> jsonlib.read ('', use_float = True)>>> jsonlib.read ('', use_float = True)SerializationSerialization has more options, but they are set to reasonable defaults. The simplest use is to call jsonlib.write with a Python value.>>> import jsonlib>>> jsonlib.write ()b''Pretty-PrintingTo "pretty-print" the output, pass a value for the indent parameter.>>> print (jsonlib.write (, indent = ' ').decode ('utf8'))>>>Mapping Key SortingBy default, mapping keys are serialized in whatever order they are stored by Python. To force a consistent ordering (for example, in doctests) use the sort_keys parameter.>>> jsonlib.write ({'e': 'Hello', 'm': 'World!'})b'{"m":"World!","e":"Hello"}'>>> jsonlib.write ({'e': 'Hello', 'm': 'World!'}, sort_keys = True)b'{"e":"Hello","m":"World!"}'Encoding and UnicodeBy default, the output is encoded in UTF-8. If you require a different encoding, pass the name of a Python codec as the encoding parameter.>>> jsonlib.write (, encoding = 'utf-16-be')b'x00'To retrieve an unencoded unicode instance, pass None for the encoding.>>> jsonlib.write (, encoding = None)''By default, non-ASCII codepoints are forbidden in the output. To include higher codepoints in the output, set ascii_only to False.>>> jsonlib.write (, encoding = None)''>>> jsonlib.write (, encoding = None, ascii_only = False)''Mapping Key CoercionBecause JSON objects must have string keys, an exception will be raised when non-string keys are encountered in a mapping. It can be useful to coerce mapping keys to strings, so the coerce_keys parameter is available.>>> jsonlib.write ({True: 1})Traceback (most recent call last):jsonlib.WriteError: Only strings may be used as object keys.>>> jsonlib.write ({True: 1}, coerce_keys = True)b'{"True":1}'Serializing Other TypesIf the object implements the iterator or mapping protocol, it will be handled automatically. If the object is intended for use as a basic value, it should subclass one of the supported basic values.String-like objects that do not inherit from unicode or UserString.UserString will likely be serialized as a list. This will not be changed. If iterating them returns an instance of the same type, the serializer might crash. This (hopefully) will be changed.To serialize a type not known to jsonlib, use the on_unknown parameter to write:>>> from datetime import date>>> def unknown_handler (value, unknown):... if isinstance (value, date):... return str (value)... unknown (value)>>> jsonlib.write (, on_unknown = unknown_handler)b''Streaming SerializerWhen serializing large objects, the use of an in-memory buffer may cause too much memory to be used. For these situations, use the dump function to write objects to a file-like object:>>> import sys>>> jsonlib.dump (, sys.stdout, encoding = None)>>>ExceptionsReadErrorRaised by read if an error was encountered parsing the expression. Will contain the line, column, and character position of the error.Note that this will report the character, not the byte, of the character that caused the error.WriteErrorRaised by write or dump if an error was encountered serializing the passed value.UnknownSerializerErrorA subclass of WriteError that is raised when a value cannot be serialized. See the on_unknown parameter to write. Requirements: · Python What's New in This Release: · Faster parsing


jsonlib-python3 Related Software