Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions src/Json/Decode/Extra.elm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Json.Decode.Extra exposing
, when
, collection, sequence, combine, indexedList, keys
, set
, dict2
, dict2, emptyObject
, withDefault, optionalField
, fromResult
, parseInt, parseFloat, doubleEncoded
Expand Down Expand Up @@ -40,7 +40,7 @@ module Json.Decode.Extra exposing

# Dict

@docs dict2
@docs dict2, emptyObject


# Maybe
Expand Down Expand Up @@ -165,6 +165,30 @@ decodeDictFromTuples keyDecoder tuples =
fail (errorToString error)


{-| Succeed only when given an empty JSON object (`{}`).

import Json.Decode exposing (..)

decodeString emptyObject "{}"
--> Ok ()

Anything else makes the decoder fail.

-}
emptyObject : Decoder ()
emptyObject =
keyValuePairs (succeed ())
|> andThen
(\pairs ->
case pairs of
[] ->
succeed ()

_ ->
fail "Expecting an empty object"
)


{-| Try running the given decoder; if that fails, then succeed with the given
fallback value.

Expand Down