Skip to content

Add emptyObject#23

Open
Janiczek wants to merge 2 commits intoelm-community:masterfrom
Janiczek:master
Open

Add emptyObject#23
Janiczek wants to merge 2 commits intoelm-community:masterfrom
Janiczek:master

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