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

Add emptyObject #23

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Add emptyObject #23

wants to merge 2 commits into from

Conversation

Janiczek
Copy link

@Janiczek Janiczek commented Feb 6, 2019

In my job, we've just encountered a situation where we need to decode an empty JSON object. There is a trick for it but not many people know it. So, let's give it a name and make it more discoverable?

EDIT: I tried to create a doctest for the Err case (ie. decodeString emptyObject """{"foo": "bar"}""" --> Err (Failure "..." (Json.Encode.string "...")) but there seems to be some issue with Encode.Value equality - couldn't make that work.

@zwilias
Copy link
Member

zwilias commented Feb 6, 2019

Alternatively, when (keyValuePairs (succeed ())) List.isEmpty (succeed ()), though the error message is less informative.

I'm going to mull this over a little bit - I agree that it's not a super obvious trick, but I also wonder whether it's a common enough use-case ("the object must be empty") that it should be a utility here.

As a final alternative, this could also be done using https://package.elm-lang.org/packages/zwilias/json-decode-exploration/latest/Json-Decode-Exploration#isObject and strict decoding - if the object had any keys, those would result in errors. (that whole package is built around the idea that unused data in the JSON should not be ignored)

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

Successfully merging this pull request may close these issues.

2 participants