elm - Json.Decode.Pipeline trouble with optional -


i'm having trouble decoding optional fields json string. trying decode "plannings" , planning can of 2 types, normal planning, or flex planning. if normal planning, have planning_id, if flex planning, have flexplanning_id. in record in store plannings, both planningid , fiexplanningid of type maybe int.

type alias planning =     { time : string     , planningid : maybe int     , groupid : int     , groupname : string     , flex : bool     , flexplanningid : maybe int     , employeetimeslotid : maybe int     , employeeid : int     } 

and here decoder use:

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 

however, decoder isn't accurately decoding , storing data json. here example. 1 piece of string returned request made application:

"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 } ], 

this, however, result of decoder:

{ monday = [   { time = "07:00 - 17:00"   , planningid = 6705   , groupid = 120   , groupname = "de rode stip"   , flex = false, flexplanningid = 1302   , employeetimeslotid = nothing   , employeeid = 120120 }  ,{ time = "07:00 - 17:00"   , planningid = nothing   , groupid = 5347   , groupname = "vakantie groep"   , flex = true   , flexplanningid = nothing   , employeetimeslotid = 195948   , employeeid = 120120    }  ], 

as can see, in json, there 2 plannings, 1 planning_id , other flexplanning_id. however, in record produced decoder, first planning has both planningid , flexplanningid, whereas second has neither.

you need flip these 2 lines in decoder match order in defined:

|> optional "employee_timeslot_id" (nullable int) nothing |> optional "flexplanning_id" (nullable int) nothing 

they defined in order:

, flexplanningid : maybe int , employeetimeslotid : maybe int 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -