A JSON value is aeson's Haskell representation of any possible JSON value. The Value type has six forms: Object, Array, String, Number, Bool, and Null. GHCi can decode JSON text into Maybe Value, allowing JSON structure to be inspected directly. section 16.1.1
Use JSON values as the intermediate representation between JSON data and typed Haskell values:
- Decode a lazy ByteString with decode, whose FromJSON constraint determines the target type. section 16.1.1
- Convert ordinary Haskell values into Value with toJSON, including strings, numbers, and Booleans. section 16.1.2
- Construct object values with object and key-value pairs made with (.=). section 16.1.2
- Parse a Value into a domain type with helpers such as withObject, withText, withArray, withScientific, and withBool. section 16.1.1
Inspecting and parsing JSON values
The JSON object {"a":"Hello", "b":[1,2,3]} decodes to an Object containing a String and an Array of Number values. A parser using withText accepts a JSON string but reports an error when given a Number. A TodoEntry parser instead uses withObject, reads required fields with (.:), and reads optional fields with (.:?).
Value also supports serialization: ToJSON turns a Haskell value into a Value, and encode turns that Value into JSON bytes. Defining matching ToJSON and FromJSON instances enables a round trip between a TodoEntry and its JSON representation. section 16.1.2
