Skip to content

Commit afac0f4

Browse files
committed
2 parents 12a767f + 605a5b9 commit afac0f4

1 file changed

Lines changed: 28 additions & 17 deletions

File tree

README.md

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
| **BigInt Support** | Tags 2/3 (RFC 8949) when compiled with `MRB_USE_BIGINT` |
1515
| **Float Precision** | Float16/32/64 with subnormals, Inf, and NaN |
1616
| **Shared References** | Tags 28/29 for deduplication, including cyclic structures |
17-
| **Zero-Copy Lazy Decoding** | `CBOR::Lazy` for on-demand nested access |
17+
| **Zero-Copy Decoding** | Both eager and lazy decoding operate directly on the input buffer without copying |
18+
| **Lazy Decoding** | `CBOR::Lazy` for on-demand nested access with result caching |
1819
| **Streaming** | `CBOR.stream` for CBOR sequence reading |
1920
| **Performance** | ~30% faster than msgpack; 1.3–3× faster than simdjson for selective access |
2021

@@ -27,7 +28,6 @@
2728
## 🚀 Quick Start
2829

2930
### Basic Encode/Decode
30-
3131
```ruby
3232
# Encode
3333
buf = CBOR.encode({ "hello" => [1, 2, 3], "ok" => true })
@@ -45,10 +45,9 @@ lazy["hello"][1].value # => 2 (constant-time after first access)
4545

4646
## 📖 Usage Guide
4747

48-
### CBOR::Lazy – Zero-Copy On-Demand Access
48+
### CBOR::Lazy – On-Demand Access
4949

5050
`decode_lazy` returns a `CBOR::Lazy` object wrapping the raw buffer **without decoding**. Navigate with `[]` or `dig`, then call `.value` when you need the actual value.
51-
5251
```ruby
5352
lazy = CBOR.decode_lazy(big_payload)
5453

@@ -64,7 +63,6 @@ lazy.dig("missing", "text") # => nil (safe)
6463
```
6564

6665
**Performance:** Access is O(n) only in skipped elements, not the full document. Results are cached for O(1) repeated access.
67-
6866
```ruby
6967
# Repeated access uses cache
7068
inner = lazy["outer"]["inner"].value
@@ -77,7 +75,6 @@ assert_same inner, inner2 # Same object (cached)
7775
### Shared References (Tags 28/29)
7876

7977
Eliminate duplication and represent cyclic structures.
80-
8178
```ruby
8279
# Encode with deduplication
8380
shared_array = [1, 2, 3]
@@ -96,7 +93,6 @@ lazy["x"].value.equal?(lazy["y"].value) # => true ✓
9693
```
9794

9895
**Cyclic Structures:**
99-
10096
```ruby
10197
cyclic = []
10298
cyclic << cyclic
@@ -112,7 +108,6 @@ result.equal?(result[0]) # => true (self-referential)
112108
### Custom Tags & Type Registration
113109

114110
Register your own classes for CBOR encoding/decoding.
115-
116111
```ruby
117112
class Person
118113
attr_accessor :name, :age
@@ -148,7 +143,7 @@ decoded = CBOR.decode(encoded) # => Person object
148143
- `CBOR::Type::Tagged` (for Bigint, your own registered Tags)
149144
- `CBOR::Type::Simple` (nil, false, true, Floats)
150145

151-
Convience Types
146+
Convenience Types:
152147
- `CBOR::Type::BytesOrString`
153148
- `CBOR::Type::Integer`
154149

@@ -157,32 +152,48 @@ Convience Types
157152
### Symbol Handling
158153

159154
Three strategies for encoding Ruby symbols:
160-
161155
```ruby
162-
# 1. Default: convert to strings
163-
CBOR.symbols_as_strings
156+
# 1. Default: convert to strings (no tag)
157+
CBOR.no_symbols
158+
159+
# 2. Use tag 39 + string
160+
CBOR.symbols_as_string
164161

165-
# 2. Use tag 39 + uint32 (mruby-to-mruby only)
162+
# 3. Use tag 39 + uint32 (mruby-to-mruby only)
166163
CBOR.symbols_as_uint32
167164
sym = :hello
168165
encoded = CBOR.encode(sym)
169166
decoded = CBOR.decode(encoded) # => :hello
170167
```
171168

172-
> **⚠️ Warning:** Symbol as uint32 are mruby instance–specific. Only use `symbols_as_uint32` when both encoder and decoder run on the same mruby executable and when all symbols are interned at compile time, see https://github.com/mruby/mruby/blob/master/doc/guides/symbol.md#preallocate-symbols
169+
> **⚠️ Warning:** `symbols_as_uint32` is mruby instance–specific. Only use it when both encoder and decoder run on the same mruby executable and when all symbols are interned at compile time, see https://github.com/mruby/mruby/blob/master/doc/guides/symbol.md#preallocate-symbols
170+
171+
---
172+
173+
### Streaming
174+
175+
Read a sequence of CBOR documents from any IO-like object:
176+
```ruby
177+
File.open("data.cbor", "rb") do |f|
178+
CBOR.stream(f) do |doc|
179+
puts doc.value.inspect
180+
end
181+
end
182+
183+
# Or as an enumerator
184+
docs = CBOR.stream(io).map(&:value)
185+
```
173186

174187
---
175188

176189
## 📦 Installation
177190

178191
Add to your `build_config.rb`:
179-
180192
```ruby
181193
conf.gem github: "Asmod4n/mruby-cbor"
182194
```
183195

184196
Then build:
185-
186197
```bash
187198
rake compile
188199
rake test
@@ -199,4 +210,4 @@ rake test
199210

200211
## 📄 License
201212

202-
Apache License 2.0
213+
Apache License 2.0

0 commit comments

Comments
 (0)