Generate a JSON Decoder (or Encoder) based on a type annotation.
- Make sure that you have imported
Json.Decode
- Put the cursor on a type annotation where the return type is a
Decoder
from elm/json (and the function body has not yet been written) - Press Option-Enter (macOS; Alt-Enter on Windows/Linux)
- Select Generate Decoder from the menu
- Make sure that you have imported
Json.Encode
- Put the cursor on a type annotation with a signature like
encodePerson : Person -> Json.Encode.Value
(and the function body has not yet been written) - Press Option-Enter (macOS; Alt-Enter on Windows/Linux)
- Select Generate Encoder from the menu
- You can import
Json.Encode
andJson.Decode
using whatever alias you prefer. The plugin will generate code using the same alias. - When decoding/encoding a type which is not part of
elm/core
, the plugin will search your project for exposed functions which know how to encode/decode that type and, if found, will use that function. - The generated decoders use Json.Decode.Pipeline to decode records.