Safe Haskell | None |
---|---|
Language | Haskell2010 |
Types and functions for handling JSON strings.
Synopsis
- type JString = JString' HeXDigit
- newtype JString' digit = JString' (Vector (JChar digit))
- class AsJString a where
- parseJString :: CharParsing f => f JString
- jStringBuilder :: JString -> Builder
- textToJString :: Text -> JString
Types
type JString = JString' HeXDigit Source #
As only one subset of digits are currently acceptable, Hexadecimal, we provide this type alias to close that loop.
newtype JString' digit Source #
A JSON string is a list of JSON acceptable characters, we use a newtype to
create the JString
type from a 'Vector JChar'. This is polymorphic over the
acceptable types of character encoding digits.
Instances
Parser / Builder
parseJString :: CharParsing f => f JString Source #
Parse a JString
, storing escaped characters and any explicitly escaped
character encodings '\uXXXX'.
>>>
testparse parseJString "\"\""
Right (JString' [])
>>>
testparse parseJString "\"\\\\\""
Right (JString' [EscapedJChar ReverseSolidus])
>>>
testparse parseJString "\"abc\""
Right (JString' [UnescapedJChar (JCharUnescaped 'a'),UnescapedJChar (JCharUnescaped 'b'),UnescapedJChar (JCharUnescaped 'c')])
>>>
testparse parseJString "\"a\\rbc\""
Right (JString' [UnescapedJChar (JCharUnescaped 'a'),EscapedJChar (WhiteSpace CarriageReturn),UnescapedJChar (JCharUnescaped 'b'),UnescapedJChar (JCharUnescaped 'c')])
>>>
testparse parseJString "\"a\\rbc\\uab12\\ndef\\\"\"" :: Either DecodeError JString
Right (JString' [UnescapedJChar (JCharUnescaped 'a'),EscapedJChar (WhiteSpace CarriageReturn),UnescapedJChar (JCharUnescaped 'b'),UnescapedJChar (JCharUnescaped 'c'),EscapedJChar (Hex (HexDigit4 HeXDigita HeXDigitb HeXDigit1 HeXDigit2)),EscapedJChar (WhiteSpace NewLine),UnescapedJChar (JCharUnescaped 'd'),UnescapedJChar (JCharUnescaped 'e'),UnescapedJChar (JCharUnescaped 'f'),EscapedJChar QuotationMark])
>>>
testparsethennoteof parseJString "\"a\"\\u"
Right (JString' [UnescapedJChar (JCharUnescaped 'a')])
>>>
testparsethennoteof parseJString "\"a\"\t"
Right (JString' [UnescapedJChar (JCharUnescaped 'a')])
jStringBuilder :: JString -> Builder Source #
Builder for a JString
.
>>>
BB.toLazyByteString $ jStringBuilder ((JString' V.empty) :: JString)
"\"\""
>>>
BB.toLazyByteString $ jStringBuilder ((JString' $ V.fromList [UnescapedJChar (JCharUnescaped 'a'),UnescapedJChar (JCharUnescaped 'b'),UnescapedJChar (JCharUnescaped 'c')]) :: JString)
"\"abc\""
>>>
BB.toLazyByteString $ jStringBuilder ((JString' $ V.fromList [UnescapedJChar (JCharUnescaped 'a'),EscapedJChar (WhiteSpace CarriageReturn),UnescapedJChar (JCharUnescaped 'b'),UnescapedJChar (JCharUnescaped 'c')]) :: JString)
"\"a\\rbc\""
>>>
BB.toLazyByteString $ jStringBuilder ((JString' $ V.fromList [UnescapedJChar (JCharUnescaped 'a'),EscapedJChar (WhiteSpace CarriageReturn),UnescapedJChar (JCharUnescaped 'b'),UnescapedJChar (JCharUnescaped 'c'),EscapedJChar (Hex (HexDigit4 HeXDigita HeXDigitb HeXDigit1 HeXDigit2)),EscapedJChar (WhiteSpace NewLine),UnescapedJChar (JCharUnescaped 'd'),UnescapedJChar (JCharUnescaped 'e'),UnescapedJChar (JCharUnescaped 'f'),EscapedJChar QuotationMark]) :: JString)
"\"a\\rbc\\uab12\\ndef\\\"\""
>>>
BB.toLazyByteString $ jStringBuilder ((JString' $ V.singleton (UnescapedJChar (JCharUnescaped 'a'))) :: JString)
"\"a\""
>>>
BB.toLazyByteString $ jStringBuilder (JString' $ V.singleton (EscapedJChar ReverseSolidus) :: JString)
"\"\\\\\""