rule : Review.Rule.Rule
Generates Elm code from a JSON sample in JsonString
config =
[ JsonToElm.rule
]
The fix runs on top-level values with a JsonString type annotation. It generates JSON decoders and encoders as well as the necessary types and type aliases.
import Json.Decode
import Json.Encode
sample : JsonString
sample =
"""@json{"a": 1}"""
import Json.Decode
import Json.Encode
type alias Sample =
{ a : Int
}
sampleDecoder : Json.Decode.Decoder Sample
sampleDecoder =
Json.Decode.map Sample
(Json.Decode.field "a" Json.Decode.int)
encodedSample : Sample -> Json.Encode.Value
encodedSample sample =
Json.Encode.object
[ ( "a", Json.Encode.int sample.a )
]
Note that the imports in the module are examined to determine the style of decoders.
If a Json.Decode.Extra
import is present, then applicative-style decoders
using Json.Decode.Extra.andMap
will be generated.
If a Json.Decode.Pipeline
import is present, then pipeline decoders are
generated.
You can try this rule out by running the following command:
elm-review --template alexkorban/elm-review-json-to-elm/example --rules JsonToElm