Elm Exactly
Results / elm-json /

JsonDecoderSelfRecursiveTest

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 through `Json.Decode.lazy` decodes a nested value correctly.

`treeDecoder` decodes the `"kids"` field as a list of trees, using `lazy` to defer the
reference to itself. The input `{"kids": [{"kids": []}]}` is a node holding one empty node.

Results

elm-0.19.1

passran in 72 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 (Node [Node []])

elm-0.19.2

passran in 53 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 (Node [Node []])

elm-0.19.3

passran in 65 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 (Node [Node []])

lamdera-1.5.0

passran in 78 ms
Output
decoded: Ok (Node [Node []])

Discussions

Discuss this test·Search the discussions on GitHub

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

Source

The -- CHECK: lines give the expected output.

module JsonDecoderSelfRecursiveTest exposing (main)
{-| A `Json.Decode` decoder that refers to itself through `Json.Decode.lazy` decodes a nested value correctly.
`treeDecoder` decodes the `"kids"` field as a list of trees, using `lazy` to defer the
reference to itself. The input `{"kids": [{"kids": []}]}` is a node holding one empty node.
-}
-- CHECK: decoded: Ok (Node [Node []])
import Html exposing (text)
import Json.Decode as D
type Tree
= Node (List Tree)
treeDecoder : D.Decoder Tree
treeDecoder =
D.map Node
(D.field "kids"
(D.list (D.lazy (\_ -> treeDecoder)))
)
main =
let
json =
"""{"kids": [{"kids": []}]}"""
result =
D.decodeString treeDecoder json
_ =
Debug.log "decoded" result
in
text "done"