3 ms·
json.loads('foo.json') (what is that? python?) requires that the json matches your types exactelly. This, at least for me, is rarely the case. And yes, it is.
by Skinney 9y ago
json.loads('foo.json') (what is that? python?) requires that the json matches your types exactelly. This, at least for me, is rarely the case.
And yes, it is. Here is code from my own project:
Json.map3 HintTemplate
(Json.field "ID" Json.int)
(Json.field "Text" Json.string)
(Json.field "Image" Json.string)
Where HintTemplate is a record type containing an integer id, a string text and a image url. What about this is difficult?
- Tarean 9y agoThat's reasonably similar to handwritten haskell json parsing: instance FromJson HintTemplate where parseJson = withObject $ \o -> HintTemplate <$> o .: "ID" <*> o .: "Text" <*> o .: "Image" I wrote <$> and <> out but there is a definition like map3: liftA3 f a b = f <$> a <*> b This works for all applicatives, though. Is it normal for elm libraries to copy paste code like the map3 definition?
- Skinney 9y agoNot sure what you mean. It's normal that different packages have their own version of a given function, like List.map3, Json.map3 etc. But the implementation is different, so it's not copy-paste. It has to be this way in cases where generic types is not enough and something like interfaces/typeclasses is required.