just by replacing the record constructor with the pair construction function
:
type alias Properties a =
{ a
| x : { v : Float, pos : Float }
, y : { v : Float, pos : Float }
, size : ( Float, Float )
, el : Float
}
type alias Pr =
{ v : Float, pos : Float }
type Block
= Block BlockType BlockProp
type alias BlockProp =
Properties { visibility : Int, tb : ( Float, Float ) }
type BlockType
= Rock
| Ground
Similarly, to get the block type you need to decode
{
"blocks": [{
"type": "ground",
"x": {
"v": "0.0",
"pos": "30"
},
"y": {
"v": "0.0",
"pos": "100.0"
},
"sizex": "50.0",
"sizey": "70.0",
"el": "1.0",
"visibility": "1",
"t": "20.0",
"b": "30.0"
},
{
"type": "rock",
"x": {
"v": "0.0",
"pos": "30"
},
"y": {
"v": "0.0",
"pos": "100.0"
},
"sizex": "50.0",
"sizey": "70.0",
"el": "1.0",
"visibility": "1",
"t": "20.0",
"b": "30.0"
}
]
}
as a type Block
, then map the result of that using a function that converts
s to
blockDecoder : Decoder BlockProp
blockDecoder =
map6 BlockProp
(field "x"
(map2 Pr
(field "v" float)
(field "pos" float)
)
)
(field "y"
(map2 Pr
(field "v" float)
(field "pos" float)
)
)
(field "size" -- I DON'T KNOW HOW TO CONVERT sizex AND sizey TO TUPLE
(field "el" float)
(field "visibility" float)
(field "tb" -- I DON'T KNOW HOW TO CONVERT t AND b TO TUPLE
s:
而我需要对保存在JSON中的块进行解码。Pr
有谁能帮我解决如何构建一个解码器 把保存在JSON中的区块转换为: Tuple.pair
? :)
map2 Tuple.pair
(field "sizex" float)
(field "sizey" float)
edit:我试过这样的东西。type
我在将字段转换为元组时遇到了问题... ... 同时我也不知道如何根据类型字段创建一个具有正确BlockType的Block。String
String
BlockType
我目前正在学习Elm,我被卡在了JSON的解码上。我有一个代表块的结构:类型别名 属性a = { a