I'm trying to test a decoder - but I only ever get back the default values. Apologies for the wall of text, but when I've tried smaller examples, they always work, so I'm guessing there's a stupid error in here somewhere.
I've been trying to figure out why this won't work for quite a while now, with no luck. The JSON appears to be valid (I have tried parsing it in JS and in online validators).
I've tried different methods of decoding the JSON, again with no luck.
Any help at all is very much appreciated. If anything else should be added to the question, please let me know (I'm new to elm, if you can't tell).
I'm trying to decode JSON which looks like this:
{
"fade": 1,
"colour": "Campbells Red",
"stock": 1,
"site": "",
"url": "",
"plastic":"DX",
"name":"aviar",
"seenAt":1612884837886,
"weight":175,
"compositeIdentifier":"aviar||Innova||DX||Campbells Red||175",
"manufacturer":"Innova",
"expiresAt":1615476837886,
"glide":3,
"turn":0,
"speed":2,
"price":8.99
}
My type looks like this:
type alias DiscSighting =
{ fade: Int
, colour: String
, stock: Int
, site: String
, url: String
, plastic: String
, name: String
, weight: Int
, compositeIdentifier: String
, manufacturer: String
, glide: Int
, turn: Int
, speed: Int
, price: Float
}
And my decoder looks like this:
discDecoder: Decoder DiscSighting
discDecoder =
succeed DiscSighting
|> andMap (field "fade" (int) |> (withDefault) -1)
|> andMap (field "colour" (string) |> (withDefault) "")
|> andMap (field "stock" (int) |> (withDefault) -1)
|> andMap (field "site" (string) |> (withDefault) "")
|> andMap (field "url" (string) |> (withDefault) "")
|> andMap (field "plastic" (string) |> (withDefault) "")
|> andMap (field "name" (string) |> (withDefault) "")
|> andMap (field "weight" (int) |> (withDefault) -1)
|> andMap (field "compositeIdentifier" (string) |> (withDefault) "")
|> andMap (field "manufacturer" (string) |> (withDefault) "")
|> andMap (field "glide" (int) |> (withDefault) -1)
|> andMap (field "turn" (int) |> (withDefault) -1)
|> andMap (field "speed" (int) |> (withDefault) -1)
|> andMap (field "price" (float) |> (withDefault) -1)
The error I get is due to a test failing (it returns the error side of the result and thus fails the test):
Err (Failure "Expecting an OBJECT with a field named
price
)
In discDecoder
I'm not sure what the definitions are for andMap
and withDefault
, but the optional function in the package NoRedInk/elm-json-decode-pipeline will work instead of andMap
and withDefault
:
discDecoder : Decoder DiscSighting
discDecoder =
succeed DiscSighting
|> optional "fade" int -1
|> optional "colour" string ""
|> optional "stock" int -1
|> optional "site" string ""
|> optional "url" string ""
|> optional "plastic" string ""
|> optional "name" string ""
|> optional "weight" int -1
|> optional "compositeIdentifier" string ""
|> optional "manufacturer" string ""
|> optional "glide" int -1
|> optional "turn" int -1
|> optional "speed" int -1
|> optional "price" float -1
Full working example here: https://ellie-app.com/ckYm8HhMJfKa1