2
2
3
3
import java .io .IOException ;
4
4
import java .math .BigDecimal ;
5
+ import java .math .BigInteger ;
5
6
import java .util .ArrayList ;
6
7
import java .util .List ;
7
8
10
11
import org .apache .avro .generic .GenericData ;
11
12
import org .apache .avro .generic .GenericDatumWriter ;
12
13
import org .apache .avro .io .Encoder ;
13
- import org .apache .avro .reflect .Stringable ;
14
14
15
15
import com .fasterxml .jackson .dataformat .avro .schema .AvroSchemaHelper ;
16
16
23
23
public class NonBSGenericDatumWriter <D >
24
24
extends GenericDatumWriter <D >
25
25
{
26
- private static final GenericData GENERIC_DATA = GenericData .get ();
26
+ private static final GenericData GENERIC_DATA = GenericData .get ();
27
27
28
- public NonBSGenericDatumWriter (Schema root ) {
29
- super (root );
30
- }
28
+ public NonBSGenericDatumWriter (Schema root ) {
29
+ super (root );
30
+ }
31
31
32
32
@ Override
33
33
public int resolveUnion (Schema union , Object datum ) {
@@ -54,7 +54,7 @@ public int resolveUnion(Schema union, Object datum) {
54
54
// Avro distinguishes between String and char[], whereas Jackson doesn't
55
55
// Check if the schema is expecting a char[] and handle appropriately
56
56
if (s .getElementType ().getType () == Type .INT && Character .class
57
- .getName ().equals (s .getElementType ().getProp (AvroSchemaHelper .AVRO_SCHEMA_PROP_CLASS ))) {
57
+ .getName ().equals (s .getElementType ().getProp (AvroSchemaHelper .AVRO_SCHEMA_PROP_CLASS ))) {
58
58
return i ;
59
59
}
60
60
break ;
@@ -81,52 +81,52 @@ public int resolveUnion(Schema union, Object datum) {
81
81
82
82
@ Override
83
83
protected void write (Schema schema , Object datum , Encoder out ) throws IOException {
84
- if (schema .getType () == Type .ENUM ) {
85
- super .write (schema , GENERIC_DATA .createEnum (datum .toString (), schema ), out );
84
+ Type t = schema .getType ();
85
+ if (t == Type .ENUM ) {
86
+ super .writeWithoutConversion (schema , GENERIC_DATA .createEnum (datum .toString (), schema ), out );
86
87
return ;
87
88
}
88
89
if (datum instanceof String ) {
89
90
String str = (String ) datum ;
90
91
final int len = str .length ();
91
- if (schema . getType () == Type .ARRAY && schema .getElementType ().getType () == Type .INT ) {
92
+ if (t == Type .ARRAY && schema .getElementType ().getType () == Type .INT ) {
92
93
ArrayList <Integer > chars = new ArrayList <>(len );
93
94
for (int i = 0 ; i < len ; ++i ) {
94
95
chars .add ((int ) str .charAt (i ));
95
96
}
96
- super .write (schema , chars , out );
97
+ super .writeWithoutConversion (schema , chars , out );
97
98
return ;
98
99
}
99
- if (len == 1 && schema . getType () == Type .INT ) {
100
- super .write (schema , (int ) str .charAt (0 ), out );
100
+ if (len == 1 && t == Type .INT ) {
101
+ super .writeWithoutConversion (schema , (int ) str .charAt (0 ), out );
101
102
return ;
102
103
}
103
104
}
104
- if (datum instanceof Number ) {
105
- Number n = (Number ) datum ;
106
- switch (schema .getType ()) {
107
- case LONG :
108
- super .write (schema , n .longValue (), out );
109
- return ;
110
- case INT :
111
- super .write (schema , n .intValue (), out );
112
- return ;
113
- case FLOAT :
114
- super .write (schema , n .floatValue (), out );
105
+ // 09-Mar-2017, tatu: BigDecimal and BigInteger written using various
106
+ // possible representations so...
107
+ if (datum instanceof BigDecimal ) {
108
+ switch (t ) {
109
+ case STRING :
110
+ super .writeWithoutConversion (schema , datum .toString (), out );
115
111
return ;
116
112
case DOUBLE :
117
- super .write (schema , n .doubleValue (), out );
113
+ super .writeWithoutConversion (schema , (( Number ) datum ) .doubleValue (), out );
118
114
return ;
115
+ default :
116
+ }
117
+ }
118
+ if (datum instanceof BigInteger ) {
119
+ switch (t ) {
119
120
case STRING :
120
- super .write (schema , datum .toString (), out );
121
+ super .writeWithoutConversion (schema , datum .toString (), out );
122
+ return ;
123
+ case LONG :
124
+ super .writeWithoutConversion (schema , ((Number ) datum ).longValue (), out );
121
125
return ;
122
126
default :
123
127
}
124
128
}
125
- // Handle stringable classes
126
- if (schema .getType () == Type .STRING && datum != null && datum .getClass ().getAnnotation (Stringable .class ) != null ) {
127
- super .write (schema , datum .toString (), out );
128
- return ;
129
- }
130
- super .write (schema , datum , out );
129
+ super .writeWithoutConversion (schema , datum , out );
130
+ // super.write(schema, datum, out);
131
131
}
132
132
}
0 commit comments