proto-lens-jsonpb-0.1.0.0: JSON protobuf encoding for proto-lens

Safe HaskellNone
LanguageHaskell2010

Data.ProtoLens.JSONPB.Class

Contents

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
ToJSONPB Bool Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

ToJSONPB Double Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

ToJSONPB Float Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

ToJSONPB Int32 Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

ToJSONPB Int64 Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

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

ToJSONPB Text Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

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

Defined in Data.ProtoLens.JSONPB.Class

Methods

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

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

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

Defined in Data.ProtoLens.JSONPB.Class

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

Defined in Data.ProtoLens.JSONPB.Class

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
FromJSONPB Bool Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

FromJSONPB Double Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

FromJSONPB Float Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

FromJSONPB Int32 Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

FromJSONPB Int64 Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

FromJSONPB Word32 Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

FromJSONPB Word64 Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

FromJSONPB ByteString Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

FromJSONPB Text Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

FromJSONPB Text Source # 
Instance details

Defined in Data.ProtoLens.JSONPB.Class

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

Defined in Data.ProtoLens.JSONPB.Class

Methods

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

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

Defined in Data.ProtoLens.JSONPB.Class

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

Defined in Data.ProtoLens.JSONPB.Class

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
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

JSONPB rendering and parsing options

data Options Source #

Constructors

Options 
Instances
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

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 [a] Source # 
Instance details

Methods

fieldDefault :: [a] #

FieldDefault (Maybe a) Source # 
Instance details

Methods

fieldDefault :: Maybe a #