proto-lens-jsonpb-0.2.2: JSON protobuf encoding for proto-lens
Safe HaskellSafe-Inferred
LanguageHaskell2010

Data.ProtoLens.JSONPB.Class

Description

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

Typeclass definitions

class ToJSONPB a where Source #

ToJSON variant for JSONPB direct encoding via Encoding

Minimal complete definition

toJSONPB

Methods

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

Instances details
ToJSONPB Int32 Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

Methods

toJSONPB :: Int32 -> Options -> Value Source #

toEncodingPB :: Int32 -> Options -> Encoding Source #

ToJSONPB Int64 Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

Methods

toJSONPB :: Int64 -> Options -> Value Source #

toEncodingPB :: Int64 -> Options -> Encoding Source #

ToJSONPB Word32 Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

ToJSONPB Word64 Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

ToJSONPB ByteString Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

ToJSONPB Text Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

Methods

toJSONPB :: Text -> Options -> Value Source #

toEncodingPB :: Text -> Options -> Encoding Source #

ToJSONPB Text Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

Methods

toJSONPB :: Text -> Options -> Value Source #

toEncodingPB :: Text -> Options -> Encoding Source #

ToJSONPB Bool Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

Methods

toJSONPB :: Bool -> Options -> Value Source #

toEncodingPB :: Bool -> Options -> Encoding Source #

ToJSONPB Double Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

ToJSONPB Float Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

Methods

toJSONPB :: Float -> Options -> Value Source #

toEncodingPB :: Float -> Options -> Encoding Source #

ToJSONPB a => ToJSONPB (Vector a) Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

Methods

toJSONPB :: Vector a -> Options -> Value Source #

toEncodingPB :: Vector a -> Options -> Encoding Source #

ToJSONPB a => ToJSONPB (Maybe a) Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

Methods

toJSONPB :: Maybe a -> Options -> Value Source #

toEncodingPB :: Maybe a -> Options -> Encoding Source #

ToJSONPB a => ToJSONPB [a] Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

Methods

toJSONPB :: [a] -> Options -> Value Source #

toEncodingPB :: [a] -> Options -> Encoding Source #

class FromJSONPB a where Source #

FromJSON variant for JSONPB decoding from the Value IR

Methods

parseJSONPB :: Value -> Parser a Source #

parseJSON variant for JSONPB decoders.

Instances

Instances details
FromJSONPB Int32 Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

Methods

parseJSONPB :: Value -> Parser Int32 Source #

FromJSONPB Int64 Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

Methods

parseJSONPB :: Value -> Parser Int64 Source #

FromJSONPB Word32 Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

Methods

parseJSONPB :: Value -> Parser Word32 Source #

FromJSONPB Word64 Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

Methods

parseJSONPB :: Value -> Parser Word64 Source #

FromJSONPB ByteString Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

Methods

parseJSONPB :: Value -> Parser ByteString Source #

FromJSONPB Text Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

Methods

parseJSONPB :: Value -> Parser Text Source #

FromJSONPB Text Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

Methods

parseJSONPB :: Value -> Parser Text Source #

FromJSONPB Bool Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

Methods

parseJSONPB :: Value -> Parser Bool Source #

FromJSONPB Double Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

Methods

parseJSONPB :: Value -> Parser Double Source #

FromJSONPB Float Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

Methods

parseJSONPB :: Value -> Parser Float Source #

FromJSONPB a => FromJSONPB (Vector a) Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

Methods

parseJSONPB :: Value -> Parser (Vector a) Source #

FromJSONPB a => FromJSONPB (Maybe a) Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

Methods

parseJSONPB :: Value -> Parser (Maybe a) Source #

FromJSONPB a => FromJSONPB [a] Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

Methods

parseJSONPB :: Value -> Parser [a] Source #

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

Methods

pair :: ToJSONPB v => Text -> v -> Options -> m Source #

Instances

Instances details
KeyValuePB Series Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

Methods

pair :: ToJSONPB v => Text -> v -> Options -> Series Source #

KeyValuePB [Pair] Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

Methods

pair :: ToJSONPB v => Text -> v -> Options -> [Pair] Source #

(.=) :: (ToJSONPB v, KeyValuePB kvp, FieldDefault v, Eq v) => Text -> v -> Options -> kvp Source #

Construct a monoidal key-value pair, using mempty to represent omission of default values (unless the given Options force their emission).

(.:) :: (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

data Options Source #

Constructors

Options 

Instances

Instances details
Show Options Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

defaultOptions :: Options Source #

Default options for JSONPB encoding. By default, all options are False.

Helper types and functions

object :: [Options -> [Pair]] -> Options -> Value Source #

pairs :: [Options -> Series] -> Options -> Encoding Source #

toAesonValue :: ToJSONPB a => a -> Value Source #

A Aeson Value encoder for values which can be JSONPB-encoded

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

Instances for composite types

Orphan instances

FieldDefault (Maybe a) Source # 
Instance details

Methods

fieldDefault :: Maybe a

FieldDefault [a] Source # 
Instance details

Methods

fieldDefault :: [a]