@@ -25,61 +25,72 @@ public NonBSGenericDatumWriter(Schema root) {
25
25
super (root );
26
26
}
27
27
28
- @ Override
29
- public int resolveUnion (Schema union , Object datum ) {
30
- // Alas, we need a work-around first...
31
- if (datum == null ) {
32
- return union .getIndexNamed (Type .NULL .getName ());
33
- }
34
- List <Schema > schemas = union .getTypes ();
35
- for ( int i = 0 , len = schemas . size (); i < len ; i ++ ) {
36
- Schema s = schemas .get ( i );
37
- if ( datum instanceof BigDecimal && s . getType () == Type . DOUBLE ) {
38
- return i ;
39
- }
40
- if ( datum instanceof String ) { // String or Enum or Character or char[]
41
- switch ( s . getType ()) {
42
- case STRING :
43
- case ENUM :
44
- return i ;
45
- case INT :
46
- // Avro distinguishes between String and Character, whereas Jackson doesn't
47
- // Check if the schema is expecting a Character and handle appropriately
48
- if ( Character . class . getName (). equals ( s . getProp ( AvroSchemaHelper . AVRO_SCHEMA_PROP_CLASS ))) {
49
- return i ;
50
- }
51
- break ;
52
- case ARRAY :
53
- // Avro distinguishes between String and char[], whereas Jackson doesn't
54
- // Check if the schema is expecting a char[] and handle appropriately
55
- if ( s . getElementType (). getType () == Type . INT && Character . class
56
- . getName (). equals ( s . getElementType (). getProp ( AvroSchemaHelper . AVRO_SCHEMA_PROP_CLASS ))) {
57
- return i ;
58
- }
59
- break ;
60
- default :
61
- }
62
- }
63
- }
64
- // otherwise just default to base impl, stupid as it is...
65
- return super . resolveUnion ( union , datum );
66
- }
28
+ @ Override
29
+ public int resolveUnion (Schema union , Object datum ) {
30
+ // Alas, we need a work-around first...
31
+ if (datum == null ) {
32
+ return union .getIndexNamed (Type .NULL .getName ());
33
+ }
34
+ List <Schema > schemas = union .getTypes ();
35
+ if ( datum instanceof String ) { // String or Enum or Character or char[]
36
+ for ( int i = 0 , len = schemas .size (); i < len ; i ++) {
37
+ Schema s = schemas . get ( i );
38
+ switch ( s . getType ()) {
39
+ case STRING :
40
+ case ENUM :
41
+ return i ;
42
+ case INT :
43
+ // Avro distinguishes between String and Character, whereas Jackson doesn't
44
+ // Check if the schema is expecting a Character and handle appropriately
45
+ if ( Character . class . getName (). equals ( s . getProp ( AvroSchemaHelper . AVRO_SCHEMA_PROP_CLASS ))) {
46
+ return i ;
47
+ }
48
+ break ;
49
+ case ARRAY :
50
+ // Avro distinguishes between String and char[], whereas Jackson doesn't
51
+ // Check if the schema is expecting a char[] and handle appropriately
52
+ if ( s . getElementType (). getType () == Type . INT && Character . class
53
+ . getName (). equals ( s . getElementType (). getProp ( AvroSchemaHelper . AVRO_SCHEMA_PROP_CLASS ))) {
54
+ return i ;
55
+ }
56
+ break ;
57
+ default :
58
+ }
59
+ }
60
+ } else if ( datum instanceof BigDecimal ) {
61
+ for ( int i = 0 , len = schemas . size (); i < len ; i ++) {
62
+ if ( schemas . get ( i ). getType () == Type . DOUBLE ) {
63
+ return i ;
64
+ }
65
+ }
66
+ }
67
67
68
- @ Override
69
- protected void write (Schema schema , Object datum , Encoder out ) throws IOException {
70
- if ((schema .getType () == Type .DOUBLE ) && datum instanceof BigDecimal ) {
71
- out .writeDouble (((BigDecimal )datum ).doubleValue ());
72
- } else if (datum instanceof String && schema .getType () == Type .ARRAY && schema .getElementType ().getType () == Type .INT ) {
73
- ArrayList <Integer > chars = new ArrayList <>(((String ) datum ).length ());
74
- char [] src = ((String ) datum ).toCharArray ();
75
- for (int i = 0 ; i < src .length ; i ++) {
76
- chars .add ((int ) src [i ]);
77
- }
78
- super .write (schema , chars , out );
79
- } else if (datum instanceof String && ((String ) datum ).length () == 1 && schema .getType () == Type .INT ) {
80
- super .write (schema , (int ) ((String ) datum ).charAt (0 ), out );
81
- } else {
82
- super .write (schema , datum , out );
83
- }
84
- }
68
+ // otherwise just default to base impl, stupid as it is...
69
+ return super .resolveUnion (union , datum );
70
+ }
71
+
72
+ @ Override
73
+ protected void write (Schema schema , Object datum , Encoder out ) throws IOException {
74
+ if ((schema .getType () == Type .DOUBLE ) && datum instanceof BigDecimal ) {
75
+ out .writeDouble (((BigDecimal )datum ).doubleValue ());
76
+ return ;
77
+ }
78
+ if (datum instanceof String ) {
79
+ String str = (String ) datum ;
80
+ final int len = str .length ();
81
+ if (schema .getType () == Type .ARRAY && schema .getElementType ().getType () == Type .INT ) {
82
+ ArrayList <Integer > chars = new ArrayList <>(len );
83
+ for (int i = 0 ; i < len ; ++i ) {
84
+ chars .add ((int ) str .charAt (i ));
85
+ }
86
+ super .write (schema , chars , out );
87
+ return ;
88
+ }
89
+ if (len == 1 && schema .getType () == Type .INT ) {
90
+ super .write (schema , (int ) str .charAt (0 ), out );
91
+ return ;
92
+ }
93
+ }
94
+ super .write (schema , datum , out );
95
+ }
85
96
}
0 commit comments