Hello, I have problem with serialization of data having optional fields.
When I pass null in corresponding field it works but when it is non-null
then serialization fails.

Schema:

{

                "type": "record",

                "name": "schema",

                "namespace": "space",

                "fields": [

                               {

                                               "name": "username",

                                               "type": "string"

                               },

                               {

                                               "name": "data",

                                               "type": [

                                                               "null",

                                                               "string"

                                               ],

                                               "defaults": null

                               },

                               {

                                               "name": "timestamp",

                                               "type": "long"

                               }

                ]

}

 

Data that works:

{"username":"miguno","data":null,"timestamp": 1366150681 }

 

Data that fails:

{"username":"miguno","data":"test","timestamp": 1366150681 }

 

Should it work or I have some error in my schema? I didn't find any active
issues in jira so I guess the concept of optional fields should work just
fine, also in C++.

 

The code is:

 

    std::unique_ptr<avro::InputStream> in = avro::memoryInputStream((const
uint8_t*)&json[0], json.size()); // json is incoming data

    avro::ValidSchema validSchema;

    std::istringstream ins(schema); // schema is avro-schema

    try {

        avro::compileJsonSchema(ins, validSchema);

    }

    catch (const std::exception& e1) {

        std::string errstr = e1.what();

    }

    avro::DecoderPtr jd = avro::jsonDecoder(validSchema);

    avro::GenericDatum datum(validSchema);

    jd->init(*in);

    avro::decode(*jd, datum); //serialization with non-null data fails
somewhere inside this step

Reply via email to