-
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.
make it compatible for jackson. (#12)
- Loading branch information
Showing
10 changed files
with
92 additions
and
7 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
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,5 @@ | ||
dependencies { | ||
compileOnly(project(":common")) | ||
|
||
compileOnly(libs.jackson) | ||
} |
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 @@ | ||
artifact-id=pubsub-jackson |
34 changes: 34 additions & 0 deletions
34
jackson/src/main/java/net/infumia/pubsub/CodecJackson.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 net.infumia.pubsub; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import java.io.IOException; | ||
|
||
final class CodecJackson<T> implements Codec<T> { | ||
private final ObjectMapper mapper; | ||
private final Class<T> type; | ||
|
||
CodecJackson(final ObjectMapper mapper, final Class<T> type) { | ||
this.mapper = mapper; | ||
this.type = type; | ||
} | ||
|
||
@Override | ||
public byte[] encode(final T t) { | ||
try { | ||
return this.mapper.writerFor(this.type).writeValueAsBytes(t); | ||
} catch (final JsonProcessingException e) { | ||
throw new RuntimeException("Failed to serialize " + this.type, e); | ||
} | ||
} | ||
|
||
@Override | ||
public T decode(final byte[] bytes) { | ||
try { | ||
return this.mapper.readerFor(this.type).readValue(bytes); | ||
} catch (final IOException e) { | ||
throw new RuntimeException("Failed to deserialize " + this.type, e); | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
jackson/src/main/java/net/infumia/pubsub/CodecProviderJackson.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,24 @@ | ||
package net.infumia.pubsub; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
/** | ||
* A {@link CodecProvider} implementation that uses Jackson for JSON serialization and deserialization. | ||
*/ | ||
public final class CodecProviderJackson implements CodecProvider { | ||
private final JacksonProvider provider; | ||
|
||
/** | ||
* Ctor. | ||
* | ||
* @param provider the {@link JacksonProvider} used to provide {@link ObjectMapper} instances. | ||
*/ | ||
public CodecProviderJackson(final JacksonProvider provider) { | ||
this.provider = provider; | ||
} | ||
|
||
@Override | ||
public <T> Codec<T> provide(final Class<T> type) { | ||
return new CodecJackson<>(this.provider.provide(), type); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
jackson/src/main/java/net/infumia/pubsub/JacksonProvider.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,15 @@ | ||
package net.infumia.pubsub; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
/** | ||
* A provider interface for supplying {@link ObjectMapper} instances. | ||
*/ | ||
public interface JacksonProvider { | ||
/** | ||
* Provides an {@link ObjectMapper} instance. | ||
* | ||
* @return an {@link ObjectMapper} instance. | ||
*/ | ||
ObjectMapper provide(); | ||
} |
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