22
33import java .io .IOException ;
44import java .math .BigDecimal ;
5+ import java .math .BigInteger ;
56import java .util .ArrayList ;
67import java .util .List ;
78
1011import org .apache .avro .generic .GenericData ;
1112import org .apache .avro .generic .GenericDatumWriter ;
1213import org .apache .avro .io .Encoder ;
13- import org .apache .avro .reflect .Stringable ;
1414
1515import com .fasterxml .jackson .dataformat .avro .schema .AvroSchemaHelper ;
1616
2323public class NonBSGenericDatumWriter <D >
2424 extends GenericDatumWriter <D >
2525{
26- private static final GenericData GENERIC_DATA = GenericData .get ();
26+ private static final GenericData GENERIC_DATA = GenericData .get ();
2727
28- public NonBSGenericDatumWriter (Schema root ) {
29- super (root );
30- }
28+ public NonBSGenericDatumWriter (Schema root ) {
29+ super (root );
30+ }
3131
3232 @ Override
3333 public int resolveUnion (Schema union , Object datum ) {
@@ -54,7 +54,7 @@ public int resolveUnion(Schema union, Object datum) {
5454 // Avro distinguishes between String and char[], whereas Jackson doesn't
5555 // Check if the schema is expecting a char[] and handle appropriately
5656 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 ))) {
5858 return i ;
5959 }
6060 break ;
@@ -81,52 +81,52 @@ public int resolveUnion(Schema union, Object datum) {
8181
8282 @ Override
8383 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 );
8687 return ;
8788 }
8889 if (datum instanceof String ) {
8990 String str = (String ) datum ;
9091 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 ) {
9293 ArrayList <Integer > chars = new ArrayList <>(len );
9394 for (int i = 0 ; i < len ; ++i ) {
9495 chars .add ((int ) str .charAt (i ));
9596 }
96- super .write (schema , chars , out );
97+ super .writeWithoutConversion (schema , chars , out );
9798 return ;
9899 }
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 );
101102 return ;
102103 }
103104 }
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 );
115111 return ;
116112 case DOUBLE :
117- super .write (schema , n .doubleValue (), out );
113+ super .writeWithoutConversion (schema , (( Number ) datum ) .doubleValue (), out );
118114 return ;
115+ default :
116+ }
117+ }
118+ if (datum instanceof BigInteger ) {
119+ switch (t ) {
119120 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 );
121125 return ;
122126 default :
123127 }
124128 }
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);
131131 }
132132}
0 commit comments