Z-Data-0.6.1.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 #

Ord Value Source # 
Instance details

Defined in Z.Data.JSON.Value

Methods

compare :: Value -> Value -> Ordering #

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

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

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

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

max :: Value -> Value -> Value #

min :: Value -> Value -> Value #

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 -> () #

Print Value Source # 
Instance details

Defined in Z.Data.JSON.Value

Methods

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

JSON Value Source # 
Instance details

Defined in Z.Data.JSON.Base

type Rep Value Source # 
Instance details

Defined in Z.Data.JSON.Value

key :: Functor f => Text -> (Value -> f Value) -> Value -> f Value Source #

Lense for Object element

  1. return Null if Value is not an Object or key not exist.
  2. Modify will have no effect if Value is not an Object or key not exist.
  3. On duplicated keys prefer the last one.

nth :: Functor f => Int -> (Value -> f Value) -> Value -> f Value Source #

Lense for Array element.

  1. return Null if Value is not an Array or index not exist.
  2. Modify will have no effect if Value is not an Array or index not exist.

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 => ParseChunks m Bytes ParseError Value Source #

Increamental parse Value without consuming trailing bytes.

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.

Convert to Scientific

floatToScientific :: Float -> Scientific Source #

Convert IEEE float to scientific notition.

doubleToScientific :: Double -> Scientific Source #

Convert IEEE double to scientific notition.