qs icon indicating copy to clipboard operation
qs copied to clipboard

qs.stringify(json) and qs.parse(json) Results are inconsistent with expectations

Open codeleep opened this issue 1 year ago • 4 comments

When my json is like this

[
    {
        "fansUid": "",
        "masterShopId": 0,
        "items": [
            {
                "itemId": "",
                "skus": [
                    {
                        "price": 0,
                        "skuId": 0,
                        "promotionToolCode": ""
                    }
                ]
            }
        ],
        "scene": ""
    }
]

i use qs.stringify(json) and qs.parse(json) . but result is

[
    {
        "fansUid": "",
        "masterShopId": "0",
        "items": [
            {
                "itemId": "",
                "skus": [
                    {
                        "[price]": "0",
                        "[skuId]": "0",
                        "[promotionToolCode]": ""
                    }
                ]
            }
        ],
        "scene": ""
    }
]

It was obvious that this was a mistake. How should I solve it

I tried to change multiple versions as well, but the latest version still did not fix the problem.

codeleep avatar May 05 '23 03:05 codeleep

I agree this is a bug.

In general, I'd pass options to both stringify and parse, but the defaults should indeed round-trip.

ljharb avatar May 05 '23 04:05 ljharb

I don't know if this would be considered a bug. There simply isn't a way to know what the original type a property was after it has been stringify'd. It's not guaranteed that all users will want numbers converted back to an integer type.

You could create some type of mapping to run your parsed response through which ensures the data types are converted to your liking.

mjoyce91 avatar Jul 28 '23 15:07 mjoyce91

hmm, that's a good counterpoint.

ljharb avatar Jul 28 '23 17:07 ljharb

The property names have brackets around them too: [price] vs price.

cesarvarela avatar Sep 07 '23 19:09 cesarvarela