Hello,
just a followup.
I now tried it with Scala and the sbt-avrohugger plugin as well, and this works
perfectly. sbt-avrohugger converts union { null, Sample } to Option[Sample],
where avrogen converts this to simply “Sample” but allows it to be null on the
C# side.
Thanks for your help,
Jens
Von: Rabe, Jens [mailto:[email protected]]
Gesendet: Dienstag, 17. Januar 2017 16:18
An: [email protected]
Betreff: AW: When using the SpecificWriter with .net, I get an exception when a
referenced object in my schema is null
Hello Jeremy,
at least with .net, this works. I can’t test with Scala currently as I am doing
work on another part of the project.
Thanks for pointing this out.
Von: Jeremy Fine [mailto:[email protected]]
Gesendet: Dienstag, 17. Januar 2017 16:12
An: [email protected]<mailto:[email protected]>
Betreff: Re: When using the SpecificWriter with .net, I get an exception when a
referenced object in my schema is null
does avdl this work? i think you need to specify that a field can be null.
@namespace("sample")
protocol SampleAvro {
record Sample {
union { null, string } foo;
union { null, Sample } bar;
}
}
On Mon, Jan 16, 2017 at 1:01 PM, Rabe, Jens
<[email protected]<mailto:[email protected]>> wrote:
Hello again,
it turns out I was wrong in first place. I re-tested it in Scala and get the
same error.
Gues I’ll have to redesign the schema then to avoid the nulls.
Von: Rabe, Jens
[mailto:[email protected]<mailto:[email protected]>]
Gesendet: Montag, 16. Januar 2017 18:13
An: [email protected]<mailto:[email protected]>
Betreff: When using the SpecificWriter with .net, I get an exception when a
referenced object in my schema is null
Hello Avro community,
I want to use my Avro serialized data with .net and I stumbled upon a strange
problem. When a record in my schema references another record, and I set it to
null, when serializing this in .net this throws an exception.
Here is what I did:
First, I created a simple protocol. This is my avdl file:
@namespace("sample")
protocol SampleAvro {
record Sample {
string foo;
Sample bar;
}
}
Now, I generated C# classes out of it:
java -jar avro-tools-1.8.0.jar idl sample.avdl >sample.json
avrogen -p sample.json .
I then copied the .cs files into a project, and tried to serialize a sample
object where the “bar” reference is null:
using(var ms = new MemoryStream()) {
var sw = new SpecificWriter<Sample>(Sample._SCHEMA);
sw.Write(new Sample { foo = "bar" }, new BinaryEncoder(ms));
}
I get the following exception: Record object is not derived from
ISpecificRecord in field bar
When looking at the source code, this happens in the SpecificWriter.cs in the
following method (I made the relevant lines bold):
protected override void WriteRecord(RecordSchema schema, object value,
Encoder encoder)
{
var rec = value as ISpecificRecord;
if (rec == null)
throw new AvroTypeException("Record object is not derived from
ISpecificRecord");
foreach (Field field in schema)
{
try
{
Write(field.Schema, rec.Get(field.Pos), encoder);
}
catch (Exception ex)
{
throw new AvroException(ex.Message + " in field " +
field.Name);
}
}
}
The “value as ISpecificRecord” returns null here because the reference is null.
This is intended in my schema, but throws an exception here.
Is this a bug or is this intended behavior, and either way, how should I work
around this? I need situations where the “bar” is null, and on the JVM/Scala
side this is no problem and working flawlessly.
Regards,
Jens
--
[Das Bild wurde vom Absender entfernt. Intent Media]<http://IntentMedia.com>
Jeremy Fine
Software Engineer
315 Hudson Street, 9th Floor, New York, NY 10013
We've been named a Crain's "Best Place to Work" for three years
running!<http://IntentMedia.com/jobs>