fastify/fast-json-stringify

Any plans to add support replacer functionality?

Mika83AC opened this issue ยท 14 comments

Hello eveyone,

I'd like to know if there are plans to add the native JSON.stringifys replacer() functionality like:

JSON.stringify(input, (key, value) => value instanceof Set ? [...value] : value)

So it would also be possible to stringify objects which contains Set()s, Map()s, and so on.

Regards,
Michael

There aren't any plans to do so. That being said, I think it might be possible to add support for those natively to objects and arrays respectively.

Would you like to send a Pull Request to address this issue? Remember to add unit tests.

Hey, @mcollina,

I am a great fan of Fastify and now I am actively using it on my projects. Please assign this task to me. It will be my pleasure to be a part of such a great product and organisation.

I will also include unit tests. I like this issue and I really want to contribute to it.

@onbit-syn go for it!

@onbit-uchenik Is Anubhav still working on this issue? If not, I am more than happy to help! ๐Ÿ˜Œ

I dont think so. I you want to provide a PR, just do it.

@mcollina

I think it might be possible to add support for those natively to objects and arrays respectively.

Do you mean we do the following:

  • Set & WeakSet: treat them as an object
  • Map & WeakMap: treat them as an array

You can do Set and Map, not their weak counterparts.

Wanna confirm what we want. Is the type of Set in the schema set or object? The same question to Map too.

Imho Set is Array and Map is object

Indeed, yes.

I didn't expect this issue would be this complicated. I have closed my PR #545. Feel free to work on this issue if you are interested!

sooo, can we get the replacer? I have a use case where I would like to do a kind of monkey-patch;

const thing = {
   properties: [
       {
           id: 1
       },
       {
           id: 2
       }
   ],
   ref: 1
}

const str = JSON.stringify(thing, (key, value) => {
    if(key === "ref") return thing.properties.find(p => p.id === value);
    else return value;
})

After two years, this is unlikeky to happen. I won't be opposed to a PR, but there seem no interest in implementing this by its maintainers or users.