我正在编写一个 elm json 解码器,并希望将一个值从“父”记录移动到“子”记录。
在此示例中,我想将beta
键/值移动到Bar
类型中。
我传入的 JSON
{ "alpha": 1,
"beta: 2,
"bar": {
"gamma": 3
}
}
我的类型
type alias Foo =
{ alpha : Int
, bar : Bar
}
type alias Bar =
{ beta : Int
, gamma : Int
}
我怎样才能在解码器中做到这一点?我觉得我想将解码器传递beta
给fooDecode
. 但这显然是不对的……
fooDecode =
decode Foo
|> required "alpha" Json.Decode.int
|> required "bar" barDecode (Json.Decode.at "beta" Json.Decode.int)
barDecode betaDecoder =
decode Bar
|> betaDecoder
|> required "gamma" Json.Decode.int
注意:我的实际用例有一个子列表,但希望我能用指针解决这个问题。我正在使用 Decode.Pipeline 因为它是一个大型 JSON 对象