json icon indicating copy to clipboard operation
json copied to clipboard

Decoder error even-though it's correct

Open empeje opened this issue 5 years ago • 0 comments

I have the following Type called Message with the following code

module Types.Message exposing (Message, decode, decodeList)

import Json.Decode as Decode exposing (Decoder, Error, list)
import Json.Decode.Pipeline exposing (required)


type alias Message =
    { username : String
    , content : String
    }


decode : Decoder Message
decode =
    Decode.succeed Message
        |> required "username" Decode.string
        |> required "content" Decode.string

decodeList : Decoder (List Message)
decodeList =
    list decode

I try to decode using something like this

decodeMessage : Decode.Value -> Result Error (List Message)
decodeMessage messageJson =
    Decode.decodeValue
        Message.decodeList
        messageJson

I found out that it works perfectly in REPL

> decodeString decode "{\"username\": \"mantap\", \"content\": \"lalui\"}"
Ok { content = "lalui", username = "mantap" }
    : Result Error Message

However, when I run the Webpack build using the elm-webpack-loader in Electron environment I found the following error

Problem with the value at json[0]: "{\"content\": \"Budi\", \"username\": \"UHU\"}" Expecting an OBJECT with a field named `content`

Any idea why?

empeje avatar Oct 19 '20 23:10 empeje