Uploaded image for project: 'Apache Avro'
  1. Apache Avro
  2. AVRO-1850

Align JSON and binary record serialization

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Open
    • Major
    • Resolution: Unresolved
    • None
    • None
    • spec

    Description

      The documentation describes the encoding of Avro records as:

      Binary: A record is encoded by encoding the values of its fields in the order that they are declared. In other words, a record is encoded as just the concatenation of the encodings of its fields. Field values are encoded per their schema.

      JSON: Except for unions, the JSON encoding is the same as is used to encode field default values.

      The field default values table says that records and maps are both encoded as JSON type object.

      Enhancement:
      There is currently no way to write an Avro schema describing a JSON array of positional parameters (i.e. an array containing variables of possibly different type). An Avro record is the datatype representing an ordered collection of values. For consistency with the binary encoding, and to allow Avro to represent a schema for JSON tuples, encoding should say:

      JSON: Except for unions and records, the JSON encoding is the same as is used to encode field default values. A record is encoded as an array by encoding the values of its fields in the order that they are declared.

      For the example schema:

      {"namespace": "example.avro",
       "type": "record",
       "name": "User",
       "fields": [
           {"name": "name", "type": "string"},
           {"name": "favorite_number",  "type": ["int", "null"]},
           {"name": "favorite_color", "type": ["string", "null"]}
       ]
      }
      

      the JSON encoding currently converts an Avro record to an Avro map (JSON object):

      {           "name": "Joe",
       "favorite_number": 42,
        "favorite_color": null  }
      

      Instead Avro records should be encoded in JSON in the same manner as they are encoded in binary, as a JSON array containing the fields in the order they are defined:

      ["Joe", 42, null]
      

      The set of JSON texts validated by the example Avro schema and by the corresponding JSON schema should be equal:

      {
        "$schema": "http://json-schema.org/draft-04/schema#",
        "type": "array",
        "name": "User",
        "items": [
          {"name":"name", "type": "string"},
          {"name":"favorite_number", "type":["integer","null"]},
          {"name":"favorite_color", "type":["string","null"]}
        ]
      }
      

      Attachments

        Activity

          People

            Unassigned Unassigned
            davek22 David Kay
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: