waargonaut-0.8.0.2: JSON wrangling
Safe HaskellNone
LanguageHaskell2010

Waargonaut.Types.JNumber

Description

Representation of a JSON number and its various components.

Synopsis

Types

data JNumber Source #

JSON Number type.

Constructors

JNumber 

Instances

Instances details
Eq JNumber Source # 
Instance details

Defined in Waargonaut.Types.JNumber

Methods

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

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

Ord JNumber Source # 
Instance details

Defined in Waargonaut.Types.JNumber

Show JNumber Source # 
Instance details

Defined in Waargonaut.Types.JNumber

HasJNumber JNumber Source # 
Instance details

Defined in Waargonaut.Types.JNumber

class HasJNumber c where Source #

Typeclass for things that may have a JNumber.

Minimal complete definition

jNumber

Methods

jNumber :: Lens' c JNumber Source #

expn :: Lens' c (Maybe Exp) Source #

frac :: Lens' c (Maybe Frac) Source #

minus :: Lens' c Bool Source #

numberint :: Lens' c JInt Source #

Instances

Instances details
HasJNumber JNumber Source # 
Instance details

Defined in Waargonaut.Types.JNumber

data E Source #

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

Constructors

EE 
Ee 

Instances

Instances details
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

Instances details
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

Instances details
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

Methods

_Wrapped' :: Iso' Frac (Unwrapped Frac)

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

Defined in Waargonaut.Types.JNumber

type Unwrapped Frac Source # 
Instance details

Defined in Waargonaut.Types.JNumber

type Unwrapped Frac = NonEmpty DecDigit

data Exp Source #

The exponent part of a JSON numeric value

Constructors

Exp 

Fields

Instances

Instances details
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

Methods

exp :: Lens' Exp Exp Source #

ex :: Lens' Exp E Source #

expdigits :: Lens' Exp (NonEmpty DecDigit) Source #

minusplus :: Lens' Exp (Maybe Bool) Source #

class HasExp c where Source #

Typeclass for things that may have an Exp component.

Minimal complete definition

exp

Methods

exp :: Lens' c Exp Source #

ex :: Lens' c E Source #

expdigits :: Lens' c (NonEmpty DecDigit) Source #

minusplus :: Lens' c (Maybe Bool) Source #

Instances

Instances details
HasExp Exp Source # 
Instance details

Defined in Waargonaut.Types.JNumber

Methods

exp :: Lens' Exp Exp Source #

ex :: Lens' Exp E Source #

expdigits :: Lens' Exp (NonEmpty DecDigit) Source #

minusplus :: Lens' Exp (Maybe Bool) Source #

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

Instances details
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

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

jIntToDigits :: JInt -> NonEmpty DecDigit Source #

Helper to convert a JInt to a NonEmpty list of component digits.