Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
codeleep opened this issue May 5, 2023 · 4 comments

Comments

@codeleep
Copy link

codeleep commented May 5, 2023

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
Copy link
Owner

ljharb commented May 5, 2023

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
Copy link

mjoyce91 commented Jul 28, 2023

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
Copy link
Owner

ljharb commented Jul 28, 2023

hmm, that's a good counterpoint.

@cesarvarela
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants