5 ms·
> making very heavy use of Ajax calls to a JSON-based RESTful API and then > Want to decode some JSON? Hard, especially if the JSON is heavily nested and it m
by dmitriid 9y ago
> making very heavy use of Ajax calls to a JSON-based RESTful API
and then
> Want to decode some JSON? Hard, especially if the JSON is heavily nested and it must be decoded to custom types defined in your application. Doing this will require an understanding of how JSON decoding works in Elm and will usually result in quite a bit of code (our application contains over 900 lines of JSON decoders alone).
What a great pragmatic language
- amimetic 9y agoIn exchange you get incredible validation of your JSON and precise error messages about missing fields etc. And you don't have to write that bit by hand: http://eeue56.github.io/json-to-elm/ http://eeue56.github.io/json-to-elm/ will generate the decoder for you for straightforward cases and give you a great starting point for more complex cases. You also get proper ADTs to manage state: not asked/loading/loaded/error something that is tedious and error prone in Javascript.
- dmitriid 9y agoYup. There are a bazillion libraries now trying to pretend to make it look like it's easy to deal with JSON in Elm. An online converter is definitely not something I would advertise for a "production-ready highly productive amazing pragmatic language to be used in a serious company"
- dmitriid 9y agoFor god's sake, Elm is the only language I know that has _an entire book written on how to handle JSON_: https://www.brianthicks.com/post/2017/01/06/announcing-the-json-survival-kit/ https://www.brianthicks.com/post/2017/01/06/announcing-the-j... It's introduction is: "You know how it takes so much effort to produce even the simplest of programs when JSON parsing is involved? Wouldn’t it be nice if you could breeze right on by that step and get on with writing your business logic? This is what you’ll get with The JSON Survival Kit, a short ebook on JSON decoding in Elm." Wut
- Skinney 9y agoAnd Go has an entire book on how to use the database/sql package. Does that mean the database/sql package is hard? Is React difficult because there is online courses? Is Bootstrap? Of course not.
- enalicho 9y agoIt is no different from using a code generator for anything. Json2elm is designed to _help you_ write decoders, not replace decoders. Once you know how to write them, json2elm is mostly useful for generating boilerplate.
- dmitriid 9y agoIf you need a code generator to generate boilerplate... something's wrong with your language
- ch4s3 9y agoI suppose you start every new project with mkdir and a few touch commands? In all seriousness, ever language has boilerplate. In js it might be error handling and state management. In Elm, its decoding json.
- dmitriid 9y ago> I suppose you start every new project with mkdir and a few touch commands? This is not a valid analogy (and no analogy is ever valid). > In js it might be error handling and state management. In Elm, its decoding json. Yeah, and Go has trouble with generics. It doesn't mean it's a feature that has to be vigorously defended. Especially if it's basically the very first thing anyone will have to do in any web application in a language that is meant for client-side web programming
- ch4s3 9y ago> It doesn't mean it's a feature that has to be vigorously defended. I'm not sure anyone is defending it. At least, I'm not. Basically Elm's compromise is safety, and less runtime complexity then Haskell. The trade off there is boilerplate. As with most things, there is no free lunch. I've stopped using Elm precisely because of these issues, but I understand why Evan made the choices he did.
- KirinDave 9y ago
- Albert_Camus 9y agoAuthor here. As I posted in a different reply: JSON decoding is hard relative to what it is like in JavaScript. In your JS code you can just call JSON.parse() and get the corresponding JavaScript object. In Elm, decoding is not nearly as easy as it is in JS because every field must be explicitly converted to an Elm value. Depending on the complexity of your conversion from JSON to Elm value (e.g. whether you are just decoding to primitive values or to custom types defined in your program), there may also be a bit of a learning curve. As I stated in the post, there is a benefit in doing all of this: your Elm application will effectively type-check your JSON and reject it if it is malformed.
- dmitriid 9y agoYeah, it will typecheck it if you're not bored out of your mind writing all the boilerplate and don't introduce subtle bugs https://medium.com/@eeue56/json-decoding-in-elm-is-still-difficult-cad2d1fb39ae https://medium.com/@eeue56/json-decoding-in-elm-is-still-dif...
- maxiepoo 9y agoCan you not do the same thing as JSON.parse() in Elm by parsing into an unstructured JSON type?
- mjaniczek 9y agoParsing into Json.Encode.Value gives you that Value but no way to work with it except, at later time, using Json.Decode.decodeValue on it. And you're back to specifying decoders...
- maxiepoo 9y agoSeems like you could easily make a library that just parses into some Union type that represents JSON values. It's likely a design decision to force people that otherwise wouldn't to represent their data in a more structured way.
- mrkgnao 9y agoThe solution isn't more "pragmatism", it's an actual "incredibly powerful type system" like Haskell's. In my projects JSON decoding is just instance FromJSON Foo and that's it.
- dmitriid 9y agoNope. The solution is actually "more pragmatism", not "here's a thing that requires a PhD in type theory to understand".
- tome 9y agoAt this point you just sound like a troll.
- dmitriid 9y agoNope. It's a genuine feeling. Basically, in an ideal world we would have a "pragmatically typed" programming language. Basically something in between "weak dynamic typing of Javascript" and "strong static typing of Haskell". No such language currently exists, and this makes me sad. It's also hard to come up with a proper definition for such a language :)
- tome 9y ago> Nope. It's a genuine feeling. That's not exclusive with being a troll. > Basically, in an ideal world we would have a "pragmatically typed" programming language. Yes, it's called Haskell. Or, if you don't like purity, either OCaml or F#.
- dmitriid 9y agoI strongly believe Haskell is very very far from being a "pragmatically typed" language. This reminds me, I need to write a blog post (which is tangentially about Haskell, but mostly about statically typed languages)
- 9y ago