Python RapidJSON

1.23 · active · verified Thu Apr 09

Python-rapidjson is a Python 3 wrapper for the extremely fast C++ JSON parser and serialization library, RapidJSON. It provides high-performance JSON serialization and deserialization, including JSON Schema validation capabilities, while aiming for compatibility with the standard `json` module API. As of version 1.23, it maintains an active release cadence, frequently updating to support new Python versions and upstream RapidJSON improvements.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the basic serialization (`dumps`) and deserialization (`loads`) functionality of `python-rapidjson`, which mirrors the standard library `json` module. It also includes an example of using the `Decoder` class with `parse_mode` flags to handle more relaxed JSON syntax, such as comments and trailing commas.

import rapidjson
import os

# Example data
data = {'name': 'Alice', 'age': 30, 'isStudent': False}

# Serialize to JSON string
json_string = rapidjson.dumps(data)
print(f"Serialized: {json_string}")

# Deserialize from JSON string
loaded_data = rapidjson.loads(json_string)
print(f"Deserialized: {loaded_data}")

# Example with custom Decoder for relaxed syntax (JSONC, trailing commas)
try:
    from rapidjson import Decoder, PM_COMMENTS, PM_TRAILING_COMMAS
    decoder = Decoder(parse_mode=PM_COMMENTS | PM_TRAILING_COMMAS)
    relaxed_json = '''
    {
        "item": "value", /* This is a comment */
        "count": 123,   // Another comment
        "enabled": true, // Trailing comma
    }
    '''
    parsed_relaxed = decoder(relaxed_json)
    print(f"Parsed relaxed JSON: {parsed_relaxed}")
except ImportError:
    print("Custom Decoder features not available (might be an older version or specific flags are missing).")

view raw JSON →