Skip to content
Generate JSON encoders/decoders and TypeScript definitions from Elm types.
Elm JavaScript Shell
Branch: master
Clone or download

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
bin put generated files into src/Interop Feb 22, 2020
dist
src handle maybe record fields Mar 28, 2020
README.md
elm.json
package-lock.json
package.json
tsconfig.json

README.md

Elm JSON Interop

Usage

npm i pravdomil/elm-json-interop -g
elm-json-interop src/Main.elm

# Generates:
# src/Interop/MainEncode.elm
# src/Interop/MainDecode.elm
# src/Interop/Main.ts

Example

Input file

module Main exposing (..)


type Msg
    = PressedEnter
    | ChangedDraft String
    | ReceivedMessage { user : User, message : String }
    | ClickedExit


type User
    = Regular String Int
    | Visitor String
    | Anonymous

Generated TypeScript file

export type Maybe<a> = a | null

export type Msg =
  | { PressedEnter: [] }
  | { ChangedDraft: string }
  | { ReceivedMessage: { user: User, message: string } }
  | { ClickedExit: [] }

export const isPressedEnter = (a: Msg): a is { PressedEnter: [] } => "PressedEnter" in a
export const isChangedDraft = (a: Msg): a is { ChangedDraft: string } => "ChangedDraft" in a
export const isReceivedMessage = (a: Msg): a is { ReceivedMessage: { user: User, message: string } } => "ReceivedMessage" in a
export const isClickedExit = (a: Msg): a is { ClickedExit: [] } => "ClickedExit" in a

export type User =
  | { Regular: [string, number] }
  | { Visitor: string }
  | { Anonymous: [] }

export const isRegular = (a: User): a is { Regular: [string, number] } => "Regular" in a
export const isVisitor = (a: User): a is { Visitor: string } => "Visitor" in a
export const isAnonymous = (a: User): a is { Anonymous: [] } => "Anonymous" in a

Generated Encoder

module Main.Encode exposing (..)

import Main exposing (..)
import Json.Encode exposing (..)

encodeMaybe a b = case b of
   Just c -> a c
   Nothing -> null

encodeDict _ b c = dict identity b c

encodeMsg : Msg -> Value
encodeMsg a =
  case a of
    PressedEnter -> object [ ( "PressedEnter", list identity [] ) ]
    ChangedDraft b -> object [ ( "ChangedDraft", string b ) ]
    ReceivedMessage b -> object [ ( "ReceivedMessage", object [ ( "user", encodeUser b.user ), ( "message", string b.message ) ] ) ]
    ClickedExit -> object [ ( "ClickedExit", list identity [] ) ]

encodeUser : User -> Value
encodeUser a =
  case a of
    Regular b c -> object [ ( "Regular", list identity [ string b, int c ] ) ]
    Visitor b -> object [ ( "Visitor", string b ) ]
    Anonymous -> object [ ( "Anonymous", list identity [] ) ]

Generated Decoder

module Main.Decode exposing (..)

import Json.Decode exposing (..)
import Main exposing (..)
import Set


setDecoder a =
    map Set.fromList (list a)


dictDecoder _ a =
    dict a


msgDecoder : Decoder Msg
msgDecoder =
    oneOf
        [ field "PressedEnter" (succeed PressedEnter)
        , field "ChangedDraft" (map ChangedDraft string)
        , field "ReceivedMessage" (map ReceivedMessage (map2 (\a b -> { user = a, message = b }) (field "user" userDecoder) (field "message" string)))
        , field "ClickedExit" (succeed ClickedExit)
        ]


userDecoder : Decoder User
userDecoder =
    oneOf
        [ field "Regular" (map2 Regular (index 0 string) (index 1 int))
        , field "Visitor" (map Visitor string)
        , field "Anonymous" (succeed Anonymous)
        ]
You can’t perform that action at this time.