liveBook · Manning

Decode

As explained in Learn Haskell by Example

decode is an aeson function that parses a lazy ByteString as JSON and decodes it into a value with a FromJSON instance.

decode is an aeson function that parses a lazy ByteString containing JSON and produces a value whose type has a FromJSON instance. Its result is Maybe a: successful input yields Just a, while invalid JSON or a failed conversion yields Nothing. section 16.1.1

Use decode at the boundary between JSON input and typed Haskell values:

  • Decode into the generic Value representation when the JSON structure is not yet mapped to a domain type. section 16.1.1
  • Decode directly into a type such as [Int] or TodoEntry when a suitable FromJSON instance is available. section 16.1.1
  • Handle Nothing before using the decoded value; malformed JSON and conversion failures do not produce a typed result. section 16.1.1

Decoding a todo entry

After defining a TodoEntry FromJSON instance, decode can turn JSON into a typed value: J.decode "{\"text\":\"Do something\",\"checked\":false}" :: Maybe TodoEntry The result is Just (TodoEntry {entryId = Nothing, text = "Do something", checked = False, rank = Nothing}). A missing required checked field instead produces Nothing when using decode directly. section 16.1.1

Separating JSON decoding from validation

A custom parser can first call decode and then inspect the resulting Value with aeson helpers such as withText or withObject. For example, invalid JSON becomes a parser failure, while valid JSON of the wrong shape reports an expected-type error. section 16.1.1

ghci> import Data.Aeson as J
ghci> :set -XOverloadedStrings
ghci> :i J.Value
type Value :: *
data Value
  = Object !Object
  | Array !Array
  | String !Text
  | Number !Scientific
  | Bool !Bool
  | Null
ghci> J.decode "{\"a\":\"Hello\", \"b\": [1,2,3]}" :: Maybe J.Value
Just (Object (fromList [("a",String "Hello"),("b",Array
 [Number 1.0,Number 2.0,Number 3.0])]))
ghci> :t J.decode
J.decode :: FromJSON a => ByteString -> Maybe a

Inspect Value constructors and try decode interactively in GHCi, including decoding an object into Value and checking the inferred type. section 16.1.1

How Learn Haskell by Example covers it

How deep it goes
Defines the type and demonstrates typed decoding, then connects failures to custom Parser validation without detailing aeson internals.
Examples
Uses GHCi expressions for Value and lists, then decodes TodoEntry records and tests malformed or incomplete JSON.
What it assumes
Assumes lazy ByteString input and a FromJSON instance; teaches Value, JSON parsing, and custom record parsers through examples.
What you can do after
Apply it via aeson to convert JSON ByteStrings into values with FromJSON instances and handle unsuccessful decoding.
How it connects
Connects JSON, lazy ByteString, Value, FromJSON, Parser, encode, and ToJSON.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Haskell by Example. The section links point to the parts of the book it draws on.