Elm Exactly
Results / elm-json /

JsonDecoderSelfRecursiveNoLazyTest

elm-0.19.1 passelm-0.19.2 passelm-0.19.3 passlamdera-1.5.0 pass
A `Json.Decode` decoder that refers to itself without `Json.Decode.lazy` decodes a nested value correctly.

`tDecoder` reads a `"tag"` field and, through `andThen`, decodes `"kids"` with
`Json.Decode.list tDecoder` only for the tag `"node"`. Because the self-reference sits inside
the `andThen` callback, no `lazy` is needed. The input is a node holding one leaf.

Results

elm-0.19.1

passran in 67 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets.
decoded: Ok (TNode [TLeaf])

elm-0.19.2

passran in 72 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets.
decoded: Ok (TNode [TLeaf])

elm-0.19.3

passran in 69 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets.
decoded: Ok (TNode [TLeaf])

lamdera-1.5.0

passran in 53 ms
Output
decoded: Ok (TNode [TLeaf])

Discussions

Discuss this test·Search the discussions on GitHub

Discussions are on GitHub; one about this test has [t:elm-json/JsonDecoderSelfRecursiveNoLazyTest] in its title.

Source

The -- CHECK: lines give the expected output.

module JsonDecoderSelfRecursiveNoLazyTest exposing (main)
{-| A `Json.Decode` decoder that refers to itself without `Json.Decode.lazy` decodes a nested value correctly.
`tDecoder` reads a `"tag"` field and, through `andThen`, decodes `"kids"` with
`Json.Decode.list tDecoder` only for the tag `"node"`. Because the self-reference sits inside
the `andThen` callback, no `lazy` is needed. The input is a node holding one leaf.
-}
-- CHECK: decoded: Ok (TNode [TLeaf])
import Html exposing (text)
import Json.Decode as D
type T
= TLeaf
| TNode (List T)
tDecoder : D.Decoder T
tDecoder =
D.field "tag" D.string
|> D.andThen
(\tag ->
case tag of
"leaf" ->
D.succeed TLeaf
"node" ->
D.map TNode (D.field "kids" (D.list tDecoder))
_ ->
D.fail "unknown tag"
)
main =
let
json =
"""{"tag": "node", "kids": [{"tag": "leaf"}]}"""
result =
D.decodeString tDecoder json
_ =
Debug.log "decoded" result
in
text "done"