waargonaut-0.1.0.0: JSON wrangling

Safe HaskellNone
LanguageHaskell2010

Waargonaut.Types.JNumber

Contents

Description

Representation of a JSON number and its various components.

Synopsis

Types

data E Source #

The textual exponent character may be upper or lower case, we maintain this fact using this type.

Constructors

EE 
Ee 
Instances
Eq E Source # 
Instance details

Defined in Waargonaut.Types.JNumber

Methods

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

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

Ord E Source # 
Instance details

Defined in Waargonaut.Types.JNumber

Methods

compare :: E -> E -> Ordering #

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

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

(>) :: E -> E -> Bool #

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

max :: E -> E -> E #

min :: E -> E -> E #

Show E Source # 
Instance details

Defined in Waargonaut.Types.JNumber

Methods

showsPrec :: Int -> E -> ShowS #

show :: E -> String #

showList :: [E] -> ShowS #

AsE E Source # 
Instance details

Defined in Waargonaut.Types.JNumber

Methods

_E :: Prism' E E Source #

_EE :: Prism' E () Source #

_Ee :: Prism' E () Source #

class AsE r where Source #

Typeclass for things that may represent a upper or lower case exponent character.

Minimal complete definition

_E

Methods

_E :: Prism' r E Source #

_EE :: Prism' r () Source #

_Ee :: Prism' r () Source #

Instances
AsE E Source # 
Instance details

Defined in Waargonaut.Types.JNumber

Methods

_E :: Prism' E E Source #

_EE :: Prism' E () Source #

_Ee :: Prism' E () Source #

newtype Frac Source #

The fractional component of a JSON numeric value

Constructors

Frac (NonEmpty DecDigit) 
Instances
Eq Frac Source # 
Instance details

Defined in Waargonaut.Types.JNumber

Methods

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

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

Ord Frac Source # 
Instance details

Defined in Waargonaut.Types.JNumber

Methods

compare :: Frac -> Frac -> Ordering #

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

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

(>) :: Frac -> Frac -> Bool #

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

max :: Frac -> Frac -> Frac #

min :: Frac -> Frac -> Frac #

Show Frac Source # 
Instance details

Defined in Waargonaut.Types.JNumber

Methods

showsPrec :: Int -> Frac -> ShowS #

show :: Frac -> String #

showList :: [Frac] -> ShowS #

Wrapped Frac Source # 
Instance details

Defined in Waargonaut.Types.JNumber

Associated Types

type Unwrapped Frac :: Type #

Frac ~ t => Rewrapped Frac t Source # 
Instance details

Defined in Waargonaut.Types.JNumber

type Unwrapped Frac Source # 
Instance details

Defined in Waargonaut.Types.JNumber

data Exp Source #

The exponent part of a JSON numeric value

Constructors

Exp 
Instances
Eq Exp Source # 
Instance details

Defined in Waargonaut.Types.JNumber

Methods

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

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

Ord Exp Source # 
Instance details

Defined in Waargonaut.Types.JNumber

Methods

compare :: Exp -> Exp -> Ordering #

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

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

(>) :: Exp -> Exp -> Bool #

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

max :: Exp -> Exp -> Exp #

min :: Exp -> Exp -> Exp #

Show Exp Source # 
Instance details

Defined in Waargonaut.Types.JNumber

Methods

showsPrec :: Int -> Exp -> ShowS #

show :: Exp -> String #

showList :: [Exp] -> ShowS #

HasExp Exp Source # 
Instance details

Defined in Waargonaut.Types.JNumber

class HasExp c where Source #

Typeclass for things that may have an Exp component.

Minimal complete definition

exp

type JInt = JInt' DecDigit Source #

Type alias to allow us to constrain the first digit type.

data JInt' digit Source #

Represent a JSON "int"

Constructors

JZero 
JIntInt digit [DecDigit] 
Instances
Eq digit => Eq (JInt' digit) Source # 
Instance details

Defined in Waargonaut.Types.JNumber

Methods

(==) :: JInt' digit -> JInt' digit -> Bool #

(/=) :: JInt' digit -> JInt' digit -> Bool #

Ord digit => Ord (JInt' digit) Source # 
Instance details

Defined in Waargonaut.Types.JNumber

Methods

compare :: JInt' digit -> JInt' digit -> Ordering #

(<) :: JInt' digit -> JInt' digit -> Bool #

(<=) :: JInt' digit -> JInt' digit -> Bool #

(>) :: JInt' digit -> JInt' digit -> Bool #

(>=) :: JInt' digit -> JInt' digit -> Bool #

max :: JInt' digit -> JInt' digit -> JInt' digit #

min :: JInt' digit -> JInt' digit -> JInt' digit #

Show digit => Show (JInt' digit) Source # 
Instance details

Defined in Waargonaut.Types.JNumber

Methods

showsPrec :: Int -> JInt' digit -> ShowS #

show :: JInt' digit -> String #

showList :: [JInt' digit] -> ShowS #

Prisms

_JZero :: Prism' JInt () Source #

Prism for JSON zeroes.

_JIntInt :: Prism' (JInt' digit) (digit, [DecDigit]) Source #

Prism for JSON non-zero values.

_JNumberInt :: Prism' JNumber Int Source #

Prism between a JNumber and a Haskell Int. This prism will go via the Scientific type to handle the various exponent and fractional values before attempting to convert it to a bounded integer.

_JNumberScientific :: Prism' JNumber Scientific Source #

Prism for trying to move between JNumber and Scientific

>>> _JNumberScientific # (read "-3.45e-2")
JNumber {_minus = True, _numberint = JIntInt DecDigit3 [], _frac = Just (Frac (DecDigit4 :| [DecDigit5])), _expn = Just (Exp {_ex = Ee, _minusplus = Just True, _expdigits = DecDigit2 :| []})}
>>> _JNumberScientific # (read "-1.23456e-787")
JNumber {_minus = True, _numberint = JIntInt DecDigit1 [], _frac = Just (Frac (DecDigit2 :| [DecDigit3,DecDigit4,DecDigit5,DecDigit6])), _expn = Just (Exp {_ex = Ee, _minusplus = Just True, _expdigits = DecDigit7 :| [DecDigit8,DecDigit7]})}
>>> _JNumberScientific # (read "-1.23456e791")
JNumber {_minus = True, _numberint = JIntInt DecDigit1 [], _frac = Just (Frac (DecDigit2 :| [DecDigit3,DecDigit4,DecDigit5,DecDigit6])), _expn = Just (Exp {_ex = Ee, _minusplus = Just False, _expdigits = DecDigit7 :| [DecDigit9,DecDigit1]})}

Parser / Builder

jNumberBuilder :: JNumber -> Builder Source #

Printing of JNumbers

>>> toLazyByteString $ jNumberBuilder (JNumber {_minus = False, _numberint = JIntInt D.DecDigit3 [], _frac = Just (Frac (D.DecDigit4 :| [D.DecDigit5])), _expn = Just (Exp {_ex = Ee, _minusplus = Just False, _expdigits = D.DecDigit1 :| [D.DecDigit0]})})
"3.45e+10"
>>> toLazyByteString $ jNumberBuilder (JNumber {_minus = True, _numberint = JIntInt D.DecDigit3 [], _frac = Just (Frac (D.DecDigit4 :| [D.DecDigit5])), _expn = Just (Exp {_ex = Ee, _minusplus = Just True, _expdigits = D.DecDigit0 :| [D.x2]})})
"-3.45e-02"
>>> toLazyByteString $ jNumberBuilder (JNumber {_minus = False, _numberint = JIntInt D.DecDigit0 [D.DecDigit0], _frac = Nothing, _expn = Nothing})
"00"

parseJNumber :: (Monad f, CharParsing f) => f JNumber Source #

Parse a JSON numeric value.

>>> testparsethen parseJNumber "600x"
Right (JNumber {_minus = False, _numberint = JIntInt DecDigit6 [DecDigit0,DecDigit0], _frac = Nothing, _expn = Nothing},'x')
>>> testparsethen parseJNumber "800x"
Right (JNumber {_minus = False, _numberint = JIntInt DecDigit8 [DecDigit0,DecDigit0], _frac = Nothing, _expn = Nothing},'x')
>>> testparsethen parseJNumber "3x"
Right (JNumber {_minus = False, _numberint = JIntInt DecDigit3 [], _frac = Nothing, _expn = Nothing},'x')
>>> testparsethen parseJNumber "-3x"
Right (JNumber {_minus = True, _numberint = JIntInt DecDigit3 [], _frac = Nothing, _expn = Nothing},'x')
>>> testparsethen parseJNumber "0x"
Right (JNumber {_minus = False, _numberint = JZero, _frac = Nothing, _expn = Nothing},'x')
>>> testparsethen parseJNumber "-0x"
Right (JNumber {_minus = True, _numberint = JZero, _frac = Nothing, _expn = Nothing},'x')
>>> testparsethen parseJNumber "3.45x"
Right (JNumber {_minus = False, _numberint = JIntInt DecDigit3 [], _frac = Just (Frac (DecDigit4 :| [DecDigit5])), _expn = Nothing},'x')
>>> testparsethen parseJNumber "-3.45x"
Right (JNumber {_minus = True, _numberint = JIntInt DecDigit3 [], _frac = Just (Frac (DecDigit4 :| [DecDigit5])), _expn = Nothing},'x')
>>> testparsethen parseJNumber "3.45e10x"
Right (JNumber {_minus = False, _numberint = JIntInt DecDigit3 [], _frac = Just (Frac (DecDigit4 :| [DecDigit5])), _expn = Just (Exp {_ex = Ee, _minusplus = Nothing, _expdigits = DecDigit1 :| [DecDigit0]})},'x')
>>> testparsethen parseJNumber "3e10x"
Right (JNumber {_minus = False, _numberint = JIntInt DecDigit3 [], _frac = Nothing, _expn = Just (Exp {_ex = Ee, _minusplus = Nothing, _expdigits = DecDigit1 :| [DecDigit0]})},'x')
>>> testparsethen parseJNumber "3.45e+10x"
Right (JNumber {_minus = False, _numberint = JIntInt DecDigit3 [], _frac = Just (Frac (DecDigit4 :| [DecDigit5])), _expn = Just (Exp {_ex = Ee, _minusplus = Just False, _expdigits = DecDigit1 :| [DecDigit0]})},'x')
>>> testparsethen parseJNumber "-3.45e-02x"
Right (JNumber {_minus = True, _numberint = JIntInt DecDigit3 [], _frac = Just (Frac (DecDigit4 :| [DecDigit5])), _expn = Just (Exp {_ex = Ee, _minusplus = Just True, _expdigits = DecDigit0 :| [DecDigit2]})},'x')
>>> isLeft (testparsethen parseJNumber "-3.45ex")
True
>>> isLeft (testparsethen parseJNumber "-.45e1x")
True

Other

jNumberToScientific :: JNumber -> Maybe Scientific Source #

Returns a normalised Scientific value or Nothing if the exponent is out of the range [minBound,maxBound::Int]

>>> jNumberToScientific JNumber {_minus = True, _numberint = JIntInt DecDigit3 [], _frac = Just (Frac (D.x4 :| [D.x5])), _expn = Just (Exp {_ex = Ee, _minusplus = Just True, _expdigits = D.x0 :| [D.x2]})}
Just -3.45e-2
>>> jNumberToScientific JNumber {_minus = True, _numberint = JIntInt D.x1 [D.x2, D.x3], _frac = Just (Frac (D.x4 :| [D.x5, D.x6])), _expn = Just (Exp {_ex = Ee, _minusplus = Just True, _expdigits = (D.x7 :| [D.x8, D.x9])})}
Just -1.23456e-787
>>> jNumberToScientific JNumber {_minus = True, _numberint = JIntInt D.x1 [D.x2, D.x3], _frac = Just (Frac (D.x4 :| [D.x5, D.x6])), _expn = Just (Exp {_ex = Ee, _minusplus = Just False, _expdigits = (D.x7 :| [D.x8, D.x9])})}
Just -1.23456e791