Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Support for the JSONPB canonical JSON encoding described at https://developers.google.com/protocol-buffers/docs/proto3#json.
This modules provides Aeson
-like helper functions, typeclasses, and
instances for converting to and from values of types which have a JSONPB
representation and equivalent underlying Aeson
representations.
This module also presents a (very minimal) surface syntax for Aeson-like
operations; the idea is that we can write ToJSONPB
and FromJSONPB
instances in a very similar manner to ToJSON
and FromJSON
instances,
except that doing so specifies JSONPB codecs instead of vanilla JSON codecs.
Example use:
message Scalar32 { int32 i32 = 1; uint32 u32 = 2; sint32 s32 = 3; fixed32 f32 = 4; sfixed32 sf32 = 5; } instance ToJSONPB Scalar32 where toJSONPB (Scalar32 i32 u32 s32 f32 sf32) = object [ "i32" .= i32 , "u32" .= u32 , "s32" .= s32 , "f32" .= f32 , "sf32" .= sf32 ] toEncodingPB (Scalar32 i32 u32 s32 f32 sf32) = pairs [ "i32" .= i32 , "u32" .= u32 , "s32" .= s32 , "f32" .= f32 , "sf32" .= sf32 ] instance FromJSONPB Scalar32 where parseJSONPB = withObject Scalar32 $ obj -> pure Scalar32 * obj .: "i32" * obj .: "u32" * obj .: "s32" * obj .: "f32" * obj .: "sf32"
Synopsis
- class ToJSONPB a where
- toJSONPB :: a -> Options -> Value
- toEncodingPB :: a -> Options -> Encoding
- class FromJSONPB a where
- parseJSONPB :: Value -> Parser a
- encode :: ToJSONPB a => Options -> a -> ByteString
- eitherDecode :: FromJSONPB a => ByteString -> Either String a
- class Monoid m => KeyValuePB m where
- (.=) :: (ToJSONPB v, KeyValuePB kvp, FieldDefault v, Eq v) => Text -> v -> Options -> kvp
- (.:) :: (FromJSONPB a, FieldDefault a) => Object -> Text -> Parser a
- parseField :: FromJSONPB a => Object -> Text -> Parser a
- data Options = Options {}
- defaultOptions :: Options
- object :: [Options -> [Pair]] -> Options -> Value
- pairs :: [Options -> Series] -> Options -> Encoding
- toAesonValue :: ToJSONPB a => a -> Value
- toAesonEncoding :: ToJSONPB a => a -> Encoding
- parseFP :: (FromJSON a, FromJSONKey a) => String -> Value -> Parser a
- parseNumOrDecimalString :: FromJSON a => String -> Value -> Parser a
- bsToJSONPB :: ByteString -> Value
Typeclass definitions
class ToJSONPB a where Source #
ToJSON
variant for JSONPB direct encoding via Encoding
toJSONPB :: a -> Options -> Value Source #
toJSON
variant for JSONPB encoders.
toEncodingPB :: a -> Options -> Encoding Source #
toEncoding
variant for JSONPB encoders. If an implementation is not
provided, uses toJSONPB
(which is less efficient since it indirects
through the Value
IR).
Instances
ToJSONPB Int32 Source # | |
ToJSONPB Int64 Source # | |
ToJSONPB Word32 Source # | |
ToJSONPB Word64 Source # | |
ToJSONPB ByteString Source # | |
Defined in Data.ProtoLens.JSONPB.Class toJSONPB :: ByteString -> Options -> Value Source # toEncodingPB :: ByteString -> Options -> Encoding Source # | |
ToJSONPB Text Source # | |
ToJSONPB Text Source # | |
ToJSONPB Bool Source # | |
ToJSONPB Double Source # | |
ToJSONPB Float Source # | |
ToJSONPB a => ToJSONPB (Vector a) Source # | |
Defined in Data.ProtoLens.JSONPB.Class | |
ToJSONPB a => ToJSONPB (Maybe a) Source # | |
ToJSONPB a => ToJSONPB [a] Source # | |
Defined in Data.ProtoLens.JSONPB.Class |
class FromJSONPB a where Source #
parseJSONPB :: Value -> Parser a Source #
parseJSON
variant for JSONPB decoders.
Instances
JSONPB codec entry points
encode :: ToJSONPB a => Options -> a -> ByteString Source #
encode
variant for serializing a JSONPB value as a lazy
ByteString
.
eitherDecode :: FromJSONPB a => ByteString -> Either String a Source #
eitherDecode
variant for deserializing a JSONPB value from a
lazy ByteString
.
Operator definitions
class Monoid m => KeyValuePB m where Source #
JSONPB-encoded monoidal key-value pairs
Instances
KeyValuePB Series Source # | |
KeyValuePB [Pair] Source # | |
(.:) :: (FromJSONPB a, FieldDefault a) => Object -> Text -> Parser a Source #
.:
variant for JSONPB; if the given key is missing from the
object, or if it is present but its value is null, we produce the default
protobuf value for the field type
parseField :: FromJSONPB a => Object -> Text -> Parser a Source #
JSONPB rendering and parsing options
defaultOptions :: Options Source #
Default options for JSONPB encoding. By default, all options are False
.
Helper types and functions
toAesonValue :: ToJSONPB a => a -> Value Source #
toAesonEncoding :: ToJSONPB a => a -> Encoding Source #
A direct Encoding
for values which can be JSONPB-encoded
parseFP :: (FromJSON a, FromJSONKey a) => String -> Value -> Parser a Source #
Parse a JSONPB floating point value; first parameter provides context for type mismatches
parseNumOrDecimalString :: FromJSON a => String -> Value -> Parser a Source #
Liberally parse an integer value (e.g. 42 or "42" as 42); first parameter provides context for type mismatches
Common instances for jsonpb codec implementations
Instances for scalar types
bsToJSONPB :: ByteString -> Value Source #
Instances for composite types
Orphan instances
FieldDefault (Maybe a) Source # | |
fieldDefault :: Maybe a | |
FieldDefault [a] Source # | |
fieldDefault :: [a] |