Skip to content

Commit

Permalink
Utilities to make the JSON mapping easier. finos#25
Browse files Browse the repository at this point in the history
  • Loading branch information
AttilaMihaly committed Mar 23, 2020
1 parent bc11981 commit 05ef121
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/Morphir/JsonExtra.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module Morphir.JsonExtra exposing (..)

import Json.Decode as Decode
import Json.Encode as Encode


encodeConstructor : String -> List Encode.Value -> Encode.Value
encodeConstructor typeTag args =
Encode.string typeTag
:: args
|> Encode.list identity


decodeCustomType : (String -> Decode.Decoder a) -> Decode.Decoder a
decodeCustomType f =
Decode.index 0 Decode.string
|> Decode.andThen f


decodeConstructor : (a -> r) -> Decode.Decoder a -> Decode.Decoder r
decodeConstructor f a =
Decode.map f
(Decode.index 1 a)


decodeConstructor2 : (a -> b -> r) -> Decode.Decoder a -> Decode.Decoder b -> Decode.Decoder r
decodeConstructor2 f a b =
Decode.map2 f
(Decode.index 1 a)
(Decode.index 2 b)


decodeConstructor3 : (a -> b -> c -> r) -> Decode.Decoder a -> Decode.Decoder b -> Decode.Decoder c -> Decode.Decoder r
decodeConstructor3 f a b c =
Decode.map3 f
(Decode.index 1 a)
(Decode.index 2 b)
(Decode.index 3 c)


decodeConstructor4 : (a -> b -> c -> d -> r) -> Decode.Decoder a -> Decode.Decoder b -> Decode.Decoder c -> Decode.Decoder d -> Decode.Decoder r
decodeConstructor4 f a b c d =
Decode.map4 f
(Decode.index 1 a)
(Decode.index 2 b)
(Decode.index 3 c)
(Decode.index 4 d)

0 comments on commit 05ef121

Please sign in to comment.