ljharb / qs

A querystring parser with nesting support
BSD 3-Clause "New" or "Revised" License
8.47k stars 731 forks source link

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

Open codeleep opened 1 year ago

codeleep commented 1 year ago

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.

ljharb commented 1 year ago

I agree this is a bug.

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

mjoyce91 commented 1 year ago

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.

ljharb commented 1 year ago

hmm, that's a good counterpoint.

cesarvarela commented 11 months ago

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