Safe Haskell | None |
---|---|
Language | Haskell2010 |
Representation of a JSON number and its various components.
Synopsis
- data JNumber = JNumber {}
- class HasJNumber c where
- data E
- class AsE r where
- newtype Frac = Frac (NonEmpty DecDigit)
- data Exp = Exp {
- _ex :: E
- _minusplus :: Maybe Bool
- _expdigits :: NonEmpty DecDigit
- class HasExp c where
- type JInt = JInt' DecDigit
- data JInt' digit
- _JZero :: Prism' JInt ()
- _JIntInt :: Prism' (JInt' digit) (digit, [DecDigit])
- _JNumberInt :: Prism' JNumber Int
- _JNumberScientific :: Prism' JNumber Scientific
- jNumberBuilder :: JNumber -> Builder
- parseJNumber :: (Monad f, CharParsing f) => f JNumber
- jNumberToScientific :: JNumber -> Maybe Scientific
Types
JSON Number type.
class HasJNumber c where Source #
Typeclass for things that may have a JNumber
.
The textual exponent character may be upper or lower case, we maintain this fact using this type.
Typeclass for things that may represent a upper or lower case exponent character.
The fractional component of a JSON numeric value
The exponent part of a JSON numeric value
Exp | |
|
Typeclass for things that may have an Exp
component.
Represent a JSON "int"
Instances
Eq digit => Eq (JInt' digit) Source # | |
Ord digit => Ord (JInt' digit) Source # | |
Defined in Waargonaut.Types.JNumber | |
Show digit => Show (JInt' digit) Source # | |
Prisms
_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