Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Encoding and decoding UTF-8 JSON content.
This module is a thin wrapper around the most excellent aeson library, which has rich and powerful facilities for encoding Haskell types into JSON.
Quite often, however, you find yourself having to create a Haskell type just to read some JSON coming from an external web service or API. This can be challenging when the source of the JSON is complex or varying its schema over time. For ease of exploration this module simply defines an easy to use intermediate type representing JSON as a format.
Often you'll be working with literals directly in your code. While you can write:
j =JsonObject
(intoMap
[(JsonKey
"answer",JsonNumber
42)])
and it would be correct, enabling:
{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE OverloadedLists #-}
allows you to write:
j = JsonObject
[("answer", 42)]
which you is somewhat less cumbersome in declaration-heavy code. You're certainly welcome to use the constructors if you find it makes for more readable code or if you need the type annotations.
Synopsis
- encodeToUTF8 :: JsonValue -> Bytes
- encodeToRope :: JsonValue -> Rope
- decodeFromUTF8 :: Bytes -> Maybe JsonValue
- decodeFromRope :: Rope -> Maybe JsonValue
- data JsonValue
- newtype JsonKey = JsonKey Rope
- data JsonToken
- colourizeJson :: JsonToken -> AnsiColour
- prettyKey :: JsonKey -> Doc JsonToken
- prettyValue :: JsonValue -> Doc JsonToken
Encoding and Decoding
encodeToUTF8 :: JsonValue -> Bytes Source #
Given a JSON value, encode it to UTF-8 bytes
I know we're not supposed to rely on types to document functions, but really, this one does what it says on the tin.
encodeToRope :: JsonValue -> Rope Source #
Given a JSON value, encode it to a Rope (which, by definition, is UTF-8 internally).
decodeFromUTF8 :: Bytes -> Maybe JsonValue Source #
Given an array of bytes, attempt to decode it as a JSON value.
decodeFromRope :: Rope -> Maybe JsonValue Source #
Given an string that is full of a bunch of JSON, attempt to decode it.
A JSON value.
JsonObject (Map JsonKey JsonValue) | |
JsonArray [JsonValue] | |
JsonString Rope | |
JsonNumber Scientific | |
JsonBool Bool | |
JsonNull |
Instances
Keys in a JSON object.
Instances
IsString JsonKey Source # | |
Defined in Core.Encoding.Json fromString :: String -> JsonKey # | |
Generic JsonKey Source # | |
Show JsonKey Source # | |
Key JsonKey Source # | |
Defined in Core.Encoding.Json | |
Textual JsonKey Source # | |
Render JsonKey Source # | |
Eq JsonKey Source # | |
Ord JsonKey Source # | |
Hashable JsonKey Source # | |
Defined in Core.Encoding.Json | |
Pretty JsonKey Source # | |
Defined in Core.Encoding.Json | |
type Rep JsonKey Source # | |
Defined in Core.Encoding.Json | |
type Token JsonKey Source # | |
Defined in Core.Encoding.Json |
Syntax highlighting
Support for pretty-printing JSON values with syntax highlighting using the
prettyprinter library. To output a JSON structure to terminal
colourized with ANSI escape codes you can use the Render
instance:
debug "j" (render j)
will get you:
23:46:04Z (00.007) j = { "answer": 42 }
colourizeJson :: JsonToken -> AnsiColour Source #
Used by the Render
instance to turn symbolic annotations into ANSI colours annotations.
If you're curious, the render pipeline looks like:
render =intoText
.renderStrict
.reAnnotateS
colourize
.layoutPretty
defaultLayoutOptions
.prettyValue