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

dict2 fails when trying to decode string keys #22

Open
malaire opened this issue Nov 30, 2018 · 1 comment
Open

dict2 fails when trying to decode string keys #22

malaire opened this issue Nov 30, 2018 · 1 comment

Comments

@malaire
Copy link

malaire commented Nov 30, 2018

This example

import Json.Decode exposing (..)

 """ { "hello": "world", "foo": "bar" } """
    |> decodeString (dict2 string string)

gives following error:

Problem with the given value: "hello"
This is not valid JSON!
JSON.parse: unexpected character at line 1 column 1 of the JSON data <internals>
@lydell
Copy link

lydell commented Oct 20, 2019

dict2 tries to parse the keys as JSON here for some reason:

case decodeString keyDecoder strKey of

1 is valid JSON, hello is not.

Maybe the signature of dict2 should be changed to:

dict2 : (String -> Decoder comparable) -> Decoder v -> Decoder (Dict comparable v)

-- with this:
case decodeValue (keyDecoder strKey) (Json.Encode.string strKey) of

If so, the dict2 int string example would become dict2 (String.toInt >> fromMaybe) string, and your dict2 string string example would become dict2 succeed string.

If one wants the current behavior, that can be done with dict2 (always (doubleEncoded keyDecoder)) valueDecoder.

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

No branches or pull requests

2 participants