JSON文字列のオプションフィールドをデコードする際に問題が発生しています。私は "プランニング"をデコードしようとしており、計画は通常のプランニングかフレックスプランニングの2種類があります。通常の計画の場合はplanning_id
、フレックス計画の場合はflexplanning_id
となります。プランニングを保存するレコードでは、planningId
とfiexplanningId
はMaybe Int
です。Json.Decode.Pipelineでオプションの問題が発生する
type alias Planning =
{ time : String
, planningId : Maybe Int
, groupId : Int
, groupName : String
, flex : Bool
, flexplanningId : Maybe Int
, employeeTimeslotId : Maybe Int
, employeeId : Int
}
そして、ここで私が使用するデコーダである。
planningDecoder : Decoder Planning
planningDecoder =
decode Planning
|> required "time" string
|> optional "planning_id" (nullable int) Nothing
|> required "group_id" int
|> required "group_name" string
|> required "flex" bool
|> optional "employee_timeslot_id" (nullable int) Nothing
|> optional "flexplanning_id" (nullable int) Nothing
|> required "employee_id" int
しかし、デコーダが正確にJSONからのデータを復号化し、記憶されていません。ここに例があります。 、JSONで
{ monday = [
{ time = "07:00 - 17:00"
, planningId = Just 6705
, groupId = 120
, groupName = "De rode stip"
, flex = False, flexplanningId = Just 1302
, employeeTimeslotId = Nothing
, employeeId = 120120 }
,{ time = "07:00 - 17:00"
, planningId = Nothing
, groupId = 5347
, groupName = "vakantie groep"
, flex = True
, flexplanningId = Nothing
, employeeTimeslotId = Just 195948
, employeeId = 120120
}
],
あなたが見ることができるように、:
"monday": [
{
"time": "07:00 - 17:00",
"planning_id": 6705,
"group_name": "De rode stip",
"group_id": 120,
"flex": false,
"employee_timeslot_id": 1302,
"employee_id": 120120
},
{
"time": "07:00 - 17:00",
"group_name": "vakantie groep",
"group_id": 5347,
"flexplanning_id": 195948,
"flex": true,
"employee_id": 120120
}
],
これは、しかし、デコーダの結果である:これは私のアプリケーションからの要求によって返される文字列の一枚ですplanning_idとflexplanning_idの2つのプランニングがあります。しかし、デコーダによって生成されたレコードでは、最初の計画はplanningIdとflexplanningIdの両方を持ちますが、2番目の計画ではどちらもありません。
ああ、愚かな間違いを!それをキャッチするためにありがとう! –