Z-Data-0.2.0.0: Array, vector and text
Copyright(c) Dong Han 2019
LicenseBSD
Maintainerwinterland1989@gmail.com
Stabilityexperimental
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

Z.Data.JSON.Value

Description

This module provides definition and parsers for JSON Values, a Haskell JSON representation. The parsers is designed to comply with rfc8258, notable pitfalls are:

  • The numeric representation use Scientific, which impose a limit on number's exponent part(limited to Int).
  • Unescaped control characters(<=0x1F) are NOT accepted, (different from aeson).
  • Only 0x20, 0x09, 0x0A, 0x0D are valid JSON whitespaces, skipSpaces from this module is different from skipSpaces.
  • A JSON document shouldn't have trailing characters except whitespaces describe above, see parseValue' and parseValueChunks'.
  • Objects are represented as key-value vectors, key order and duplicated keys are preserved for further processing.

Note that rfc8258 doesn't enforce unique key in objects, it's up to users to decided how to deal with key duplication, e.g. prefer first or last key, see withFlatMap or withFlatMapR for example.

There's no lazy parsers here, every pieces of JSON document will be parsed into a normal form Value. Object and Arrays payloads are packed into Vectors to avoid accumulating lists in memory. Read more about why no lazy parsing is needed.

Synopsis

Value type

data Value Source #

A JSON value represented as a Haskell value.

The Object's payload is a key-value vector instead of a map, which parsed directly from JSON document. This design choice has following advantages:

  • Allow different strategies handling duplicated keys.
  • Allow different Map type to do further parsing, e.g. FlatMap
  • Roundtrip without touching the original key-value order.
  • Save time if constructing map is not neccessary, e.g. using a linear scan to find a key if only that key is needed.

Instances

Instances details
Eq Value Source # 
Instance details

Defined in Z.Data.JSON.Value

Methods

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

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

Show Value Source # 
Instance details

Defined in Z.Data.JSON.Value

Methods

showsPrec :: Int -> Value -> ShowS #

show :: Value -> String #

showList :: [Value] -> ShowS #

Generic Value Source # 
Instance details

Defined in Z.Data.JSON.Value

Associated Types

type Rep Value :: Type -> Type #

Methods

from :: Value -> Rep Value x #

to :: Rep Value x -> Value #

Arbitrary Value Source # 
Instance details

Defined in Z.Data.JSON.Value

Methods

arbitrary :: Gen Value #

shrink :: Value -> [Value] #

NFData Value Source # 
Instance details

Defined in Z.Data.JSON.Value

Methods

rnf :: Value -> () #

ShowT Value Source # 
Instance details

Defined in Z.Data.JSON.Value

Methods

toUTF8BuilderP :: Int -> Value -> Builder () Source #

FromValue Value Source # 
Instance details

Defined in Z.Data.JSON.Base

EncodeJSON Value Source # 
Instance details

Defined in Z.Data.JSON.Base

Methods

encodeJSON :: Value -> Builder () Source #

ToValue Value Source # 
Instance details

Defined in Z.Data.JSON.Base

Methods

toValue :: Value -> Value Source #

type Rep Value Source # 
Instance details

Defined in Z.Data.JSON.Value

parse into JSON Value

parseValue :: Bytes -> (Bytes, Either ParseError Value) Source #

Parse Value without consuming trailing bytes.

parseValue' :: Bytes -> Either ParseError Value Source #

Parse Value, and consume all trailing JSON white spaces, if there're bytes left, parsing will fail.

parseValueChunks :: Monad m => m Bytes -> Bytes -> m (Bytes, Either ParseError Value) Source #

Increamental parse Value without consuming trailing bytes.

parseValueChunks' :: Monad m => m Bytes -> Bytes -> m (Either ParseError Value) Source #

Increamental parse Value and consume all trailing JSON white spaces, if there're bytes left, parsing will fail.

Value Parsers

value :: Parser Value Source #

JSON Value parser.

object :: Parser (Vector (Text, Value)) Source #

parse json array with leading 123.

array :: Parser (Vector Value) Source #

parse json array with leading 91.

skipSpaces :: Parser () Source #

The only valid whitespace in a JSON document is space, newline, carriage pure, and tab.