josdejong / lossless-json

Parse JSON without risk of losing numeric information
MIT License
422 stars 28 forks source link

fix: backslash is not transform when stringify object key #239

Closed mengfanliao closed 3 years ago

mengfanliao commented 3 years ago

first, thank you to open source, and there is a bug, i found and want to fix it.

like code blow, i execute stringify api and JSON.stringify, and the arguments is the same, but the result is not the same. the result of stringify lose four backslash。

so, i modified the code in stringify.js and add unit test to prove it works.

stringify({'\\\\d': 1})
// result "{\"\\\\d\":1}"

JSON.stringify({'\\\\d': 1})
// result "{\"\\\\\\\\d\":1}"
mengfanliao commented 3 years ago

@josdejong

josdejong commented 3 years ago

Thanks a lot @mengfanliao , good find and fix. I'll review your fix soon but I'm a bit short on time this week.

josdejong commented 3 years ago

I've had another look at your PR @mengfanliao. It works correctly indeed 👍. I was thinking about whether it makes sense to call stringifyValue to stringify the keys: this function is meant for values and contains checking types like string, array, object, etc. I think we can make the code a little bit simpler and more performant by calling JSON.stringify(key) instead of stringifyValue(...). What do you think?

mengfanliao commented 3 years ago

@josdejong i think you are right,i have modified the code and call JSON.stringify rather than stringifyValue

josdejong commented 3 years ago

Thanks for the update @mengfanliao , looks good 👍

josdejong commented 3 years ago

Fix is published now in v1.0.5. Thanks again @mengfanliao!