Python Rison Encoder/Decoder
"prison" is a Python library providing an encoder and decoder for the Rison data serialization format. Rison (Recursive object notation) is a compact, URL-safe data format designed for representing simple data structures, often used in URLs. The current version is 0.2.1. The library is largely feature-complete for the Rison specification and has a low release cadence.
Common errors
-
ModuleNotFoundError: No module named 'prison'
cause The 'prison' library is not installed in the Python environment where the code is being executed.fixpip install prison -
prison.decoder.ParserException: missing ', '
cause The input string provided to `prison.loads()` is not valid Rison, often due to improper escaping of special characters or malformed Rison syntax.fixEnsure the Rison string conforms to the Rison specification. If constructing the string manually, be mindful of characters like '!', '(', ')', ':', and ',' and use `prison.dumps()` to serialize Python objects into valid Rison. For example, `prison.loads("(key:value)")` for an object, or `prison.loads("!(item1,item2)")` for an array. -
rison decoder error: missing ':'
cause The input Rison string is syntactically incorrect, specifically missing a colon where a key-value pair is expected in an object, or other structural issues.fixVerify that your Rison string adheres to the correct syntax. For object keys, ensure they are followed by a colon and a value, e.g., `(key:value)`. Use `prison.dumps()` to reliably generate correct Rison strings. -
AttributeError: module 'prison' has no attribute 'dumps'
cause This error occurs when attempting to access a function or attribute, such as `dumps` or `loads`, from the 'prison' module incorrectly, often by trying to import them directly when they are intended to be accessed as methods of the imported module itself.fixImport the 'prison' module and access its functions as attributes, e.g., `import prison` then `prison.dumps()` or `prison.loads()`.
Warnings
- gotcha Rison is not JSON. Its syntax and supported data types differ significantly. Direct mental mapping from JSON to Rison often leads to parsing errors or unexpected output. For example, objects use parentheses `()` instead of curly braces `{}`, and unquoted identifiers are common.
- gotcha The `prison` library has not been updated since August 2021 (v0.2.1). While stable and functional for the existing Rison specification, new Python features, performance optimizations, or edge-case bug fixes are unlikely to be implemented.
- gotcha Deserializing Rison data from untrusted sources with `prison.loads()` carries inherent security risks. While Rison's simplicity mitigates some complex attack vectors found in formats like YAML or Pickle, malicious input could still lead to resource exhaustion (e.g., deep nesting) or unexpected data structures.
Install
-
pip install prison
Imports
- loads
from prison import loads
- dumps
from prison import dumps
Quickstart
import prison
data = {'name': 'Alice', 'age': 30, 'tags': ['dev', 'python']}
# Encode Python dict to Rison string
rison_string = prison.dumps(data)
print(f"Encoded Rison: {rison_string}")
# Expected: (name:Alice,age:30,tags:!(dev,python))
# Decode Rison string back to Python dict
decoded_data = prison.loads(rison_string)
print(f"Decoded data: {decoded_data}")
# Expected: {'name': 'Alice', 'age': 30, 'tags': ['dev', 'python']}