From 07900e5008f4876d2d9db545941b3d06d98e26b7 Mon Sep 17 00:00:00 2001 From: Joshua Cline Date: Sat, 1 Apr 2023 07:51:08 -0700 Subject: [PATCH] Update README.md Fixes example so data doesn't get lost in recursion. Creates valid workaround for #138 --- README.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index fc4f0fc..5cebb1f 100644 --- a/README.md +++ b/README.md @@ -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; + const array = decode(data, { extensionCodec }) as Array; return new Set(array); }, }); @@ -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); }, }); @@ -304,7 +304,9 @@ const encoded = encode([new Set(), new Map()], { 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