forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request quarkusio#26287 from quarkusio/avro-jackson
Allows Avro Specific Record classes (generated from Avro schema) to be serialized using Jackson
- Loading branch information
Showing
7 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...ns/avro/runtime/src/main/java/io/quarkus/avro/runtime/jackson/SpecificDataSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package io.quarkus.avro.runtime.jackson; | ||
|
||
import java.io.IOException; | ||
|
||
import org.apache.avro.specific.SpecificData; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import com.fasterxml.jackson.databind.ser.std.StdSerializer; | ||
|
||
public class SpecificDataSerializer extends StdSerializer<SpecificData> { | ||
public SpecificDataSerializer() { | ||
super(SpecificData.class); | ||
} | ||
|
||
@Override | ||
public void serialize(SpecificData data, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) | ||
throws IOException { | ||
System.out.println("here! "); | ||
// Skip the specific data instance | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
...o/runtime/src/main/java/io/quarkus/avro/runtime/jackson/SpecificRecordBaseSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package io.quarkus.avro.runtime.jackson; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
import org.apache.avro.Schema; | ||
import org.apache.avro.specific.SpecificRecordBase; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import com.fasterxml.jackson.databind.ser.std.StdSerializer; | ||
|
||
/** | ||
* By default, you cannot serialize Avro specific records to JSON, as they contain non-serializable members. | ||
* This serializer iterates over the declared fields (in the schema), and build an object only containing these fields. | ||
* It means that the Avro "metadata" won't be included in the JSON representation. | ||
*/ | ||
public class SpecificRecordBaseSerializer extends StdSerializer<SpecificRecordBase> { | ||
|
||
public SpecificRecordBaseSerializer() { | ||
super(SpecificRecordBase.class); | ||
} | ||
|
||
@Override | ||
public void serialize(SpecificRecordBase record, JsonGenerator gen, | ||
SerializerProvider provider) throws IOException { | ||
gen.writeStartObject(); | ||
List<Schema.Field> fields = record.getSchema().getFields(); | ||
for (Schema.Field field : fields) { | ||
provider.defaultSerializeField(field.name(), record.get(field.pos()), gen); | ||
} | ||
gen.writeEndObject(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...ka-avro/src/test/java/io/quarkus/it/kafka/AvroSpecificRecordJacksonSerializationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.quarkus.it.kafka; | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import io.quarkus.it.kafka.avro.Pet; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
|
||
/** | ||
* This test verifies that the Avro classes can be serialized using Jackson. | ||
* By default, they can't as the Avro class contains non-serializable fields. | ||
* | ||
* There is a custom serializer registered explicitly that allow the serialization. | ||
*/ | ||
@QuarkusTest | ||
public class AvroSpecificRecordJacksonSerializationTest { | ||
|
||
@Inject | ||
ObjectMapper mapper; | ||
|
||
@Test | ||
void testSerialization() throws JsonProcessingException { | ||
Assertions.assertTrue(mapper.getRegisteredModuleIds().contains("AvroSpecificRecordModule")); | ||
|
||
Pet pet = new Pet("roxanne", "gray"); | ||
String s = mapper.writer().writeValueAsString(pet); | ||
// The serializer preserves the order of the field. | ||
Assertions.assertEquals(s, "{\"name\":\"roxanne\",\"color\":\"gray\"}"); | ||
} | ||
|
||
} |