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

boolean value is stringified as string #214

Closed
sht5 opened this issue Jun 13, 2017 · 7 comments
Closed

boolean value is stringified as string #214

sht5 opened this issue Jun 13, 2017 · 7 comments

Comments

@sht5
Copy link

sht5 commented Jun 13, 2017

if you stringify an object with a boolean key\value and then parse it the boolean is parsed as a string

const qs = require('qs'); const x = {a:2,b:false}; const str = qs.stringify(x); const xAfter = qs.parse(str); console.log(JSON.stringify(xAfter)); //{a:2,b:"false"}

@ljharb
Copy link
Owner

ljharb commented Jun 13, 2017

Everything that goes over the wire is a string; I'm not sure what else it would be?

@sht5
Copy link
Author

sht5 commented Jun 13, 2017 via email

@ljharb
Copy link
Owner

ljharb commented Jun 13, 2017

Unfortunately, that's absolutely not a valid expectation when dealing with query strings - it's not even a valid expectation when dealing with JSON.

@carpben
Copy link

carpben commented Aug 9, 2018

The default should be to return true as a string. But it would be nice if you can opt in to parse a true/false strings as booleans.

@ljharb
Copy link
Owner

ljharb commented Aug 9, 2018

@carpben you should be able to do that by providing a custom encoder/decoder function.

@carpben
Copy link

carpben commented Aug 12, 2018

I tried this:

qs.parse(history.location.search, { 
   ignoreQueryPrefix: true ,
   decoder: str => (str==="false")? false : (str==="true")? true : str
 })

But this led to a bug, changing the keys for the returned object. For example, when logging the parsed object:
{activities%5B0%5D: "16"}

@ljharb
Copy link
Owner

ljharb commented Aug 12, 2018

You do actually have to still decode things. The decoder gets additional arguments that may help.

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

3 participants