mlms13 / bs-decode

Type-safe JSON decoding for ReasonML and OCaml
https://mlms13.github.io/bs-decode/docs/what-and-why
MIT License
103 stars 18 forks source link

Add `unit` decoder #47

Closed mlms13 closed 4 years ago

mlms13 commented 5 years ago

In some cases, it may be useful to have a decoder that takes in JSON and always returns a successful (). For example:

mlms13 commented 4 years ago

Just a quick note, you can already do this pretty easily with pure(), which creates a decoder that will always succeed with (). I'm less convinced now that this case is so important that we need to provide a special function to do this.

Alternatively, there was a suggestion in Discord awhile ago to add a "decoder" that always succeeds with the JSON input it was given. This can already be achieved (e.g. with Result.pure or Option.pure), but it locks you in to that specific monad, meaning you'd have to find and replace that code if you wanted to switch from one to the other.

So I'm inclined to add a helper for this, but naming becomes difficult because json is likely to cause issues with shadowing, and id has a slightly different meaning (and might also shadow things). Maybe okJson or just ok?

mlms13 commented 4 years ago

Opted for okJson. This will be included in the next release.