Skip to content

Commit b8d8582

Browse files
committed
minor stylistic/micro-perf tweaking
1 parent ca58cfc commit b8d8582

File tree

4 files changed

+42
-32
lines changed

4 files changed

+42
-32
lines changed

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,21 +85,24 @@ public Boolean hasRequiredMarker(AnnotatedMember m) {
8585

8686
@Override
8787
public JsonCreator.Mode findCreatorAnnotation(MapperConfig<?> config, Annotated a) {
88-
AnnotatedConstructor constructor = a instanceof AnnotatedConstructor ? (AnnotatedConstructor) a : null;
89-
AnnotatedClass parentClass =
90-
a instanceof AnnotatedConstructor && ((AnnotatedConstructor) a).getTypeContext() instanceof AnnotatedClass
91-
? (AnnotatedClass) ((AnnotatedConstructor) a).getTypeContext()
92-
: null;
93-
if (constructor != null && parentClass != null && parentClass.hasAnnotation(Stringable.class)
94-
&& constructor.getParameterCount() == 1 && String.class.equals(constructor.getRawParameterType(0))) {
95-
return JsonCreator.Mode.DELEGATING;
88+
if (a instanceof AnnotatedConstructor) {
89+
AnnotatedConstructor constructor = (AnnotatedConstructor) a;
90+
// 09-Mar-2017, tatu: Ideally would allow mix-ins etc, but for now let's take
91+
// a short-cut here:
92+
Class<?> declClass = constructor.getDeclaringClass();
93+
if (declClass.getAnnotation(Stringable.class) != null) {
94+
if (constructor.getParameterCount() == 1
95+
&& String.class.equals(constructor.getRawParameterType(0))) {
96+
return JsonCreator.Mode.DELEGATING;
97+
}
98+
}
9699
}
97100
return null;
98101
}
99102

100103
@Override
101104
public Object findSerializer(Annotated a) {
102-
if (a instanceof AnnotatedClass && a.hasAnnotation(Stringable.class)) {
105+
if (a.hasAnnotation(Stringable.class)) {
103106
return ToStringSerializer.class;
104107
}
105108
return null;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public AvroModule()
3333
{
3434
super(PackageVersion.VERSION);
3535
addSerializer(new SchemaSerializer());
36+
// 09-Mar-2017, tatu: As per [dataformats-binary#57], require simple serialization?
3637
addSerializer(File.class, new ToStringSerializer(File.class));
3738
// 08-Mar-2016, tatu: to fix [dataformat-avro#35], need to prune 'schema' property:
3839
setSerializerModifier(new AvroSerializerModifier());

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

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ public int resolveUnion(Schema union, Object datum) {
6666
for (int i = 0, len = schemas.size(); i < len; i++) {
6767
if (schemas.get(i).getType() == Type.STRING) {
6868
return i;
69-
} else if (schemas.get(i).getType() == Type.DOUBLE) {
69+
}
70+
if (schemas.get(i).getType() == Type.DOUBLE) {
7071
subOptimal = i;
7172
}
7273
}
@@ -80,47 +81,51 @@ public int resolveUnion(Schema union, Object datum) {
8081

8182
@Override
8283
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);
86+
return;
87+
}
88+
if (datum instanceof String) {
89+
String str = (String) datum;
90+
final int len = str.length();
91+
if (schema.getType() == Type.ARRAY && schema.getElementType().getType() == Type.INT) {
92+
ArrayList<Integer> chars = new ArrayList<>(len);
93+
for (int i = 0; i < len; ++i) {
94+
chars.add((int) str.charAt(i));
95+
}
96+
super.write(schema, chars, out);
97+
return;
98+
}
99+
if (len == 1 && schema.getType() == Type.INT) {
100+
super.write(schema, (int) str.charAt(0), out);
101+
return;
102+
}
103+
}
83104
if (datum instanceof Number) {
105+
Number n = (Number) datum;
84106
switch (schema.getType()) {
85107
case LONG:
86-
super.write(schema, (((Number) datum).longValue()), out);
108+
super.write(schema, n.longValue(), out);
87109
return;
88110
case INT:
89-
super.write(schema, (((Number) datum).intValue()), out);
111+
super.write(schema, n.intValue(), out);
90112
return;
91113
case FLOAT:
92-
super.write(schema, (((Number) datum).floatValue()), out);
114+
super.write(schema, n.floatValue(), out);
93115
return;
94116
case DOUBLE:
95-
super.write(schema, (((Number) datum).doubleValue()), out);
117+
super.write(schema, n.doubleValue(), out);
96118
return;
97119
case STRING:
98120
super.write(schema, datum.toString(), out);
99121
return;
122+
default:
100123
}
101124
}
102125
// Handle stringable classes
103126
if (schema.getType() == Type.STRING && datum != null && datum.getClass().getAnnotation(Stringable.class) != null) {
104127
super.write(schema, datum.toString(), out);
105128
return;
106-
} else if (schema.getType() == Type.ENUM) {
107-
super.write(schema, GENERIC_DATA.createEnum(datum.toString(), schema), out);
108-
return;
109-
} else if (datum instanceof String) {
110-
String str = (String) datum;
111-
final int len = str.length();
112-
if (schema.getType() == Type.ARRAY && schema.getElementType().getType() == Type.INT) {
113-
ArrayList<Integer> chars = new ArrayList<>(len);
114-
for (int i = 0; i < len; ++i) {
115-
chars.add((int) str.charAt(i));
116-
}
117-
super.write(schema, chars, out);
118-
return;
119-
}
120-
if (len == 1 && schema.getType() == Type.INT) {
121-
super.write(schema, (int) str.charAt(0), out);
122-
return;
123-
}
124129
}
125130
super.write(schema, datum, out);
126131
}

release-notes/VERSION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Modules:
1515
#14 (avro): Add support for Avro annotations via `AvroAnnotationIntrospector`
1616
(contributed by baharclerode@github)
1717
#15 (avro): Add a way to produce "file" style Avro output
18+
#57 (avro): Add support for @Stringable annotation
1819
- [avro] Upgrade `avro-core` dep from 1.7.7 to 1.8.1
1920

2021
2.8.8 (not yet released)

0 commit comments

Comments
 (0)