{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE ViewPatterns #-}
{-# OPTIONS_GHC -Wno-orphans #-}
module Data.ProtoLens.JSONPB.Class where
import qualified Data.Aeson as A (Encoding, FromJSON(..), FromJSONKey(..), FromJSONKeyFunction(..), ToJSON(..), Value(..), eitherDecode, json, (.!=))
import qualified Data.Aeson.Encoding as E
import qualified Data.Aeson.Internal as A (formatError, iparse)
import qualified Data.Aeson.Parser as A (eitherDecodeWith)
import qualified Data.Aeson.Types as A (Object, Pair, Parser, Series, explicitParseField, explicitParseFieldMaybe, object, typeMismatch)
import qualified Data.Attoparsec.ByteString as Atto (skipWhile)
import qualified Data.Attoparsec.ByteString.Char8 as Atto (Parser, endOfInput)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Base64 as B64
import qualified Data.ByteString.Lazy as LBS
import Data.ProtoLens.Runtime.Data.ProtoLens (FieldDefault(..))
import Data.String (fromString)
import Data.Text (Text, unpack)
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Encoding as TL
import qualified Data.Vector as V
import GHC.Int (Int32, Int64)
import GHC.Word (Word32, Word64)
class ToJSONPB a where
toJSONPB :: a -> Options -> A.Value
toEncodingPB :: a -> Options -> A.Encoding
toEncodingPB a
x = Value -> Encoding
forall a. ToJSON a => a -> Encoding
A.toEncoding (Value -> Encoding) -> (Options -> Value) -> Options -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Options -> Value
forall a. ToJSONPB a => a -> Options -> Value
toJSONPB a
x
class FromJSONPB a where
parseJSONPB :: A.Value -> A.Parser a
encode :: ToJSONPB a => Options -> a -> LBS.ByteString
encode :: forall a. ToJSONPB a => Options -> a -> ByteString
encode Options
opts a
x = Encoding -> ByteString
forall a. Encoding' a -> ByteString
E.encodingToLazyByteString (a -> Options -> Encoding
forall a. ToJSONPB a => a -> Options -> Encoding
toEncodingPB a
x Options
opts)
{-# INLINE encode #-}
eitherDecode :: FromJSONPB a => LBS.ByteString -> Either String a
eitherDecode :: forall a. FromJSONPB a => ByteString -> Either [Char] a
eitherDecode =
Either (JSONPath, [Char]) a -> Either [Char] a
forall {b}. Either (JSONPath, [Char]) b -> Either [Char] b
eitherFormatError (Either (JSONPath, [Char]) a -> Either [Char] a)
-> (ByteString -> Either (JSONPath, [Char]) a)
-> ByteString
-> Either [Char] a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser Value
-> (Value -> IResult a)
-> ByteString
-> Either (JSONPath, [Char]) a
forall a.
Parser Value
-> (Value -> IResult a)
-> ByteString
-> Either (JSONPath, [Char]) a
A.eitherDecodeWith Parser Value
jsonEOF ((Value -> Parser a) -> Value -> IResult a
forall a b. (a -> Parser b) -> a -> IResult b
A.iparse Value -> Parser a
forall a. FromJSONPB a => Value -> Parser a
parseJSONPB)
where
eitherFormatError :: Either (JSONPath, [Char]) b -> Either [Char] b
eitherFormatError = ((JSONPath, [Char]) -> Either [Char] b)
-> (b -> Either [Char] b)
-> Either (JSONPath, [Char]) b
-> Either [Char] b
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either ([Char] -> Either [Char] b
forall a b. a -> Either a b
Left ([Char] -> Either [Char] b)
-> ((JSONPath, [Char]) -> [Char])
-> (JSONPath, [Char])
-> Either [Char] b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (JSONPath -> [Char] -> [Char]) -> (JSONPath, [Char]) -> [Char]
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry JSONPath -> [Char] -> [Char]
A.formatError) b -> Either [Char] b
forall a b. b -> Either a b
Right
{-# INLINE eitherFormatError #-}
jsonEOF :: Atto.Parser A.Value
jsonEOF :: Parser Value
jsonEOF = Parser Value
A.json Parser Value -> Parser ByteString () -> Parser Value
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Parser ByteString ()
skipSpace Parser Value -> Parser ByteString () -> Parser Value
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Parser ByteString ()
forall t. Chunk t => Parser t ()
Atto.endOfInput
where
skipSpace :: Atto.Parser ()
skipSpace :: Parser ByteString ()
skipSpace =
(Word8 -> Bool) -> Parser ByteString ()
Atto.skipWhile ((Word8 -> Bool) -> Parser ByteString ())
-> (Word8 -> Bool) -> Parser ByteString ()
forall a b. (a -> b) -> a -> b
$ \Word8
w ->
Word8
w Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
0x20 Bool -> Bool -> Bool
|| Word8
w Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
0x0a Bool -> Bool -> Bool
|| Word8
w Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
0x0d Bool -> Bool -> Bool
|| Word8
w Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
0x09
{-# INLINE skipSpace #-}
{-# INLINE eitherDecode #-}
class Monoid m =>
KeyValuePB m
where
pair :: ToJSONPB v => Text -> v -> Options -> m
instance KeyValuePB A.Series where
pair :: forall v. ToJSONPB v => Text -> v -> Options -> Series
pair Text
k v
v Options
opts = Key -> Encoding -> Series
E.pair ([Char] -> Key
forall a. IsString a => [Char] -> a
fromString (Text -> [Char]
unpack Text
k)) (v -> Options -> Encoding
forall a. ToJSONPB a => a -> Options -> Encoding
toEncodingPB v
v Options
opts)
instance KeyValuePB [A.Pair] where
pair :: forall v. ToJSONPB v => Text -> v -> Options -> [Pair]
pair Text
k v
v Options
opts = Pair -> [Pair]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([Char] -> Key
forall a. IsString a => [Char] -> a
fromString (Text -> [Char]
unpack Text
k), v -> Options -> Value
forall a. ToJSONPB a => a -> Options -> Value
toJSONPB v
v Options
opts)
(.=) ::
(ToJSONPB v, KeyValuePB kvp, FieldDefault v, Eq v) => Text -> v -> Options -> kvp
Text
k .= :: forall v kvp.
(ToJSONPB v, KeyValuePB kvp, FieldDefault v, Eq v) =>
Text -> v -> Options -> kvp
.= v
v = Options -> kvp
forall {a}. KeyValuePB a => Options -> a
mk
where
mk :: Options -> a
mk opts :: Options
opts@Options {Bool
optEmitDefaultValuedFields :: Options -> Bool
optEmitDefaultValuedFields :: Bool
..}
| Bool -> Bool
not Bool
optEmitDefaultValuedFields Bool -> Bool -> Bool
&& v
forall value. FieldDefault value => value
fieldDefault v -> v -> Bool
forall a. Eq a => a -> a -> Bool
== v
v = a
forall a. Monoid a => a
mempty
| Bool
otherwise = Text -> v -> Options -> a
forall m v. (KeyValuePB m, ToJSONPB v) => Text -> v -> Options -> m
pair Text
k v
v Options
opts
(.:) :: (FromJSONPB a, FieldDefault a) => A.Object -> Text -> A.Parser a
Object
obj .: :: forall a.
(FromJSONPB a, FieldDefault a) =>
Object -> Text -> Parser a
.: Text
key = Object
obj Object -> Key -> Parser (Maybe a)
.:? [Char] -> Key
forall a. IsString a => [Char] -> a
fromString (Text -> [Char]
unpack Text
key) Parser (Maybe a) -> a -> Parser a
forall a. Parser (Maybe a) -> a -> Parser a
A..!= a
forall value. FieldDefault value => value
fieldDefault
where
.:? :: Object -> Key -> Parser (Maybe a)
(.:?) = (Value -> Parser a) -> Object -> Key -> Parser (Maybe a)
forall a. (Value -> Parser a) -> Object -> Key -> Parser (Maybe a)
A.explicitParseFieldMaybe Value -> Parser a
forall a. FromJSONPB a => Value -> Parser a
parseJSONPB
parseField :: FromJSONPB a => A.Object -> Text -> A.Parser a
parseField :: forall a. FromJSONPB a => Object -> Text -> Parser a
parseField Object
o = (Value -> Parser a) -> Object -> Key -> Parser a
forall a. (Value -> Parser a) -> Object -> Key -> Parser a
A.explicitParseField Value -> Parser a
forall a. FromJSONPB a => Value -> Parser a
parseJSONPB Object
o (Key -> Parser a) -> (Text -> Key) -> Text -> Parser a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> Key
forall a. IsString a => [Char] -> a
fromString ([Char] -> Key) -> (Text -> [Char]) -> Text -> Key
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> [Char]
unpack
data Options =
Options
{ Options -> Bool
optEmitDefaultValuedFields :: Bool
}
deriving (Int -> Options -> [Char] -> [Char]
[Options] -> [Char] -> [Char]
Options -> [Char]
(Int -> Options -> [Char] -> [Char])
-> (Options -> [Char])
-> ([Options] -> [Char] -> [Char])
-> Show Options
forall a.
(Int -> a -> [Char] -> [Char])
-> (a -> [Char]) -> ([a] -> [Char] -> [Char]) -> Show a
showList :: [Options] -> [Char] -> [Char]
$cshowList :: [Options] -> [Char] -> [Char]
show :: Options -> [Char]
$cshow :: Options -> [Char]
showsPrec :: Int -> Options -> [Char] -> [Char]
$cshowsPrec :: Int -> Options -> [Char] -> [Char]
Show)
defaultOptions :: Options
defaultOptions :: Options
defaultOptions = Options {optEmitDefaultValuedFields :: Bool
optEmitDefaultValuedFields = Bool
False}
object :: [Options -> [A.Pair]] -> Options -> A.Value
object :: [Options -> [Pair]] -> Options -> Value
object [Options -> [Pair]]
fs = [Pair] -> Value
A.object ([Pair] -> Value) -> (Options -> [Pair]) -> Options -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Options -> [Pair]] -> Options -> [Pair]
forall a. Monoid a => [a] -> a
mconcat [Options -> [Pair]]
fs
pairs :: [Options -> A.Series] -> Options -> E.Encoding
pairs :: [Options -> Series] -> Options -> Encoding
pairs [Options -> Series]
fs = Series -> Encoding
E.pairs (Series -> Encoding) -> (Options -> Series) -> Options -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Options -> Series] -> Options -> Series
forall a. Monoid a => [a] -> a
mconcat [Options -> Series]
fs
toAesonValue :: ToJSONPB a => a -> A.Value
toAesonValue :: forall a. ToJSONPB a => a -> Value
toAesonValue = (a -> Options -> Value) -> Options -> a -> Value
forall a b c. (a -> b -> c) -> b -> a -> c
flip a -> Options -> Value
forall a. ToJSONPB a => a -> Options -> Value
toJSONPB Options
defaultOptions
toAesonEncoding :: ToJSONPB a => a -> A.Encoding
toAesonEncoding :: forall a. ToJSONPB a => a -> Encoding
toAesonEncoding = (a -> Options -> Encoding) -> Options -> a -> Encoding
forall a b c. (a -> b -> c) -> b -> a -> c
flip a -> Options -> Encoding
forall a. ToJSONPB a => a -> Options -> Encoding
toEncodingPB Options
defaultOptions
parseFP :: (A.FromJSON a, A.FromJSONKey a) => String -> A.Value -> A.Parser a
parseFP :: forall a.
(FromJSON a, FromJSONKey a) =>
[Char] -> Value -> Parser a
parseFP [Char]
tyDesc Value
v =
case Value
v of
A.Number {} -> Value -> Parser a
forall a. FromJSON a => Value -> Parser a
A.parseJSON Value
v
A.String Text
t ->
case FromJSONKeyFunction a
forall a. FromJSONKey a => FromJSONKeyFunction a
A.fromJSONKey of
A.FromJSONKeyTextParser Text -> Parser a
p -> Text -> Parser a
p Text
t
FromJSONKeyFunction a
_ -> [Char] -> Parser a
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail [Char]
"internal: parseKeyPB: unexpected FromJSONKey summand"
Value
_ -> [Char] -> Value -> Parser a
forall a. [Char] -> Value -> Parser a
A.typeMismatch [Char]
tyDesc Value
v
parseNumOrDecimalString :: (A.FromJSON a) => String -> A.Value -> A.Parser a
parseNumOrDecimalString :: forall a. FromJSON a => [Char] -> Value -> Parser a
parseNumOrDecimalString [Char]
tyDesc Value
v =
case Value
v of
A.Number {} -> Value -> Parser a
forall a. FromJSON a => Value -> Parser a
A.parseJSON Value
v
A.String Text
t ->
([Char] -> Parser a)
-> (a -> Parser a) -> Either [Char] a -> Parser a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either [Char] -> Parser a
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either [Char] a -> Parser a)
-> (Text -> Either [Char] a) -> Text -> Parser a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either [Char] a
forall a. FromJSON a => ByteString -> Either [Char] a
A.eitherDecode (ByteString -> Either [Char] a)
-> (Text -> ByteString) -> Text -> Either [Char] a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
TL.encodeUtf8 (Text -> ByteString) -> (Text -> Text) -> Text -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
TL.fromStrict (Text -> Parser a) -> Text -> Parser a
forall a b. (a -> b) -> a -> b
$ Text
t
Value
_ -> [Char] -> Value -> Parser a
forall a. [Char] -> Value -> Parser a
A.typeMismatch [Char]
tyDesc Value
v
instance ToJSONPB Bool where
toJSONPB :: Bool -> Options -> Value
toJSONPB = Value -> Options -> Value
forall a b. a -> b -> a
const (Value -> Options -> Value)
-> (Bool -> Value) -> Bool -> Options -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> Value
forall a. ToJSON a => a -> Value
A.toJSON
toEncodingPB :: Bool -> Options -> Encoding
toEncodingPB = Encoding -> Options -> Encoding
forall a b. a -> b -> a
const (Encoding -> Options -> Encoding)
-> (Bool -> Encoding) -> Bool -> Options -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> Encoding
forall a. ToJSON a => a -> Encoding
A.toEncoding
instance FromJSONPB Bool where
parseJSONPB :: Value -> Parser Bool
parseJSONPB = Value -> Parser Bool
forall a. FromJSON a => Value -> Parser a
A.parseJSON
instance ToJSONPB Int32 where
toJSONPB :: Int32 -> Options -> Value
toJSONPB = Value -> Options -> Value
forall a b. a -> b -> a
const (Value -> Options -> Value)
-> (Int32 -> Value) -> Int32 -> Options -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int32 -> Value
forall a. ToJSON a => a -> Value
A.toJSON
toEncodingPB :: Int32 -> Options -> Encoding
toEncodingPB = Encoding -> Options -> Encoding
forall a b. a -> b -> a
const (Encoding -> Options -> Encoding)
-> (Int32 -> Encoding) -> Int32 -> Options -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int32 -> Encoding
forall a. ToJSON a => a -> Encoding
A.toEncoding
instance FromJSONPB Int32 where
parseJSONPB :: Value -> Parser Int32
parseJSONPB = [Char] -> Value -> Parser Int32
forall a. FromJSON a => [Char] -> Value -> Parser a
parseNumOrDecimalString [Char]
"int32 / sint32"
instance ToJSONPB Word32 where
toJSONPB :: Word32 -> Options -> Value
toJSONPB = Value -> Options -> Value
forall a b. a -> b -> a
const (Value -> Options -> Value)
-> (Word32 -> Value) -> Word32 -> Options -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word32 -> Value
forall a. ToJSON a => a -> Value
A.toJSON
toEncodingPB :: Word32 -> Options -> Encoding
toEncodingPB = Encoding -> Options -> Encoding
forall a b. a -> b -> a
const (Encoding -> Options -> Encoding)
-> (Word32 -> Encoding) -> Word32 -> Options -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word32 -> Encoding
forall a. ToJSON a => a -> Encoding
A.toEncoding
instance FromJSONPB Word32 where
parseJSONPB :: Value -> Parser Word32
parseJSONPB = [Char] -> Value -> Parser Word32
forall a. FromJSON a => [Char] -> Value -> Parser a
parseNumOrDecimalString [Char]
"uint32"
instance ToJSONPB Int64 where
toJSONPB :: Int64 -> Options -> Value
toJSONPB Int64
x Options
_ = Text -> Value
A.String (Text -> Value) -> (Int64 -> Text) -> Int64 -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> Text
T.pack ([Char] -> Text) -> (Int64 -> [Char]) -> Int64 -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int64 -> [Char]
forall a. Show a => a -> [Char]
show (Int64 -> Value) -> Int64 -> Value
forall a b. (a -> b) -> a -> b
$ Int64
x
toEncodingPB :: Int64 -> Options -> Encoding
toEncodingPB Int64
x Options
_ = [Char] -> Encoding
forall a. [Char] -> Encoding' a
E.string (Int64 -> [Char]
forall a. Show a => a -> [Char]
show Int64
x)
instance FromJSONPB Int64 where
parseJSONPB :: Value -> Parser Int64
parseJSONPB = [Char] -> Value -> Parser Int64
forall a. FromJSON a => [Char] -> Value -> Parser a
parseNumOrDecimalString [Char]
"int64 / sint64"
instance ToJSONPB Word64 where
toJSONPB :: Word64 -> Options -> Value
toJSONPB Word64
x Options
_ = Text -> Value
A.String (Text -> Value) -> (Word64 -> Text) -> Word64 -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> Text
T.pack ([Char] -> Text) -> (Word64 -> [Char]) -> Word64 -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> [Char]
forall a. Show a => a -> [Char]
show (Word64 -> Value) -> Word64 -> Value
forall a b. (a -> b) -> a -> b
$ Word64
x
toEncodingPB :: Word64 -> Options -> Encoding
toEncodingPB Word64
x Options
_ = [Char] -> Encoding
forall a. [Char] -> Encoding' a
E.string (Word64 -> [Char]
forall a. Show a => a -> [Char]
show Word64
x)
instance FromJSONPB Word64 where
parseJSONPB :: Value -> Parser Word64
parseJSONPB = [Char] -> Value -> Parser Word64
forall a. FromJSON a => [Char] -> Value -> Parser a
parseNumOrDecimalString [Char]
"int64 / sint64"
instance ToJSONPB Float where
toJSONPB :: Float -> Options -> Value
toJSONPB = Value -> Options -> Value
forall a b. a -> b -> a
const (Value -> Options -> Value)
-> (Float -> Value) -> Float -> Options -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Float -> Value
forall a. ToJSON a => a -> Value
A.toJSON
toEncodingPB :: Float -> Options -> Encoding
toEncodingPB = Encoding -> Options -> Encoding
forall a b. a -> b -> a
const (Encoding -> Options -> Encoding)
-> (Float -> Encoding) -> Float -> Options -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Float -> Encoding
forall a. ToJSON a => a -> Encoding
A.toEncoding
instance FromJSONPB Float where
parseJSONPB :: Value -> Parser Float
parseJSONPB = [Char] -> Value -> Parser Float
forall a.
(FromJSON a, FromJSONKey a) =>
[Char] -> Value -> Parser a
parseFP [Char]
"float"
instance ToJSONPB Double where
toJSONPB :: Double -> Options -> Value
toJSONPB = Value -> Options -> Value
forall a b. a -> b -> a
const (Value -> Options -> Value)
-> (Double -> Value) -> Double -> Options -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> Value
forall a. ToJSON a => a -> Value
A.toJSON
toEncodingPB :: Double -> Options -> Encoding
toEncodingPB = Encoding -> Options -> Encoding
forall a b. a -> b -> a
const (Encoding -> Options -> Encoding)
-> (Double -> Encoding) -> Double -> Options -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> Encoding
forall a. ToJSON a => a -> Encoding
A.toEncoding
instance FromJSONPB Double where
parseJSONPB :: Value -> Parser Double
parseJSONPB = [Char] -> Value -> Parser Double
forall a.
(FromJSON a, FromJSONKey a) =>
[Char] -> Value -> Parser a
parseFP [Char]
"double"
instance ToJSONPB TL.Text where
toJSONPB :: Text -> Options -> Value
toJSONPB = Value -> Options -> Value
forall a b. a -> b -> a
const (Value -> Options -> Value)
-> (Text -> Value) -> Text -> Options -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Value
forall a. ToJSON a => a -> Value
A.toJSON
toEncodingPB :: Text -> Options -> Encoding
toEncodingPB = Encoding -> Options -> Encoding
forall a b. a -> b -> a
const (Encoding -> Options -> Encoding)
-> (Text -> Encoding) -> Text -> Options -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Encoding
forall a. ToJSON a => a -> Encoding
A.toEncoding
instance FromJSONPB TL.Text where
parseJSONPB :: Value -> Parser Text
parseJSONPB = Value -> Parser Text
forall a. FromJSON a => Value -> Parser a
A.parseJSON
instance ToJSONPB T.Text where
toJSONPB :: Text -> Options -> Value
toJSONPB = Value -> Options -> Value
forall a b. a -> b -> a
const (Value -> Options -> Value)
-> (Text -> Value) -> Text -> Options -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Value
forall a. ToJSON a => a -> Value
A.toJSON
toEncodingPB :: Text -> Options -> Encoding
toEncodingPB = Encoding -> Options -> Encoding
forall a b. a -> b -> a
const (Encoding -> Options -> Encoding)
-> (Text -> Encoding) -> Text -> Options -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Encoding
forall a. ToJSON a => a -> Encoding
A.toEncoding
instance FromJSONPB T.Text where
parseJSONPB :: Value -> Parser Text
parseJSONPB = Value -> Parser Text
forall a. FromJSON a => Value -> Parser a
A.parseJSON
bsToJSONPB :: BS.ByteString -> A.Value
bsToJSONPB :: ByteString -> Value
bsToJSONPB (ByteString -> Either UnicodeException Text
T.decodeUtf8' (ByteString -> Either UnicodeException Text)
-> (ByteString -> ByteString)
-> ByteString
-> Either UnicodeException Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
B64.encode -> Either UnicodeException Text
ebs) =
case Either UnicodeException Text
ebs of
Right Text
bs -> Text -> Value
forall a. ToJSON a => a -> Value
A.toJSON Text
bs
Left UnicodeException
e ->
[Char] -> Value
forall a. HasCallStack => [Char] -> a
error ([Char]
"internal: failed to encode B64-encoded bytestring: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ UnicodeException -> [Char]
forall a. Show a => a -> [Char]
show UnicodeException
e)
instance ToJSONPB BS.ByteString where
toJSONPB :: ByteString -> Options -> Value
toJSONPB ByteString
bs Options
_ = ByteString -> Value
bsToJSONPB ByteString
bs
toEncodingPB :: ByteString -> Options -> Encoding
toEncodingPB ByteString
bs Options
opts = Value -> Encoding
E.value (ByteString -> Options -> Value
forall a. ToJSONPB a => a -> Options -> Value
toJSONPB ByteString
bs Options
opts)
instance FromJSONPB BS.ByteString where
parseJSONPB :: Value -> Parser ByteString
parseJSONPB (A.String Text
b64enc) =
ByteString -> Parser ByteString
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> Parser ByteString)
-> (Text -> ByteString) -> Text -> Parser ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
B64.decodeLenient (ByteString -> ByteString)
-> (Text -> ByteString) -> Text -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
T.encodeUtf8 (Text -> Parser ByteString) -> Text -> Parser ByteString
forall a b. (a -> b) -> a -> b
$ Text
b64enc
parseJSONPB Value
v = [Char] -> Value -> Parser ByteString
forall a. [Char] -> Value -> Parser a
A.typeMismatch [Char]
"bytes" Value
v
instance ToJSONPB a => ToJSONPB (V.Vector a) where
toJSONPB :: Vector a -> Options -> Value
toJSONPB Vector a
v Options
opts = Array -> Value
A.Array ((a -> Value) -> Vector a -> Array
forall a b. (a -> b) -> Vector a -> Vector b
V.map (\a
x -> a -> Options -> Value
forall a. ToJSONPB a => a -> Options -> Value
toJSONPB a
x Options
opts) Vector a
v)
toEncodingPB :: Vector a -> Options -> Encoding
toEncodingPB Vector a
v Options
opts = (a -> Encoding) -> [a] -> Encoding
forall a. (a -> Encoding) -> [a] -> Encoding
E.list (\a
x -> a -> Options -> Encoding
forall a. ToJSONPB a => a -> Options -> Encoding
toEncodingPB a
x Options
opts) (Vector a -> [a]
forall a. Vector a -> [a]
V.toList Vector a
v)
instance FromJSONPB a => FromJSONPB (V.Vector a) where
parseJSONPB :: Value -> Parser (Vector a)
parseJSONPB (A.Array Array
vs) = (Value -> Parser a) -> Array -> Parser (Vector a)
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Value -> Parser a
forall a. FromJSONPB a => Value -> Parser a
parseJSONPB Array
vs
parseJSONPB Value
A.Null = Vector a -> Parser (Vector a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
parseJSONPB Value
v = [Char] -> Value -> Parser (Vector a)
forall a. [Char] -> Value -> Parser a
A.typeMismatch [Char]
"repeated" Value
v
instance ToJSONPB a => ToJSONPB (Maybe a) where
toJSONPB :: Maybe a -> Options -> Value
toJSONPB Maybe a
mx Options
opts = Value -> (a -> Value) -> Maybe a -> Value
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Value
A.Null (\a
x -> a -> Options -> Value
forall a. ToJSONPB a => a -> Options -> Value
toJSONPB a
x Options
opts) Maybe a
mx
toEncodingPB :: Maybe a -> Options -> Encoding
toEncodingPB Maybe a
mx Options
opts = Encoding -> (a -> Encoding) -> Maybe a -> Encoding
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Encoding
E.null_ (\a
x -> a -> Options -> Encoding
forall a. ToJSONPB a => a -> Options -> Encoding
toEncodingPB a
x Options
opts) Maybe a
mx
instance FromJSONPB a => FromJSONPB (Maybe a) where
parseJSONPB :: Value -> Parser (Maybe a)
parseJSONPB Value
A.Null = Maybe a -> Parser (Maybe a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe a
forall a. Maybe a
Nothing
parseJSONPB Value
v = (a -> Maybe a) -> Parser a -> Parser (Maybe a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Maybe a
forall a. a -> Maybe a
Just (Value -> Parser a
forall a. FromJSONPB a => Value -> Parser a
parseJSONPB Value
v)
instance ToJSONPB a => ToJSONPB [a] where
toJSONPB :: [a] -> Options -> Value
toJSONPB [a]
xs Options
opts = Array -> Value
A.Array (Array -> Value) -> Array -> Value
forall a b. (a -> b) -> a -> b
$ [Value] -> Array
forall a. [a] -> Vector a
V.fromList ((a -> Value) -> [a] -> [Value]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\a
x -> a -> Options -> Value
forall a. ToJSONPB a => a -> Options -> Value
toJSONPB a
x Options
opts) [a]
xs)
toEncodingPB :: [a] -> Options -> Encoding
toEncodingPB [a]
xs Options
opts = (a -> Encoding) -> [a] -> Encoding
forall a. (a -> Encoding) -> [a] -> Encoding
E.list (\a
x -> a -> Options -> Encoding
forall a. ToJSONPB a => a -> Options -> Encoding
toEncodingPB a
x Options
opts) [a]
xs
instance FromJSONPB a => FromJSONPB [a] where
parseJSONPB :: Value -> Parser [a]
parseJSONPB (A.Array Array
xs) = (Value -> Parser a) -> [Value] -> Parser [a]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Value -> Parser a
forall a. FromJSONPB a => Value -> Parser a
parseJSONPB (Array -> [Value]
forall a. Vector a -> [a]
V.toList Array
xs)
parseJSONPB Value
A.Null = [a] -> Parser [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
parseJSONPB Value
v = [Char] -> Value -> Parser [a]
forall a. [Char] -> Value -> Parser a
A.typeMismatch [Char]
"repeated" Value
v
instance FieldDefault [a] where
fieldDefault :: [a]
fieldDefault = [a]
forall a. Monoid a => a
mempty
instance FieldDefault (Maybe a) where
fieldDefault :: Maybe a
fieldDefault = Maybe a
forall a. Maybe a
Nothing