Skip to content

Commit d174718

Browse files
committed
Start work for supporting root values: serialization works, deser next
1 parent 14661fb commit d174718

File tree

4 files changed

+42
-16
lines changed

4 files changed

+42
-16
lines changed

avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroGenerator.java

+18-11
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,15 @@ private Feature(boolean defaultState) {
9999
protected int _formatFeatures;
100100

101101
protected AvroSchema _rootSchema;
102-
102+
103103
/*
104104
/**********************************************************
105105
/* Output state
106106
/**********************************************************
107107
*/
108108

109109
final protected OutputStream _output;
110-
110+
111111
/**
112112
* Reference to the root context since that is needed for serialization
113113
*/
@@ -118,12 +118,17 @@ private Feature(boolean defaultState) {
118118
*/
119119
protected AvroWriteContext _avroContext;
120120

121+
/**
122+
* Lazily constructed encoder; reused in case of writing root-value sequences.
123+
*/
124+
protected BinaryEncoder _encoder;
125+
121126
/**
122127
* Flag that is set when the whole content is complete, can
123128
* be output.
124129
*/
125130
protected boolean _complete;
126-
131+
127132
/*
128133
/**********************************************************
129134
/* Life-cycle
@@ -356,6 +361,7 @@ public void close() throws IOException
356361
@Override
357362
public final void writeStartArray() throws IOException {
358363
_avroContext = _avroContext.createChildArrayContext();
364+
_complete = false;
359365
}
360366

361367
@Override
@@ -373,6 +379,7 @@ public final void writeEndArray() throws IOException
373379
@Override
374380
public final void writeStartObject() throws IOException {
375381
_avroContext = _avroContext.createChildObjectContext();
382+
_complete = false;
376383
}
377384

378385
@Override
@@ -578,18 +585,18 @@ protected void _releaseBuffers() {
578585
protected void _complete() throws IOException
579586
{
580587
_complete = true;
581-
588+
582589
// add defensive coding here but only because this often gets triggered due
583590
// to forced closure resulting from another exception; so, we typically
584591
// do not want to hide the original problem...
585-
// First one sanity check, for a (relatively?) common case
592+
// First one sanity check, for a (relatively?) common case
586593
if (_rootContext == null) {
587-
return;
594+
return;
595+
}
596+
if (_encoder == null) {
597+
_encoder = AvroSchema.encoder(_output, isEnabled(Feature.AVRO_BUFFERING));
588598
}
589-
BinaryEncoder encoder = AvroSchema.encoder(
590-
_output, isEnabled(Feature.AVRO_BUFFERING)
591-
);
592-
_rootContext.complete(encoder);
593-
encoder.flush();
599+
_rootContext.complete(_encoder);
600+
_encoder.flush();
594601
}
595602
}

avro/src/main/java/com/fasterxml/jackson/dataformat/avro/ser/NonBSGenericDatumWriter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* Need to sub-class to prevent encoder from crapping on writing an optional
1414
* Enum value (see [dataformat-avro#12])
1515
*
16-
* @since 2.5.0
16+
* @since 2.5
1717
*/
1818
public class NonBSGenericDatumWriter<D>
1919
extends GenericDatumWriter<D>

avro/src/main/java/com/fasterxml/jackson/dataformat/avro/ser/RootContext.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,18 @@ public void writeValue(Object value) {
7272
public void writeString(String value) {
7373
_reportError();
7474
}
75-
75+
76+
/**
77+
* Lazily created instance for encoding: reused in case of root value sequences.
78+
*/
79+
private NonBSGenericDatumWriter<GenericContainer> _writer;
80+
7681
@Override
7782
public void complete(BinaryEncoder encoder) throws IOException {
78-
new NonBSGenericDatumWriter<GenericContainer>(_schema).write(_rootValue, encoder);
83+
if (_writer == null) {
84+
_writer = new NonBSGenericDatumWriter<GenericContainer>(_schema);
85+
}
86+
_writer.write(_rootValue, encoder);
7987
}
8088

8189
@Override

avro/src/test/java/com/fasterxml/jackson/dataformat/avro/AvroTestBase.java

+13-2
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,22 @@ public abstract class AvroTestBase extends TestCase
3535
/* Test classes
3636
/**********************************************************
3737
*/
38-
38+
3939
protected static class Employee
4040
{
4141
public Employee() { }
42-
42+
43+
public Employee(String n, int a, String[] e, Employee b) {
44+
name = n;
45+
age = a;
46+
emails = e;
47+
boss = b;
48+
}
49+
50+
public static Employee construct() {
51+
return new Employee();
52+
}
53+
4354
public String name;
4455
public int age;
4556
public String[] emails;

0 commit comments

Comments
 (0)