persistent_pineapple._json¶
This module contains a wrapper for Python’s built-in json module to make it easier to use.
-
class
persistent_pineapple._json.
CommentedJSON
¶ This object is used to _approximately_ keep the comments in the stored text file. The order and format of the original file is not kept. The data is translated to Python native objects. In the case of a dictionary, key order is not guaranteed. This object does not attempt to re-order the output.
Known Issues
- Line comments not immediately preceding a setting are ignored
/* */
style comments are discarded- Block comments not associated with a setting are ignored, with the exception of a comment at the top of the file (called the _header_)
- Settings with the same name will collide
- Guaranteed not to work well with complex JSON
TODO
Find/write a proper lexer
-
load
(string=None, path=None, force_ascii=True)¶ Loads JSON data from either a string or a file. The _template_ of the file will be used during as_string() and store().
Parameters: - string (basestring) – The string to parse
- path (basestring) – The path of the file to parse
- force_ascii (bool) – For lazy people who refuse to use unicode; the data returned is guaranteed to be ascii.
Returns: JSON encoded data
-
store
(data, path, cls=<class 'persistent_pineapple._json.MyEncoder'>)¶ Write the data to a JSON formatted file while attempting to keep comments.
-
class
persistent_pineapple._json.
JSON
¶ This object is used to load JSON data from a string or file. It will remove any comment-only lines, and try to give some indication of why JSON failed to load (e.g. trailing comma).
JSON reads much like Python; [] is for a list of items, {} is for a dictionary.
Some important notes:
- single-quotes (B{‘}) are invalid; always use double-quotes (B{“})
- A Python B{None} is represented by a JSON B{null}
- A list cannot end with a trailing comma (a comma after the last item in the list)
Comments:
- JSON does not supports comments. This functionality was added to this class to make life easier.
We support the following types of comments
- A line consisting of 0 or more whitespace characters followed by
//
- Text ending a line with 1 or more whitespace characters followed
by
//
- All text in between
/*
and*/
Examples
1 2 3 4 5
// this line is ignored {"key": 5} // the rest of the line is ignored {"key": 5 /*,"key2": 6*/} // key2 and the remainder is ignored {"key": 5}// INVALID! At least 1 whitespace character // before // for hanging comments
-
load
(string=None, path=None, force_ascii=True)¶ Loads JSON data from either a string or a file.
-
store
(data, path, cls=<class 'persistent_pineapple._json.MyEncoder'>)¶ Write the data to a JSON formatted file.
-
class
persistent_pineapple._json.
JSONComment
(_type, line_number, comment, setting=None)¶ A simple object to contain information about a comment found in a JSON file/string.
-
class
persistent_pineapple._json.
MyEncoder
(skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, encoding='utf-8', default=None)¶ Simple decoder that returns the dict of an object.
-
persistent_pineapple._json.
container_to_ascii
(item)¶ Converts all items from unicode to ascii, where needed. This is a recursive function. You may pass in any container or object, however, only basic types will be converted: scalars, lists, and dictionaries. Everything else will be ignored.
- Examples::
>>> print(container_to_ascii(int(4))) 4 >>> print(container_to_ascii(['test', u'test'])) ['test', 'test'] >>> print(container_to_ascii({u'one': u'test', u'two': 2, u'three': ... {'test': 2}, 4: [1, 2, 3, u'four']})) {'one': 'test', 4: [1, 2, 3, 'four'], 'three': {'test': 2}, 'two': 2} >>>
-
persistent_pineapple._json.
encode
(data, cls=<class 'persistent_pineapple._json.MyEncoder'>)¶ encode the provided data in JSON format