avro-0.3.4.0: Avro serialization support for Haskell

Safe HaskellNone
LanguageHaskell2010

Data.Avro.JSON

Description

Avro supports a JSON representation of Avro objects alongside the Avro binary format. An Avro schema can be used to generate and validate JSON representations of Avro objects.

The JSON format is the same format as used for default values in schemas except unions are encoded differently. Non-union values are encoded as follows:

Avro Type JSON Type Example
null null null
boolean boolean true
int, long integer 1
float, double number 1.1
bytes string "u00FF"
string string "foo"
record object {"a":1}
enum string FOO
array array [1]
map object {"a":1}
fixed string "u00FF"

(Table from the Avro 1.8.2 specification: https://avro.apache.org/docs/1.8.2/spec.html#schema_record)

Bytes and fixed are encoded as JSON strings where each byte is translated into the corresponding Unicode codepoint between 0–255, which includes non-printable characters. Note that this encoding happens at the Unicode code-point level, meaning it is independent of text encoding. (JSON is, by definition, encoded in UTF8.)

Unions are encoded as an object with a single field that specifies the "branch" of the union. If the branch is a primitive type like "string", the name of the primitive type is used:

{ "string" : "foo" }

For named types (record, enum and fixed), the name of the type is used:

{ MyRecord : { ... } }
Synopsis

Documentation

fromJSON :: forall a. FromAvro a => Value -> Result a Source #

Convert a Value into a type that has an Avro schema. The schema is used to validate the JSON and will return an Error if the JSON object is not encoded correctly or does not match the schema.

parseJSON :: forall a. FromAvro a => ByteString -> Result a Source #

Parse a ByteString as JSON and convert it to a type with an Avro schema. Will return Error if the input is not valid JSON or the JSON does not convert with the specified schema.

toJSON :: forall a. ToAvro a => a -> Value Source #

Convert an object with an Avro schema to JSON using that schema.

We always need the schema to encode to JSON because representing unions requires using the names of named types.