Json.Decode.Pipeline 可选问题
Json.Decode.Pipeline trouble with optional
我在解码 JSON 字符串中的可选字段时遇到一些问题。我正在尝试解码 "plannings" 并且计划可以有两种类型,正常计划或弹性计划。如果是普通计划,它将有一个planning_id
,如果是弹性计划,它将有一个flexplanning_id
。在我将存储计划的记录中,planningId
和 fiexplanningId
都是 Maybe Int
类型。
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 的数据。这是一个例子。这是我的应用程序发出的请求返回的字符串的一部分:
"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
}
],
然而,这是解码器的结果:
{ 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
}
],
如您所见,在JSON中有两个规划,一个是planning_id,另一个是flexplanning_id。然而,在解码器生成的记录中,第一个规划同时具有 planningId 和 flexplanningId,而第二个规划两者都没有。
您需要在解码器中翻转这两行以匹配它们定义的顺序:
|> optional "employee_timeslot_id" (nullable int) Nothing
|> optional "flexplanning_id" (nullable int) Nothing
它们按以下顺序定义:
, flexplanningId : Maybe Int
, employeeTimeslotId : Maybe Int
我在解码 JSON 字符串中的可选字段时遇到一些问题。我正在尝试解码 "plannings" 并且计划可以有两种类型,正常计划或弹性计划。如果是普通计划,它将有一个planning_id
,如果是弹性计划,它将有一个flexplanning_id
。在我将存储计划的记录中,planningId
和 fiexplanningId
都是 Maybe Int
类型。
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 的数据。这是一个例子。这是我的应用程序发出的请求返回的字符串的一部分:
"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
}
],
然而,这是解码器的结果:
{ 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
}
],
如您所见,在JSON中有两个规划,一个是planning_id,另一个是flexplanning_id。然而,在解码器生成的记录中,第一个规划同时具有 planningId 和 flexplanningId,而第二个规划两者都没有。
您需要在解码器中翻转这两行以匹配它们定义的顺序:
|> optional "employee_timeslot_id" (nullable int) Nothing
|> optional "flexplanning_id" (nullable int) Nothing
它们按以下顺序定义:
, flexplanningId : Maybe Int
, employeeTimeslotId : Maybe Int