Skip to content

Instantly share code, notes, and snippets.

@JoelQ
Created October 30, 2017 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoelQ/176d570b7ce4a028bb00916c4794ac86 to your computer and use it in GitHub Desktop.
Save JoelQ/176d570b7ce4a028bb00916c4794ac86 to your computer and use it in GitHub Desktop.

Why JSON Decoders?

JSON decoders are one of my favorite parts of Elm. Whaaaat???!!!

For many, decoders are source of frustration. For others, they seem a bit pointless. Why can't we decode automatically just like everyone else?

Join me on as we look at how Elm's decoders add more value than the basic JSON parsing provided by other languages, why decoders are difficult to learn, and dive into a few tricks to make your decoding life easier.

You'll leave with a new appreciation for decoders, and maybe they'll become one of your favorite parts of Elm too.

Motivation

JSON decoding has a bit of a weird reputation in the Elm community. Newcomers dislike it while veterans swear by it. In this talk I want to bridge the divide by addressing some key misconceptions such as:

  • Decoders exist to transform JSON into Elm records
  • Your program's data structure will mirror JSON's
  • You can assume data from the server will be correct

I also want to address decoders' steep learning curve. JSON decoders build on top of many concepts in Elm such as:

  • Auto-generated constructor functions
  • map functions
  • andThen
  • de-coupling the structure of data from actually generating that data
  • The Result type

Newcomers try to immediately jump into JSON assuming it will be easy (based on experience in other languages) and then get confused by all these new concepts. There's a reason the official guide introduces JSON at the end

Rough outline

Decoders vs the world

Elm decoders give you a lot of features not found in other languages JSON parsing so you can't compare them directly. Show how much work you need to do to get the equivalent in another language (Ruby?).

  • Show basic "other language" JSON parsing. So simple!
  • Show equivalent Elm to decode structure to a record. More work :(
  • Now compare both when JSON has unexpected shape
  • Ruby blows up, Elm has you covered!
  • Show defensive Ruby code that can handle different shapes. Less simple now :|
  • Discuss primitive obsession
  • Show extra code to convert Ruby hashes into domain objects
  • Show Elm that decodes to union types
  • Compare and contrast
  • Elm decoders give you domain types and error handling for unexpected JSON shapes.
  • "other language"'s JSON parsing looks simple until you try to add these features. Then not so much.

Learning Decoders

This isn't a class on how to write decoders so much as some pointers on the path to learning them.

  • JSON decoders build on many Elm concepts
  • Jumping directly into them often leads to frustration
  • Some concepts:
    • Auto-generated constructor functions
    • map functions
    • andThen
    • de-coupling the structure of data from actually generating that data
    • The Result type
  • In my experience, Random Generators are more intuitive to learn than decoders and make decoders much easier
  • Avoid Json.Decode.Pipeline at first, it adds more abstraction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment