Skip to content

Commit 07900e5

Browse files
authored
Update README.md
Fixes example so data doesn't get lost in recursion. Creates valid workaround for msgpack#138
1 parent e615511 commit 07900e5

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

README.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -272,13 +272,13 @@ extensionCodec.register({
272272
type: SET_EXT_TYPE,
273273
encode: (object: unknown): Uint8Array | null => {
274274
if (object instanceof Set) {
275-
return encode([...object]);
275+
return encode([...object], { extensionCodec });
276276
} else {
277277
return null;
278278
}
279279
},
280280
decode: (data: Uint8Array) => {
281-
const array = decode(data) as Array<unknown>;
281+
const array = decode(data, { extensionCodec }) as Array<unknown>;
282282
return new Set(array);
283283
},
284284
});
@@ -289,13 +289,13 @@ extensionCodec.register({
289289
type: MAP_EXT_TYPE,
290290
encode: (object: unknown): Uint8Array => {
291291
if (object instanceof Map) {
292-
return encode([...object]);
292+
return encode([...object], { extensionCodec });
293293
} else {
294294
return null;
295295
}
296296
},
297297
decode: (data: Uint8Array) => {
298-
const array = decode(data) as Array<[unknown, unknown]>;
298+
const array = decode(data, { extensionCodec }) as Array<[unknown, unknown]>;
299299
return new Map(array);
300300
},
301301
});
@@ -304,7 +304,9 @@ const encoded = encode([new Set<any>(), new Map<any, any>()], { extensionCodec }
304304
const decoded = decode(encoded, { extensionCodec });
305305
```
306306

307-
Not that extension types for custom objects must be `[0, 127]`, while `[-1, -128]` is reserved for MessagePack itself.
307+
Ensure you include your extensionCodec in any recursive encode and decode statements!
308+
309+
Note that extension types for custom objects must be `[0, 127]`, while `[-1, -128]` is reserved for MessagePack itself.
308310

309311
#### ExtensionCodec context
310312

0 commit comments

Comments
 (0)