Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deserialisation fails for nested Maps and Sets #12

Open
I-Enderlord-I opened this issue Mar 26, 2024 · 1 comment
Open

Deserialisation fails for nested Maps and Sets #12

I-Enderlord-I opened this issue Mar 26, 2024 · 1 comment

Comments

@I-Enderlord-I
Copy link

While both serialisation and deserialisation workds fine if Maps and Sets are only used on top level, the deserialisation throws when they are nested in other objects.

const { desia, sia, DeSia, Sia } = require('sializer');

let obj = new Map();
let buf = sia(obj);
obj = desia(buf); //fine

obj = { k: new Map() };
buf = sia(obj);
obj = desia(buf); //throws string "Key of type 57 is invalid."

This bug is apparently already fixed in the fork by @c7x43t, but that fork does not yet have TypeScript support.

As far as I can tell, the Problem is simply that this.offset in the Deserialiser is not incremented at the end of

      case SIA_TYPES.mapStart: {
        const map = new Map();
        let curr = this.buffer[this.offset];
        while (curr !== SIA_TYPES.mapEnd) {
          map.set(this.readBlock(), this.readBlock());
          curr = this.buffer[this.offset];
        }
        //insert increment here
        return map;
      }

to skip over the SIA_TYPES.mapEnd byte, which is not incremented over before, because it might be the start of the next block. So adding this.offset++; before the return fixes it.

I tried this fix locally for both Map and Set and confirmed that deserialisation works as expected then.

@pouya-eghbali
Copy link
Owner

@I-Enderlord-I I will make a v1 branch and merge #11 to solve this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants