Search code examples
avro

Avro java serialization as json not working correctly


I have a simple avro schema, from which I generated a java class using the avro-maven-plugin.

The avro schema is as follows:

{
    "type": "record",
    "name": "addressGeo",
    "namespace": "com.mycompany",
    "doc": "Best record address and list of geos",
    "fields": [

    {
        "name": "version",
        "type": "int",
        "default": 1,
        "doc": "version the class"
    },
    {
        "name": "eventType",
        "type": "string",
        "default": "addressGeo",
        "doc": "event type"
    },
    {
        "name": "parcelId",
        "type": "long",
        "doc": "ParcelID of the parcel. Join parcelid and sequence with ParcelInfo"
    },
    {
        "name": "geoCodes",
        "type": {"type": "array", "items": "com.mycompany.geoCode"},
        "doc": "Multiple Geocodes, with restrictions information"
    },
    {
        "name": "brfAddress",
        "type": ["null", "com.mycompany.address"],
        "doc": "Address cleansed version of BRF"
    }
    ]
}

If I construct a simple object using the builder, and serialize it using json, I get the following output:

{
  "version": 1,
  "eventType": {
    "bytes": [
      97,
      100,
      100,
      114,
      101,
      115,
      115,
      71,
      101,
      111
    ],
    "length": 10,
    "string": null
  },
  "parcelId": 1,
  "geoCodes": [
    {
      "version": 1,
      "latitude": 1,
      "longitude": 1,
      "geoQualityCode": "g",
      "geoSourceTypeID": 1,
      "restrictions": "NONE"
    }
  ],
  "brfAddress": {
    "version": 1,
    "houseNumber": "1",
    "houseNumberFraction": null,
    "streetDirectionPrefix": null,
    "streetName": "main",
    "streetSuffix": "street",
    "streetDirectionSuffix": null,
    "fullStreetAddress": "1 main street, seattle, wa, 98101",
    "unitPrefix": null,
    "unitNumber": null,
    "city": "seattle",
    "state": "wa",
    "zipCode": "98101",
    "zipPlusFour": null,
    "addressDPV": "Y",
    "addressQualityCode": "good",
    "buildingNumber": "1",
    "carrierRoute": "t",
    "censusTract": "c",
    "censusTractAndBlock": "b",
    "dataCleanerTypeID": 1,
    "restrictions": "NONE"
  }
}

Note the output of the eventType field. It is coming through as an array of bytes whereas the type of the field is a CharSequence.

Any idea why serialization is doing this? It works fine for other types that are strings.

I am using google-gson to serialize the object to json.


Solution

  • You might be working with a older version of avro, that uses CharSequence. Ideally string type should be java String type. I would suggest to update the avro version or have a look at this one - Apache Avro: map uses CharSequence as key