json-syntax-0.2.7.2: High-performance JSON parser and encoder
Safe HaskellSafe-Inferred
LanguageHaskell2010

Json

Synopsis

Types

data Value Source #

The JSON syntax tree described by the ABNF in RFC 7159. Notable design decisions include:

  • True and False are their own data constructors rather than being lumped together under a data constructor for boolean values. This improves performance when decoding the syntax tree to a Bool.
  • Object uses an association list rather than a hash map. This is the data type that key-value pairs can be parsed into most cheaply.
  • Object and Array both use Chunks rather than using SmallArray or cons-list directly. This a middle ground between those two types. We get the efficent use of cache lines that SmallArray offers, and we get the worst-case O(1) appends that cons-list offers. Users will typically fold over the elements with the Foldable instance of Chunks, although there are functions in Data.Chunks that efficently perform other operations.

Instances

Instances details
Show Value Source # 
Instance details

Defined in Json

Methods

showsPrec :: Int -> Value -> ShowS #

show :: Value -> String #

showList :: [Value] -> ShowS #

Eq Value Source # 
Instance details

Defined in Json

Methods

(==) :: Value -> Value -> Bool #

(/=) :: Value -> Value -> Bool #

ToValue Value Source # 
Instance details

Defined in Json

Methods

toValue :: Value -> Value Source #

data Member Source #

A key-value pair in a JSON object. The name of this type is taken from section 4 of RFC 7159.

Constructors

Member 

Fields

Instances

Instances details
Show Member Source # 
Instance details

Defined in Json

Eq Member Source # 
Instance details

Defined in Json

Methods

(==) :: Member -> Member -> Bool #

(/=) :: Member -> Member -> Bool #

Classes

class ToValue a where Source #

Typeclass for types that can be encoded as JSON.

Methods

toValue :: a -> Value Source #

Instances

Instances details
ToValue Int16 Source # 
Instance details

Defined in Json

Methods

toValue :: Int16 -> Value Source #

ToValue Int32 Source # 
Instance details

Defined in Json

Methods

toValue :: Int32 -> Value Source #

ToValue Int64 Source # 
Instance details

Defined in Json

Methods

toValue :: Int64 -> Value Source #

ToValue Int8 Source # 
Instance details

Defined in Json

Methods

toValue :: Int8 -> Value Source #

ToValue Word16 Source # 
Instance details

Defined in Json

Methods

toValue :: Word16 -> Value Source #

ToValue Word32 Source # 
Instance details

Defined in Json

Methods

toValue :: Word32 -> Value Source #

ToValue Word64 Source # 
Instance details

Defined in Json

Methods

toValue :: Word64 -> Value Source #

ToValue Word8 Source # 
Instance details

Defined in Json

Methods

toValue :: Word8 -> Value Source #

ToValue Value Source # 
Instance details

Defined in Json

Methods

toValue :: Value -> Value Source #

ToValue Scientific Source # 
Instance details

Defined in Json

ToValue Text Source # 
Instance details

Defined in Json

Methods

toValue :: Text -> Value Source #

ToValue ShortText Source # 
Instance details

Defined in Json

ToValue () Source #

Encodes the unit value as JSON null.

Instance details

Defined in Json

Methods

toValue :: () -> Value Source #

ToValue Bool Source # 
Instance details

Defined in Json

Methods

toValue :: Bool -> Value Source #

ToValue Int Source # 
Instance details

Defined in Json

Methods

toValue :: Int -> Value Source #

ToValue Word Source # 
Instance details

Defined in Json

Methods

toValue :: Word -> Value Source #

ToValue a => ToValue (Array a) Source # 
Instance details

Defined in Json

Methods

toValue :: Array a -> Value Source #

(Prim a, ToValue a) => ToValue (PrimArray a) Source # 
Instance details

Defined in Json

Methods

toValue :: PrimArray a -> Value Source #

ToValue a => ToValue (SmallArray a) Source # 
Instance details

Defined in Json

ToValue a => ToValue [a] Source # 
Instance details

Defined in Json

Methods

toValue :: [a] -> Value Source #

Functions

decode :: Bytes -> Either SyntaxException Value Source #

Decode a JSON syntax tree from a byte sequence.

decodeNewlineDelimited :: Bytes -> Either SyntaxException (SmallArray Value) Source #

Decode newline-delimited JSON. Both the LF and the CRLF conventions are supported. The newline character (or character sequence) following the final object may be omitted. This also allows blanks lines consisting of only whitespace.

It's not strictly necessary for this to be a part of this library, but newline-delimited JSON is somewhat common in practice. It's nice to have this here instead of having to reimplement it in a bunch of different applications.

Note: To protect against malicious input, this reject byte sequences with more than 10 million newlines. If this is causing a problem for you, open an issue.

Other note: in the future, this function might be changed transparently to parallelize the decoding of large input (at least 1000 lines) with GHC sparks.

encode :: Value -> Builder Source #

Encode a JSON syntax tree.

Infix Synonyms

pattern (:->) :: ShortText -> Value -> Member Source #

Infix pattern synonym for Member.

Constants

emptyArray :: Value Source #

An array with no elements (i.e. [])

emptyObject :: Value Source #

An object with no members (i.e. {})

Value Construction

Array Construction

arrayFromList :: [Value] -> Value Source #

Construct a JSON array from a list of JSON values.

Unlike objectFromList, this is not currently equipped with a rewrite rule.

Object Construction

objectFromList :: [Member] -> Value Source #

Construct a JSON object from a list of members.

Note: When the argument is a list literal with 16 or fewer elements, a rewrite rule transforms this into the appropriate objectN function. When the argument is not a list literal, this function just calls smallArrayFromList on the members, which has poor performance.

object1 :: Member -> Value Source #

Construct a JSON object with one member.

object2 :: Member -> Member -> Value Source #

Construct a JSON object with two members.

object3 :: Member -> Member -> Member -> Value Source #

Construct a JSON object with three members.

object4 :: Member -> Member -> Member -> Member -> Value Source #

Construct a JSON object with four members.

object5 :: Member -> Member -> Member -> Member -> Member -> Value Source #

Construct a JSON object with five members.

object6 :: Member -> Member -> Member -> Member -> Member -> Member -> Value Source #

Construct a JSON object with six members.

object7 :: Member -> Member -> Member -> Member -> Member -> Member -> Member -> Value Source #

Construct a JSON object with seven members.

object8 :: Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Value Source #

Construct a JSON object with nine members.

object9 :: Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Value Source #

Construct a JSON object with nine members.

object10 :: Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Value Source #

Construct a JSON object with ten members.

object11 :: Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Value Source #

Construct a JSON object with eleven members.

object12 :: Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Value Source #

Construct a JSON object with twelve members.

object13 :: Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Value Source #

Construct a JSON object with thirteen members.

object14 :: Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Value Source #

Construct a JSON object with fourteen members.

object15 :: Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Value Source #

Construct a JSON object with fifteen members.

object16 :: Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Member -> Value Source #

Construct a JSON object with sixteen members.