1{-# LANGUAGE BangPatterns #-}
2{-# LANGUAGE OverloadedStrings #-}
4-- Module: Data.Aeson.Text
5-- Copyright: (c) 2012-2016 Bryan O'Sullivan
6-- (c) 2011 MailRank, Inc.
8-- Maintainer: Bryan O'Sullivan <bos@serpentine.com>
9-- Stability: experimental
10-- Portability: portable
12-- Most frequently, you'll probably want to encode straight to UTF-8
13-- (the standard JSON encoding) using 'encode'.
15-- You can use the conversions to 'Builder's when embedding JSON messages as
16-- parts of a protocol.
26import Data.Aeson.Types (Value(..), ToJSON(..))
27import Data.Aeson.Encoding (encodingToLazyByteString)
28import Data.Monoid ((<>))
29import Data.Scientific (FPFormat(..), Scientific, base10Exponent)
30import Data.Text.Lazy.Builder
31import Data.Text.Lazy.Builder.Scientific (formatScientificBuilder)
32import Numeric (showHex)
33import qualified Data.HashMap.Strict as H
34import qualified Data.Text as T
35import qualified Data.Text.Lazy as LT
36import qualified Data.Text.Lazy.Encoding as LT
37import qualified Data.Vector as V
39-- | Encode a JSON 'Value' to a "Data.Text.Lazy"
41-- /Note:/ uses 'toEncoding'
42encodeToLazyText :: ToJSON a => a -> LT.Text
43encodeToLazyText = LT.decodeUtf8 . encodingToLazyByteString . toEncoding
45-- | Encode a JSON 'Value' to a "Data.Text" 'Builder', which can be
46-- embedded efficiently in a text-based protocol.
48-- If you are going to immediately encode straight to a
49-- 'L.ByteString', it is more efficient to use 'encodeToBuilder'
52-- /Note:/ Uses 'toJSON'
53encodeToTextBuilder :: ToJSON a => a -> Builder
57 go Null = {-# SCC "go/Null" #-} "null"
58 go (Bool b) = {-# SCC "go/Bool" #-} if b then "true" else "false"
59 go (Number s) = {-# SCC "go/Number" #-} fromScientific s
60 go (String s) = {-# SCC "go/String" #-} string s
62 | V.null v = {-# SCC "go/Array" #-} "[]"
63 | otherwise = {-# SCC "go/Array" #-}
65 go (V.unsafeHead v) <>
66 V.foldr f (singleton ']') (V.unsafeTail v)
67 where f a z = singleton ',' <> go a <> z
68 go (Object m) = {-# SCC "go/Object" #-}
70 (x:xs) -> singleton '{' <> one x <> foldr f (singleton '}') xs
72 where f a z = singleton ',' <> one a <> z
73 one (k,v) = string k <> singleton ':' <> go v
75string :: T.Text -> Builder
76string s = {-# SCC "string" #-} singleton '"' <> quote s <> singleton '"'
78 quote q = case T.uncons t of
80 Just (!c,t') -> fromText h <> escape c <> quote t'
81 where (h,t) = {-# SCC "break" #-} T.break isEscape q
82 isEscape c = c == '\"' ||
92 | c < '\x20' = fromString $ "\\u" ++ replicate (4 - length h) '0' ++ h
93 | otherwise = singleton c
94 where h = showHex (fromEnum c) ""
96fromScientific :: Scientific -> Builder
97fromScientific s = formatScientificBuilder format prec s
100 | base10Exponent s < 0 = (Generic, Nothing)
101 | otherwise = (Fixed, Just 0)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list