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

Stringify null values #462

Open
tada5hi opened this issue Jan 15, 2023 · 9 comments
Open

Stringify null values #462

tada5hi opened this issue Jan 15, 2023 · 9 comments

Comments

@tada5hi
Copy link

tada5hi commented Jan 15, 2023

It would be really great, if null values could be stringified via an option as a null string instead of an empty string. If that is fine for you @ljharb i would create a pull request for that.

Greetings
Peter

@ljharb
Copy link
Owner

ljharb commented Jan 15, 2023

You can already do that with the encoder option.

@tada5hi
Copy link
Author

tada5hi commented Jan 16, 2023

good point, but i find is not very intuitive to use and feels like a bit of a overhead.

@ljharb
Copy link
Owner

ljharb commented Jan 16, 2023

Given that it's a pretty uncommon use case (you're the first to ask afaik) I don't think it's worth the complexity of an additional option. Can you elaborate on your use case?

@tada5hi
Copy link
Author

tada5hi commented Jan 18, 2023

i use it for a library (rapiq) which builds & parses an extended JSON-API Query String format and i plan to do a bigger refactoring & cleanup in the near feature and also want to reach v1.0.0.
Therefore, the following should be possible:
/?filter[parameter]=null and ?filter[parameter]=id1,id2,null

@ljharb
Copy link
Owner

ljharb commented Jan 18, 2023

Wouldn't there be a lot more things you'd need to handle to make query strings compatible with JSONAPI, such that you'd need the encoder/decoder anyways?

@tada5hi
Copy link
Author

tada5hi commented Jan 21, 2023

properly, yes.

@ljharb
Copy link
Owner

ljharb commented Jan 21, 2023

then what would be the benefit of the option, when it’s trivial to handle it in a decoder?

@teobmg
Copy link

teobmg commented Aug 1, 2023

@ljharb can you please provide an example?
I'm trying to stringify in this way:

stringify(
          {
            division: ['', null],
          },
          {
            arrayFormat: 'brackets',
            strictNullHandling: true
          },
        )

And parse in this way

qs.parse(query, {
      decoder: (string, defaultDecoder) =>
        string === 'null' ? null : defaultDecoder(string),
      strictNullHandling: true,
    });

But debugging the decoder, the string that I receive as value is always '', so I'm not able to distinguish between a real empty string and a null value.

@ljharb
Copy link
Owner

ljharb commented Aug 4, 2023

@teobmg stringify gets a null, so the encoder would be what handles that - sorry for misspeaking. by the time it's parsed, everything's a string and there's no null.

In other words, you have to encode null specially, and then decode that specially.

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