### Decoding JSON and getting position information Source: https://github.com/codecobblers/dirtyjson/blob/master/index.rst Demonstrates how to decode JSON with dirtyjson and retrieve line and column number information for elements. ```python >>> import dirtyjson >>> obj = [u'foo', {u'bar': [u'baz', None, 1.0, 2]}] >>> d = dirtyjson.loads("""[\"foo\", /* not fu*/ {bar: [\'baz\', null, 1.0, 2,]} and then ignore this junk""") >>> d == obj True >>> pos = d.attributes(0) # line/column position of first element in array >>> pos.line == 1 True >>> pos.column == 2 True >>> pos = d[1].attributes('bar') # line/column position of 'bar' key/value pair >>> pos.key.line == 1 True >>> pos.key.column == 22 True >>> pos.value.line == 1 True >>> pos.value.column == 27 True ``` -------------------------------- ### Using Decimal instead of float Source: https://github.com/codecobblers/dirtyjson/blob/master/index.rst Demonstrates how to use a custom parser for floats, such as decimal.Decimal, with dirtyjson.loads. ```python >>> import dirtyjson >>> from decimal import Decimal >>> dirtyjson.loads('1.1', parse_float=Decimal) == Decimal('1.1') True ``` -------------------------------- ### Decoding JSON from streams Source: https://github.com/codecobblers/dirtyjson/blob/master/index.rst Illustrates decoding JSON data directly from a file-like object using dirtyjson.load. ```python >>> from dirtyjson.compat import StringIO >>> io = StringIO('["streaming API"]') >>> dirtyjson.load(io)[0] == 'streaming API' True ``` -------------------------------- ### loads function signature Source: https://github.com/codecobblers/dirtyjson/blob/master/README.rst The signature for the loads function, detailing its parameters and their purposes. ```python loads(s[, encoding[, parse_float[, parse_int[, parse_constant[, search_for_first_object[, start_index]]]]]) ``` -------------------------------- ### KeyValuePosition utility class Source: https://github.com/codecobblers/dirtyjson/blob/master/README.rst A utility class to store key and value position information. ```python KeyValuePosition() This is another very simple utility class that contains ``key`` and ``value``. Each of those is a `Position` object specifying the location in the original source string/file of the key and value. It is used for storing the position attributes for `AttributedDict`. ``` -------------------------------- ### Position utility class Source: https://github.com/codecobblers/dirtyjson/blob/master/README.rst A simple utility class to store line and column numbers. ```python Position() This is a very simple utility class that contains ``line`` and ``column``. ``` -------------------------------- ### dirtyjson.Error exception attributes Source: https://github.com/codecobblers/dirtyjson/blob/master/README.rst Details the attributes of the custom Error exception raised by dirtyjson. ```python dirtyjson.Error(msg, doc, pos) ``` -------------------------------- ### Decoding unicode from JSON Source: https://github.com/codecobblers/dirtyjson/blob/master/index.rst Shows how dirtyjson decodes unicode characters, including escaped backslashes. ```python >>> dirtyjson.loads('"\\"foo\\\bar"') == u'"foo\x08ar' True ``` -------------------------------- ### AttributedDict attributes method Source: https://github.com/codecobblers/dirtyjson/blob/master/README.rst Method to retrieve attributes associated with a key. ```python attributes(self, key) Return the attributes associated with the specified *key* or ``None`` if no attributes exist for the key. In our case, we store `KeyValuePosition`. Retrieve position info like this:: ``` ```python pos = d.attributes(key) key_line = pos.key.line key_column = pos.key.column value_line = pos.value.line value_column = pos.value.column ``` -------------------------------- ### AttributedList append method Source: https://github.com/codecobblers/dirtyjson/blob/master/README.rst Method to append a value with optional attributes to a list. ```python append(self, value, attributes=None): Appends *value* to the list and *attributes* to the associated location. In our case, we store `Position`. ``` -------------------------------- ### AttributedList attributes method Source: https://github.com/codecobblers/dirtyjson/blob/master/README.rst Method to retrieve attributes for a value at a given index. ```python attributes(self, index) Returns the attributes for the value at the given *index*. In our case, we store `Position`. Retrieve position info like this:: ``` ```python pos = l.attributes(index) value_line = pos.line value_column = pos.column ``` -------------------------------- ### AttributedDict add_with_attributes method Source: https://github.com/codecobblers/dirtyjson/blob/master/README.rst Method to add a key-value pair with associated attributes. ```python add_with_attributes(self, key, value, attributes) Set the *key* in the underlying ``dict`` to the *value* and also store whatever is passed in as *attributes* for later retrieval. In our case, we store `KeyValuePosition`. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.