|
| 1 | +# Amazon Ion Binary Codec |
| 2 | + |
| 3 | +This library provides high-performance Amazon Ion binary format encoding and decoding capabilities. |
| 4 | + |
| 5 | +## Usage |
| 6 | + |
| 7 | +```typescript |
| 8 | +import {IonEncoderFast, IonDecoder} from '@jsonjoy.com/json-pack/lib/ion'; |
| 9 | + |
| 10 | +const encoder = new IonEncoderFast(); |
| 11 | +const decoder = new IonDecoder(); |
| 12 | + |
| 13 | +const data = {users: [{name: 'Alice', age: 30}], count: 1}; |
| 14 | +const encoded = encoder.encode(data); |
| 15 | +const decoded = decoder.decode(encoded); |
| 16 | +``` |
| 17 | + |
| 18 | +## Important Usage Notes |
| 19 | + |
| 20 | +⚠️ **Instance Reuse Limitation**: Due to internal state management with shared UTF-8 decoders, encoder and decoder instances should **not be reused** across multiple encode/decode operations with complex data. For reliable operation, create fresh instances for each encoding/decoding operation: |
| 21 | + |
| 22 | +```typescript |
| 23 | +// ❌ DON'T: Reuse instances for multiple operations |
| 24 | +const encoder = new IonEncoderFast(); |
| 25 | +const decoder = new IonDecoder(); |
| 26 | +for (const item of items) { |
| 27 | + const encoded = encoder.encode(item); // May cause state corruption |
| 28 | + const decoded = decoder.decode(encoded); |
| 29 | +} |
| 30 | + |
| 31 | +// ✅ DO: Create fresh instances for each operation |
| 32 | +for (const item of items) { |
| 33 | + const encoder = new IonEncoderFast(); |
| 34 | + const decoder = new IonDecoder(); |
| 35 | + const encoded = encoder.encode(item); |
| 36 | + const decoded = decoder.decode(encoded); |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +This limitation primarily affects complex nested objects with many string keys. Simple data structures may work with reused instances, but fresh instances are recommended for guaranteed correctness. |
| 41 | + |
1 | 42 | ## Benchmarks |
2 | 43 |
|
3 | 44 | Encoding: |
|
0 commit comments