chunk_norris
Описание
That’s not a kick… THIS is a kick! A simple pure Dart library for working with chunked JSON
Языки
- Dart100%
Chunk Norris 🥋 
Chuck Norris doesn't wait for JSON to load. JSON loads instantly when Chuck Norris needs it.
That's exactly what this library does - it loads JSON so fast, it's almost supernatural!
Chunk Norris is a powerful Dart library for progressive JSON hydration. It allows you to work with JSON data that arrives in chunks, using placeholders that get resolved as data becomes available. Perfect for streaming APIs, Server-Sent Events, and any scenario where you need to handle partial data loading.
🚀 Features
- Progressive Loading: Load JSON data incrementally as chunks arrive
- Placeholder System: Use
,$1(or custom placeholder patterns), etc. as placeholders that get resolved dynamically$2 - Type Safety: Strongly typed access to your data with automatic deserialization
- Streaming Support: Built-in support for data streams (SSE, WebSockets, etc.)
- State Management: Track loading states (pending, loaded, error) for each chunk
- Flexible API: Work with raw JSON or strongly typed objects
- Error Handling: Comprehensive error handling with fallback mechanisms
📦 Installation
Add this to your :
🎯 Quick Start
Basic ChunkJson Usage
Typed ChunkObject Usage
🔧 Advanced Features
Working with ChunkField
provides type-safe access to chunked data with built-in deserializers:
Streaming Data Processing
State Management
Error Handling
🎛️ API Reference
ChunkJson
| Method | Description |
|---|---|
| Create instance from JSON with placeholders |
| Get resolved value for key |
| Get Future for value |
| Get loading state for key |
| Get fully resolved JSON |
| Process incoming chunk |
| Process chunk stream |
| Wait for all chunks to load |
| Listen for data updates |
| Check if all chunks are loaded |
| Clean up resources |
ChunkObject
| Method | Description |
|---|---|
| Create typed object instance |
| Get fully resolved object (throws if not ready) |
| Get object or null if not ready |
| Process incoming chunk |
| Wait for all data to load |
| Check if all chunks are loaded |
| Get typed chunk field by key |
| Check if specific field is ready |
| Get state of specific field |
| Get field error (if any) |
| Get unmodifiable map of chunk fields |
ChunkField
| Property/Method | Description |
|---|---|
| Current loading state |
| Whether field is loaded |
| Whether field has error |
| Get resolved value (throws if not ready) |
| Get resolved value or null |
| Future that completes when loaded |
| Resolve field with data |
ChunkState
| State | Description |
|---|---|
| Data not yet loaded |
| Data successfully loaded |
| Error occurred during loading |
🎨 Custom Placeholder Patterns
By default, Chunk Norris uses the pattern for placeholders (e.g., , ). You can customize this pattern by providing a custom RegExp to match your specific needs.
Using Custom Patterns
Custom Patterns with ChunkObject
Pattern Requirements
Your custom RegExp pattern must:
- Include exactly one capture group
for the placeholder ID() - Match the complete placeholder string (use
and^anchors)$ - Extract a unique identifier from the first capture group
Pattern Examples
| Pattern | RegExp | Matches | Extracts |
|---|---|---|---|
| Default | | , | , |
| Braces | | , | , |
| Variables | | , | , |
| Custom | | , | , |
🌟 Use Cases
Server-Sent Events, WebSockets (SSE, WS)
Perfect for real-time data streaming where the initial response contains a skeleton and subsequent events fill in the details.
Progressive Web Apps
Load critical data first, then enhance with additional information as it becomes available.
API Optimization
Reduce initial response times by sending partial data immediately and streaming the rest.
Data Aggregation from Multiple Sources
Combine data from different sources (databases, APIs, files) into a single unified model. Load available data immediately and fill in missing pieces as they become available from slower sources.
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
📝 License
This project is licensed under the MIT License - see the LICENSE file for details.
Remember: When Chuck Norris needs JSON data, it loads instantly. When you need JSON data, use Chunk Norris! 🥋