Elm Language
कस्टम JSON डिकोडर
खोज…
परिचय
कस्टम डिकोडर बनाने के लिए Json.Decode का उपयोग कैसे करें, उदाहरण के लिए यूनियन प्रकार और उपयोगकर्ता परिभाषित डेटा प्रकारों में डिकोडिंग
यूनियन प्रकार में डिकोडिंग
import Json.Decode as JD
import Json.Decode.Pipeline as JP
type PostType = Image | Video
type alias Post = {
id: Int
, postType: PostType
}
-- assuming server will send int value of 0 for Image or 1 for Video
decodePostType: JD.Decoder PostType
decodePostType =
JD.int |> JD.andThen (\postTypeInt ->
case postTypeInt of
0 ->
JD.succeed Image
1 ->
JD.succed Video
_ ->
JD.fail "invalid posttype"
)
decodePostMap : JD.Decoder Post
decodePostMap =
JD.map2 Post
(JD.field "id" JD.int)
(JD.field "postType" decodePostType)
decodePostPipline : JD.Decoder Post
decodePostPipline =
JP.decode Post
|> JP.required "id" JD.int
|> JP.required "postType" decodePostType
Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow