[ 
https://issues.apache.org/jira/browse/AVRO-3286?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17628769#comment-17628769
 ] 

Miroslav commented on AVRO-3286:
--------------------------------

Hi, I've been having the same problem and I found out that in order for the 
data to be valid, you need to put the value in another object where the key is 
the type: 

{"username":"user1","data":\{"string":"test"},"timestamp": 1366150681 }

[This is described 
here.|https://avro.apache.org/docs/1.10.2/spec.html#:~:text=the%20string%20%22a%22%20as%20%7B%22string%22%3A%20%22a%22%7D%3B%20and]

For me it doesn't feel easy it use, because of how json parsing typically works 
and I think it would be much better if it's changed to check every single data 
type in an union instead of this.

> Serialization of optional fields with C++ library doesn't work
> --------------------------------------------------------------
>
>                 Key: AVRO-3286
>                 URL: https://issues.apache.org/jira/browse/AVRO-3286
>             Project: Apache Avro
>          Issue Type: Bug
>          Components: c++
>    Affects Versions: 1.10.2
>            Reporter: Anton Kondratev
>            Priority: Critical
>
> There is a problem with serialization of data having optional fields. When I 
> pass null in corresponding field it works but when I pass non-null value then 
> serialization fails.
> Example schema:
> {code:java}
> {
>     "type": "record",
>     "name": "schema",
>     "namespace": "space",
>     "fields": [
>        {
>            "name": "username",
>            "type": "string"
>        },
>        {
>            "name": "data",
>            "type": [
>                "null",
>                "string"
>            ],
>            "defaults": null
>        },
>        {
>            "name": "timestamp",
>            "type": "long"
>        }
>     ]
> }{code}
> Data that works:
> {"username":"user1","data":null,"timestamp": 1366150681 }
> Data that fails:
> {"username":"user1","data":"test","timestamp": 1366150681 }
> My code:
> {code:java}
> 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{code}
>  



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to