lib-orjson

Serialize JSON with sorted keys using orjson and fallback to json.dumps.

Updated May 20, 2025
One-click install
npx skills add https://github.com/flext-sh/flext --skill lib-orjson
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: lib-orjson
Source: https://github.com/flext-sh/flext/tree/main/.claude/skills/lib-orjson
Command: npx skills add https://github.com/flext-sh/flext --skill lib-orjson

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes rules (resource) components.

What problem does it solve?

Ensures consistent and high-performance JSON serialization, preventing data inconsistencies and improving processing speed in critical utility functions.

Core Features & Use Cases

  • Deterministic Ordering: Guarantees that JSON output is always sorted by keys, essential for caching and comparisons.
  • High Performance: Leverages the orjson library for significantly faster serialization than the standard json module.
  • Robust Fallback: Includes a fallback to json.dumps for unsupported types or serialization errors, maintaining stability.
  • Use Case: When serializing complex Python objects for cache keys or inter-service communication, lib-orjson ensures that the resulting JSON strings are identical every time, preventing cache misses and ensuring reliable data exchange.

Quick Start

Ensure all orjson.dumps calls in flext-core/src/flext_core/_utilities/validation.py include option=orjson.OPT_SORT_KEYS and decode the output to a string using c.Utilities.DEFAULT_ENCODING.

Frequently Asked Questions about lib-orjson

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I make JSON serialization deterministic in Python for consistent cache keys?

Deterministic JSON serialization in Python is achieved by enforcing sorted key ordering using orjson with the OPT_SORT_KEYS option, ensuring identical objects produce identical JSON strings for reliable caching and comparisons.

Why does orjson return bytes and how do I get a string for inter-service communication?

The orjson library outputs bytes by default; you must explicitly decode the bytes to a string using standard UTF-8 encoding to obtain a usable JSON string for inter-service communication or caching.

What is the best way to speed up Python JSON serialization for high-performance applications?

Using the orjson library is the best way to speed up Python JSON serialization, offering significantly faster processing than the standard json module for high-performance utility functions.

Does this deterministic JSON serialization approach handle unsupported Python types?

Yes, the deterministic serialization approach includes a robust fallback to the standard json.dumps function for unsupported types, maintaining error resilience and stability when orjson encounters complex objects.

When do I need deterministic JSON serialization in Python utilities?

You need deterministic JSON serialization when generating stable cache keys or comparing complex Python objects, ensuring identical data structures always produce identical JSON output to prevent cache misses and data mismatches.