EDN Format Reader and Writer

0.7.5 · active · verified Mon Apr 13

edn-format is a Python library that implements the Extensible Data Notation (EDN) format, providing functionalities to read (loads, loads_all) and write (dumps) EDN data, including support for custom tagged elements. The current version, 0.7.5, was released in November 2020, and the project is considered stable and actively maintained on GitHub, despite a less frequent release cadence.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to serialize Python dictionaries containing various EDN types (keywords, sets, booleans, character literals) into an EDN string using `edn_format.dumps`, and then deserialize an EDN string back into Python data using `edn_format.loads`. It highlights the use of `edn_format.Keyword` and `edn_format.Char` for explicit EDN type representation.

import edn_format

# Define some Python data
python_data = {
    edn_format.Keyword('name'): 'Alice',
    edn_format.Keyword('age'): 30,
    edn_format.Keyword('tags'): {'python', 'edn'},
    edn_format.Keyword('active?'): True,
    edn_format.Keyword('notes'): edn_format.Char('\n') # Example of EDN char literal
}

# Dump Python data to EDN string
edn_string = edn_format.dumps(python_data, sort_keys=True, indent=2)
print("--- EDN Output ---")
print(edn_string)
# Expected output might look like:
# {
#   :active? true,
#   :age 30,
#   :name "Alice",
#   :notes \newline,
#   :tags #{"edn" "python"}
# }

# Load EDN string back to Python data
edn_input = "{ :id 123 :items [\"apple\" \"banana\"] :metadata #myapp/custom {\"version\" \"1.0\"} }"
loaded_data = edn_format.loads(edn_input)
print("\n--- Loaded Python Data ---")
print(loaded_data)
# Example of accessing loaded data
# print(loaded_data[edn_format.Keyword('id')]) # 123
# print(loaded_data[edn_format.Keyword('items')]) # ['apple', 'banana']

view raw JSON →