{-# LANGUAGE FlexibleInstances, OverloadedLists, RecordWildCards, ScopedTypeVariables, 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.Text (Text)
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 :: 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 :: ByteString -> Either String a
eitherDecode =
Either (JSONPath, String) a -> Either String a
forall b. Either (JSONPath, String) b -> Either String b
eitherFormatError (Either (JSONPath, String) a -> Either String a)
-> (ByteString -> Either (JSONPath, String) a)
-> ByteString
-> Either String a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser Value
-> (Value -> IResult a)
-> ByteString
-> Either (JSONPath, String) a
forall a.
Parser Value
-> (Value -> IResult a)
-> ByteString
-> Either (JSONPath, String) 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, String) b -> Either String b
eitherFormatError = ((JSONPath, String) -> Either String b)
-> (b -> Either String b)
-> Either (JSONPath, String) b
-> Either String b
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> Either String b
forall a b. a -> Either a b
Left (String -> Either String b)
-> ((JSONPath, String) -> String)
-> (JSONPath, String)
-> Either String b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (JSONPath -> String -> String) -> (JSONPath, String) -> String
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry JSONPath -> String -> String
A.formatError) b -> Either String 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 :: Text -> v -> Options -> Series
pair Text
k v
v Options
opts = Text -> Encoding -> Series
E.pair Text
k (v -> Options -> Encoding
forall a. ToJSONPB a => a -> Options -> Encoding
toEncodingPB v
v Options
opts)
instance KeyValuePB [A.Pair] where
pair :: Text -> v -> Options -> [Pair]
pair Text
k v
v Options
opts = Pair -> [Pair]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (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 .= :: Text -> v -> Options -> kvp
.= v
v = Options -> kvp
forall p. KeyValuePB p => Options -> p
mk
where
mk :: Options -> p
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 = p
forall a. Monoid a => a
mempty
| Bool
otherwise = Text -> v -> Options -> p
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 .: :: Object -> Text -> Parser a
.: Text
key = Object
obj Object -> Text -> Parser (Maybe a)
.:? 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 -> Text -> Parser (Maybe a)
(.:?) = (Value -> Parser a) -> Object -> Text -> Parser (Maybe a)
forall a. (Value -> Parser a) -> Object -> Text -> 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 :: Object -> Text -> Parser a
parseField = (Value -> Parser a) -> Object -> Text -> Parser a
forall a. (Value -> Parser a) -> Object -> Text -> Parser a
A.explicitParseField Value -> Parser a
forall a. FromJSONPB a => Value -> Parser a
parseJSONPB
data Options =
Options
{ Options -> Bool
optEmitDefaultValuedFields :: Bool
}
deriving (Int -> Options -> String -> String
[Options] -> String -> String
Options -> String
(Int -> Options -> String -> String)
-> (Options -> String)
-> ([Options] -> String -> String)
-> Show Options
forall a.
(Int -> a -> String -> String)
-> (a -> String) -> ([a] -> String -> String) -> Show a
showList :: [Options] -> String -> String
$cshowList :: [Options] -> String -> String
show :: Options -> String
$cshow :: Options -> String
showsPrec :: Int -> Options -> String -> String
$cshowsPrec :: Int -> Options -> String -> String
Show)
defaultOptions :: Options
defaultOptions :: Options
defaultOptions = Options :: Bool -> Options
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 :: 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 :: 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 :: String -> Value -> Parser a
parseFP String
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
_ -> String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"internal: parseKeyPB: unexpected FromJSONKey summand"
Value
_ -> String -> Value -> Parser a
forall a. String -> Value -> Parser a
A.typeMismatch String
tyDesc Value
v
parseNumOrDecimalString :: (A.FromJSON a) => String -> A.Value -> A.Parser a
parseNumOrDecimalString :: String -> Value -> Parser a
parseNumOrDecimalString String
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 ->
(String -> Parser a)
-> (a -> Parser a) -> Either String a -> Parser a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either String a -> Parser a)
-> (Text -> Either String a) -> Text -> Parser a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either String a
forall a. FromJSON a => ByteString -> Either String a
A.eitherDecode (ByteString -> Either String a)
-> (Text -> ByteString) -> Text -> Either String 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
_ -> String -> Value -> Parser a
forall a. String -> Value -> Parser a
A.typeMismatch String
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 = String -> Value -> Parser Int32
forall a. FromJSON a => String -> Value -> Parser a
parseNumOrDecimalString String
"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 = String -> Value -> Parser Word32
forall a. FromJSON a => String -> Value -> Parser a
parseNumOrDecimalString String
"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
. String -> Text
T.pack (String -> Text) -> (Int64 -> String) -> Int64 -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int64 -> String
forall a. Show a => a -> String
show (Int64 -> Value) -> Int64 -> Value
forall a b. (a -> b) -> a -> b
$ Int64
x
toEncodingPB :: Int64 -> Options -> Encoding
toEncodingPB Int64
x Options
_ = String -> Encoding
forall a. String -> Encoding' a
E.string (Int64 -> String
forall a. Show a => a -> String
show Int64
x)
instance FromJSONPB Int64 where
parseJSONPB :: Value -> Parser Int64
parseJSONPB = String -> Value -> Parser Int64
forall a. FromJSON a => String -> Value -> Parser a
parseNumOrDecimalString String
"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
. String -> Text
T.pack (String -> Text) -> (Word64 -> String) -> Word64 -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> String
forall a. Show a => a -> String
show (Word64 -> Value) -> Word64 -> Value
forall a b. (a -> b) -> a -> b
$ Word64
x
toEncodingPB :: Word64 -> Options -> Encoding
toEncodingPB Word64
x Options
_ = String -> Encoding
forall a. String -> Encoding' a
E.string (Word64 -> String
forall a. Show a => a -> String
show Word64
x)
instance FromJSONPB Word64 where
parseJSONPB :: Value -> Parser Word64
parseJSONPB = String -> Value -> Parser Word64
forall a. FromJSON a => String -> Value -> Parser a
parseNumOrDecimalString String
"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 = String -> Value -> Parser Float
forall a.
(FromJSON a, FromJSONKey a) =>
String -> Value -> Parser a
parseFP String
"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 = String -> Value -> Parser Double
forall a.
(FromJSON a, FromJSONKey a) =>
String -> Value -> Parser a
parseFP String
"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 ->
String -> Value
forall a. HasCallStack => String -> a
error (String
"internal: failed to encode B64-encoded bytestring: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ UnicodeException -> String
forall a. Show a => a -> String
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 = String -> Value -> Parser ByteString
forall a. String -> Value -> Parser a
A.typeMismatch String
"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 = String -> Value -> Parser (Vector a)
forall a. String -> Value -> Parser a
A.typeMismatch String
"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 = String -> Value -> Parser [a]
forall a. String -> Value -> Parser a
A.typeMismatch String
"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