fast-json-stringify icon indicating copy to clipboard operation
fast-json-stringify copied to clipboard

Any plans to add support replacer functionality?

Open Mika83AC opened this issue 3 years ago • 11 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

Mika83AC avatar Mar 12 '21 07:03 Mika83AC

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.

mcollina avatar Mar 12 '21 09:03 mcollina

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

mcollina avatar Mar 12 '21 09:03 mcollina

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-uchenik avatar May 25 '21 19:05 onbit-uchenik

@onbit-syn go for it!

mcollina avatar May 25 '21 20:05 mcollina

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

TommyDew42 avatar Sep 05 '22 20:09 TommyDew42

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

Uzlopak avatar Sep 05 '22 22:09 Uzlopak

@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

TommyDew42 avatar Sep 06 '22 13:09 TommyDew42

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

mcollina avatar Sep 06 '22 16:09 mcollina

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

TommyDew42 avatar Sep 12 '22 16:09 TommyDew42

Imho Set is Array and Map is object

Uzlopak avatar Sep 12 '22 16:09 Uzlopak

Indeed, yes.

mcollina avatar Sep 13 '22 14:09 mcollina

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!

TommyDew42 avatar Oct 07 '22 15:10 TommyDew42

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;
})

captainrdubb avatar Dec 28 '22 18:12 captainrdubb

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.

mcollina avatar Dec 28 '22 18:12 mcollina