module Messages exposing(..) import Json.Decode import Json.Encode exposing (Value) -- The following module comes from bartavelle/json-helpers import Json.Helpers exposing (..) import Dict exposing (Dict) import Set exposing (Set) type ServerMessage = Welcome Int | Bye jsonDecServerMessage : Json.Decode.Decoder ( ServerMessage ) jsonDecServerMessage = let jsonDecDictServerMessage = Dict.fromList [ ("Welcome", Json.Decode.lazy (\_ -> Json.Decode.map Welcome (Json.Decode.int))) , ("Bye", Json.Decode.lazy (\_ -> Json.Decode.succeed Bye)) ] in decodeSumObjectWithSingleField "ServerMessage" jsonDecDictServerMessage jsonEncServerMessage : ServerMessage -> Value jsonEncServerMessage val = let keyval v = case v of Welcome v1 -> ("Welcome", encodeValue (Json.Encode.int v1)) Bye -> ("Bye", encodeValue (Json.Encode.list identity [])) in encodeSumObjectWithSingleField keyval val