Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
Fixes example so data doesn't get lost in recursion.

Creates valid workaround for msgpack#138
  • Loading branch information
jcc10 authored Apr 1, 2023
1 parent e615511 commit 07900e5
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,13 @@ extensionCodec.register({
type: SET_EXT_TYPE,
encode: (object: unknown): Uint8Array | null => {
if (object instanceof Set) {
return encode([...object]);
return encode([...object], { extensionCodec });
} else {
return null;
}
},
decode: (data: Uint8Array) => {
const array = decode(data) as Array<unknown>;
const array = decode(data, { extensionCodec }) as Array<unknown>;
return new Set(array);
},
});
Expand All @@ -289,13 +289,13 @@ extensionCodec.register({
type: MAP_EXT_TYPE,
encode: (object: unknown): Uint8Array => {
if (object instanceof Map) {
return encode([...object]);
return encode([...object], { extensionCodec });
} else {
return null;
}
},
decode: (data: Uint8Array) => {
const array = decode(data) as Array<[unknown, unknown]>;
const array = decode(data, { extensionCodec }) as Array<[unknown, unknown]>;
return new Map(array);
},
});
Expand All @@ -304,7 +304,9 @@ const encoded = encode([new Set<any>(), new Map<any, any>()], { extensionCodec }
const decoded = decode(encoded, { extensionCodec });
```

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

Note that extension types for custom objects must be `[0, 127]`, while `[-1, -128]` is reserved for MessagePack itself.

#### ExtensionCodec context

Expand Down

0 comments on commit 07900e5

Please sign in to comment.