You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Register your own classes for CBOR encoding/decoding.
145
+
Register your own classes for CBOR encoding/decoding. The `native_ext_type` DSL declares which ivars to encode/decode and their expected Ruby type. Any Ruby class works as a type constraint — including other registered classes, enabling nested structures.
146
+
147
147
```ruby
148
-
classPerson
149
-
attr_accessor:name, :age
148
+
classAddress
149
+
native_ext_type :@street, String
150
+
native_ext_type :@city, String
151
+
end
150
152
151
-
# Declare which fields to encode/decode and their expected CBOR types
152
-
native_ext_type :@name, CBOR::Type::String
153
-
native_ext_type :@age, CBOR::Type::Integer
153
+
CBOR.register_tag(1001, Address)
154
+
155
+
classPerson
156
+
native_ext_type :@name, String
157
+
native_ext_type :@age, Integer
158
+
native_ext_type :@address, Address# nested registered class
154
159
155
160
# Called after decoding (optional)
156
161
def_after_decode
157
162
puts"Person #{@name} loaded"
158
163
self
159
164
end
160
165
161
-
# Called before encoding (optional)
162
-
# Must return self or a modified object
166
+
# Called before encoding (optional). Must return self or a modified object.
163
167
def_before_encode
164
168
@age+=1if@age<18
165
169
self
166
170
end
167
171
end
168
172
169
-
# Register with a tag number (user-defined: 1000+)
170
173
CBOR.register_tag(1000, Person)
171
174
175
+
addr =Address.new
176
+
addr.instance_variable_set(:@street, "Main St")
177
+
addr.instance_variable_set(:@city, "Berlin")
178
+
172
179
person =Person.new
173
-
person.name ="Alice"
174
-
person.age =30
180
+
person.instance_variable_set(:@name, "Alice")
181
+
person.instance_variable_set(:@age, 30)
182
+
person.instance_variable_set(:@address, addr)
175
183
176
184
encoded =CBOR.encode(person)
177
185
decoded =CBOR.decode(encoded) # => Person object, _after_decode called
-`CBOR::Type::Tagged` (for Bigint, your own registered Tags)
187
-
-`CBOR::Type::Simple` (nil, false, true, Floats)
192
+
**Type constraints use standard Ruby classes:**
193
+
194
+
| Schema type | Accepts |
195
+
|-------------|---------|
196
+
|`String`| UTF-8 strings and byte strings |
197
+
|`Integer`| Integer values (fixnum or bigint) |
198
+
|`Float`| Floating-point values |
199
+
|`Array`| Arrays |
200
+
|`Hash`| Maps |
201
+
|`TrueClass` / `FalseClass`| Booleans |
202
+
|`NilClass`| nil |
203
+
| Any registered class | Instances of that class (or subclasses) |
204
+
205
+
Type checking uses `is_a?`, so inheritance works: a schema of `Numeric` accepts both `Integer` and `Float`, and a schema of `Animal` accepts any subclass of `Animal`.
188
206
189
-
Convenience Types:
190
-
-`CBOR::Type::BytesOrString`
191
-
-`CBOR::Type::Integer`
207
+
Fields absent from a decoded payload are silently skipped — only declared ivars are populated (allowlist model). Extra fields in the payload are ignored.
192
208
193
209
---
194
210
@@ -280,8 +296,6 @@ end
280
296
Use this when you control the read loop yourself, e.g. in an event-driven or async context:
0 commit comments