forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
quarkusio#2663 JSON-B based (de-)serialization support for Kafka clie…
…nt module
- Loading branch information
1 parent
68bf66d
commit 81bf2f6
Showing
12 changed files
with
326 additions
and
162 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
50 changes: 50 additions & 0 deletions
50
...client/runtime/src/main/java/io/quarkus/kafka/client/serialization/JsonbDeserializer.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,50 @@ | ||
package io.quarkus.kafka.client.serialization; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Map; | ||
|
||
import javax.json.bind.Jsonb; | ||
import javax.json.bind.JsonbBuilder; | ||
|
||
import org.apache.kafka.common.serialization.Deserializer; | ||
|
||
/** | ||
* A {@link Deserializer} that deserializes JSON using JSON-B. | ||
*/ | ||
public class JsonbDeserializer<U> implements Deserializer<U> { | ||
|
||
private final Jsonb jsonb; | ||
private final Class<U> type; | ||
|
||
public JsonbDeserializer(Class<U> type) { | ||
this(type, JsonbBuilder.create()); | ||
} | ||
|
||
public JsonbDeserializer(Class<U> type, Jsonb jsonb) { | ||
this.jsonb = jsonb; | ||
this.type = type; | ||
} | ||
|
||
@Override | ||
public void configure(Map<String, ?> configs, boolean isKey) { | ||
} | ||
|
||
@Override | ||
public U deserialize(String topic, byte[] data) { | ||
if (data == null) { | ||
return null; | ||
} | ||
|
||
try (InputStream is = new ByteArrayInputStream(data)) { | ||
return jsonb.fromJson(is, type); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void close() { | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
.../kafka-client/runtime/src/main/java/io/quarkus/kafka/client/serialization/JsonbSerde.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,51 @@ | ||
package io.quarkus.kafka.client.serialization; | ||
|
||
import java.util.Map; | ||
|
||
import javax.json.bind.Jsonb; | ||
import javax.json.bind.JsonbBuilder; | ||
|
||
import org.apache.kafka.common.serialization.Deserializer; | ||
import org.apache.kafka.common.serialization.Serde; | ||
import org.apache.kafka.common.serialization.Serializer; | ||
|
||
/** | ||
* A {@link Serde} that (de-)serializes JSON using JSON-B. | ||
*/ | ||
public class JsonbSerde<T> implements Serde<T> { | ||
|
||
private final Class<T> type; | ||
private final Jsonb jsonb; | ||
|
||
public JsonbSerde(Class<T> type) { | ||
this(type, JsonbBuilder.create()); | ||
} | ||
|
||
public JsonbSerde(Class<T> type, Jsonb jsonb) { | ||
this.type = type; | ||
this.jsonb = jsonb; | ||
} | ||
|
||
@Override | ||
public void configure(Map<String, ?> configs, boolean isKey) { | ||
} | ||
|
||
@Override | ||
public void close() { | ||
try { | ||
jsonb.close(); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public Serializer<T> serializer() { | ||
return new JsonbSerializer<T>(); | ||
} | ||
|
||
@Override | ||
public Deserializer<T> deserializer() { | ||
return new JsonbDeserializer<T>(type); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...a-client/runtime/src/main/java/io/quarkus/kafka/client/serialization/JsonbSerializer.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,44 @@ | ||
package io.quarkus.kafka.client.serialization; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
import javax.json.bind.Jsonb; | ||
import javax.json.bind.JsonbBuilder; | ||
|
||
import org.apache.kafka.common.serialization.Serializer; | ||
|
||
/** | ||
* A {@link Serializer} that serializes to JSON using JSON-B. | ||
*/ | ||
public class JsonbSerializer<U> implements Serializer<U> { | ||
|
||
private final Jsonb jsonb; | ||
|
||
public JsonbSerializer() { | ||
jsonb = JsonbBuilder.create(); | ||
} | ||
|
||
public JsonbSerializer(Jsonb jsonb) { | ||
this.jsonb = jsonb; | ||
} | ||
|
||
@Override | ||
public void configure(Map<String, ?> configs, boolean isKey) { | ||
} | ||
|
||
@Override | ||
public byte[] serialize(String topic, U data) { | ||
try (ByteArrayOutputStream output = new ByteArrayOutputStream()) { | ||
jsonb.toJson(data, output); | ||
return output.toByteArray(); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void close() { | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...ions/kafka-client/runtime/src/test/java/io/quarkus/kafka/client/serde/JsonbSerdeTest.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,28 @@ | ||
package io.quarkus.kafka.client.serde; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
public class JsonbSerdeTest { | ||
|
||
@Test | ||
public void shouldSerializeAndDeserializeEntity() { | ||
MyEntity entity = new MyEntity(); | ||
entity.id = 42L; | ||
entity.name = "Bob"; | ||
|
||
try (JsonbSerde<MyEntity> serde = new JsonbSerde<>(MyEntity.class)) { | ||
byte[] serialized = serde.serializer().serialize("my-topic", entity); | ||
MyEntity deserialized = serde.deserializer().deserialize("my-topic", serialized); | ||
|
||
assertThat(deserialized.id).isEqualTo(42L); | ||
assertThat(deserialized.name).isEqualTo("Bob"); | ||
} | ||
} | ||
|
||
public static class MyEntity { | ||
public long id; | ||
public String name; | ||
} | ||
} |
84 changes: 0 additions & 84 deletions
84
integration-tests/kafka/src/main/java/io/quarkus/it/kafka/JsonObjectSerde.java
This file was deleted.
Oops, something went wrong.
18 changes: 18 additions & 0 deletions
18
integration-tests/kafka/src/main/java/io/quarkus/it/kafka/streams/Category.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,18 @@ | ||
package io.quarkus.it.kafka.streams; | ||
|
||
import io.quarkus.runtime.annotations.RegisterForReflection; | ||
|
||
@RegisterForReflection | ||
public class Category { | ||
|
||
public String name; | ||
public String value; | ||
|
||
public Category() { | ||
} | ||
|
||
public Category(String name, String value) { | ||
this.name = name; | ||
this.value = value; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
integration-tests/kafka/src/main/java/io/quarkus/it/kafka/streams/Customer.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,20 @@ | ||
package io.quarkus.it.kafka.streams; | ||
|
||
import io.quarkus.runtime.annotations.RegisterForReflection; | ||
|
||
@RegisterForReflection | ||
public class Customer { | ||
|
||
public int id; | ||
public String name; | ||
public int category; | ||
|
||
public Customer() { | ||
} | ||
|
||
public Customer(int id, String name, int category) { | ||
this.id = id; | ||
this.name = name; | ||
this.category = category; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
integration-tests/kafka/src/main/java/io/quarkus/it/kafka/streams/EnrichedCustomer.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,20 @@ | ||
package io.quarkus.it.kafka.streams; | ||
|
||
import io.quarkus.runtime.annotations.RegisterForReflection; | ||
|
||
@RegisterForReflection | ||
public class EnrichedCustomer { | ||
|
||
public int id; | ||
public String name; | ||
public Category category; | ||
|
||
public EnrichedCustomer() { | ||
} | ||
|
||
public EnrichedCustomer(int id, String name, Category category) { | ||
this.id = id; | ||
this.name = name; | ||
this.category = category; | ||
} | ||
} |
Oops, something went wrong.