Experimental JSON decoding API

I was curious how this implementation compares to the old pipeline implementation in terms of error messages, so I put together this ellie app (https://ellie-app.com/3yvjBn8YMVQa1) to compare the same mistake made in both APIs. To test this, I made the equivalent decoder with the new and old apis, made sure they worked, and then in both I deliberately tried to decode the email using Decode.int.

Here are the error messages side by side:


Experimental API

I cannot send this through the (<|) pipe:

34|>    require "email" int <| \email ->
35|>    default "name" string "Guest" <| \name ->
36|>    succeed (User { id = id, email = email, name = name, selected = False })

The argument is:

    String -> Decoder User

But (<|) is piping it a function that expects:

    Int -> Decoder b

Hint: Want to convert a String into an Int? Use the String.toInt function!

Old API

This function cannot handle the argument sent through the (|>) pipe:

34|     succeed UserModel
35|         |> JDP.required "id" int
36|         |> JDP.required "email" int
               ^^^^^^^^^^^^^^^^^^^^^^^^
The argument is:

    Decoder (Int -> b)

But (|>) is piping it a function that expects:

    Decoder (String -> String -> Bool -> UserModel)

Not that different I suppose, but it looks like the old API has the advantage of pointing to the line where the mistake was made. I bet the error message of this new API could be improved if the code didnt have lambdas, but Im not sure how it could be written with comparable clarity.

Are there other common errors to test?

3 Likes