-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[#719] JSON-B extension. #720
Open
bmarwell
wants to merge
4
commits into
jwtk:master
Choose a base branch
from
bmarwell:719_jsonb_extension
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fragment-Host: io.jsonwebtoken.jjwt-api |
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,47 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>io.jsonwebtoken</groupId> | ||
<artifactId>jjwt-root</artifactId> | ||
<version>0.11.3-SNAPSHOT</version> | ||
<relativePath>../../pom.xml</relativePath> | ||
</parent> | ||
|
||
<artifactId>jjwt-jsonb</artifactId> | ||
<name>JJWT :: Extensions :: JSON-B</name> | ||
<packaging>jar</packaging> | ||
|
||
<properties> | ||
<jjwt.root>${basedir}/../..</jjwt.root> | ||
<!-- JSON-B uses static methods in interfaces. --> | ||
<jdk.version>8</jdk.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.jsonwebtoken</groupId> | ||
<artifactId>jjwt-api</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>jakarta.json</groupId> | ||
<artifactId>jakarta.json-api</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>jakarta.json.bind</groupId> | ||
<artifactId>jakarta.json.bind-api</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<!-- Test dependency --> | ||
<dependency> | ||
<groupId>org.apache.johnzon</groupId> | ||
<artifactId>johnzon-jsonb</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
66 changes: 66 additions & 0 deletions
66
extensions/jsonb/src/main/java/io/jsonwebtoken/jsonb/io/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,66 @@ | ||
/* | ||
* Copyright (C) 2014 jsonwebtoken.io | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.jsonwebtoken.jsonb.io; | ||
|
||
import io.jsonwebtoken.io.DeserializationException; | ||
import io.jsonwebtoken.io.Deserializer; | ||
|
||
import javax.json.bind.Jsonb; | ||
import javax.json.bind.JsonbException; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
/** | ||
* @since 0.10.0 | ||
bmarwell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
public class JsonbDeserializer<T> implements Deserializer<T> { | ||
|
||
private final Class<T> returnType; | ||
private final Jsonb jsonb; | ||
|
||
@SuppressWarnings("unused") //used via reflection by RuntimeClasspathDeserializerLocator | ||
public JsonbDeserializer() { | ||
this(JsonbSerializer.DEFAULT_JSONB); | ||
} | ||
|
||
@SuppressWarnings({"unchecked", "WeakerAccess", "unused"}) // for end-users providing a custom ObjectMapper | ||
public JsonbDeserializer(Jsonb jsonb) { | ||
this(jsonb, (Class<T>) Object.class); | ||
} | ||
|
||
private JsonbDeserializer(Jsonb jsonb, Class<T> returnType) { | ||
requireNonNull(jsonb, "ObjectMapper cannot be null."); | ||
requireNonNull(returnType, "Return type cannot be null."); | ||
this.jsonb = jsonb; | ||
this.returnType = returnType; | ||
} | ||
|
||
@Override | ||
public T deserialize(byte[] bytes) throws DeserializationException { | ||
try { | ||
return readValue(bytes); | ||
} catch (JsonbException jsonbException) { | ||
String msg = "Unable to deserialize bytes into a " + returnType.getName() + " instance: " + jsonbException.getMessage(); | ||
throw new DeserializationException(msg, jsonbException); | ||
} | ||
} | ||
|
||
protected T readValue(byte[] bytes) { | ||
return jsonb.fromJson(new String(bytes, StandardCharsets.UTF_8), returnType); | ||
} | ||
|
||
} |
75 changes: 75 additions & 0 deletions
75
extensions/jsonb/src/main/java/io/jsonwebtoken/jsonb/io/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,75 @@ | ||
/* | ||
* Copyright (C) 2014 jsonwebtoken.io | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.jsonwebtoken.jsonb.io; | ||
|
||
import io.jsonwebtoken.io.Encoders; | ||
import io.jsonwebtoken.io.SerializationException; | ||
import io.jsonwebtoken.io.Serializer; | ||
import io.jsonwebtoken.lang.Assert; | ||
|
||
import javax.json.bind.Jsonb; | ||
import javax.json.bind.JsonbBuilder; | ||
import javax.json.bind.JsonbException; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
/** | ||
* @since 0.10.0 | ||
bmarwell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
public class JsonbSerializer<T> implements Serializer<T> { | ||
|
||
static final Jsonb DEFAULT_JSONB = JsonbBuilder.create(); | ||
|
||
private final Jsonb jsonb; | ||
|
||
@SuppressWarnings("unused") //used via reflection by RuntimeClasspathDeserializerLocator | ||
public JsonbSerializer() { | ||
this(DEFAULT_JSONB); | ||
} | ||
|
||
@SuppressWarnings("WeakerAccess") //intended for end-users to use when providing a custom ObjectMapper | ||
public JsonbSerializer(Jsonb jsonb) { | ||
requireNonNull(jsonb, "Jsonb cannot be null."); | ||
this.jsonb = jsonb; | ||
} | ||
|
||
@Override | ||
public byte[] serialize(T t) throws SerializationException { | ||
Assert.notNull(t, "Object to serialize cannot be null."); | ||
try { | ||
return writeValueAsBytes(t); | ||
} catch (JsonbException jsonbException) { | ||
String msg = "Unable to serialize object: " + jsonbException.getMessage(); | ||
throw new SerializationException(msg, jsonbException); | ||
} | ||
} | ||
|
||
@SuppressWarnings("WeakerAccess") //for testing | ||
protected byte[] writeValueAsBytes(T t) { | ||
final Object obj; | ||
|
||
if (t instanceof byte[]) { | ||
obj = Encoders.BASE64.encode((byte[]) t); | ||
} else if (t instanceof char[]) { | ||
obj = new String((char[]) t); | ||
} else { | ||
obj = t; | ||
} | ||
|
||
return this.jsonb.toJson(obj).getBytes(StandardCharsets.UTF_8); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
extensions/jsonb/src/main/resources/META-INF/services/io.jsonwebtoken.io.Deserializer
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 @@ | ||
io.jsonwebtoken.jsonb.io.JsonbDeserializer |
1 change: 1 addition & 0 deletions
1
extensions/jsonb/src/main/resources/META-INF/services/io.jsonwebtoken.io.Serializer
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 @@ | ||
io.jsonwebtoken.jsonb.io.JsonbSerializer |
76 changes: 76 additions & 0 deletions
76
extensions/jsonb/src/test/groovy/io/jsonwebtoken/jsonb/io/JsonbDeserializerTest.groovy
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,76 @@ | ||
package io.jsonwebtoken.jsonb.io | ||
|
||
import io.jsonwebtoken.io.DeserializationException | ||
import io.jsonwebtoken.io.Deserializer | ||
import io.jsonwebtoken.lang.Strings | ||
import org.junit.Test | ||
|
||
import javax.json.bind.JsonbBuilder | ||
|
||
import static org.easymock.EasyMock.* | ||
import static org.hamcrest.CoreMatchers.instanceOf | ||
import static org.hamcrest.MatcherAssert.assertThat | ||
import static org.junit.Assert.* | ||
|
||
class JsonbDeserializerTest { | ||
|
||
@Test | ||
void loadService() { | ||
def deserializer = ServiceLoader.load(Deserializer).iterator().next() | ||
assertThat(deserializer, instanceOf(JsonbDeserializer)) | ||
} | ||
|
||
|
||
@Test | ||
void testDefaultConstructor() { | ||
def deserializer = new JsonbDeserializer() | ||
assertNotNull deserializer.jsonb | ||
} | ||
|
||
@Test | ||
void testObjectMapperConstructor() { | ||
def customJsonb = JsonbBuilder.create() | ||
def deserializer = new JsonbDeserializer(customJsonb) | ||
assertSame customJsonb, deserializer.jsonb | ||
} | ||
|
||
@Test(expected = NullPointerException) | ||
void testObjectMapperConstructorWithNullArgument() { | ||
new JsonbDeserializer<>(null) | ||
} | ||
|
||
@Test | ||
void testDeserialize() { | ||
byte[] serialized = '{"hello":"世界"}'.getBytes(Strings.UTF_8) | ||
def expected = [hello: '世界'] | ||
def result = new JsonbDeserializer().deserialize(serialized) | ||
assertEquals expected, result | ||
} | ||
|
||
@Test | ||
void testDeserializeFailsWithJsonProcessingException() { | ||
|
||
def ex = createMock javax.json.bind.JsonbException | ||
|
||
expect(ex.getMessage()).andReturn('foo') | ||
|
||
def deserializer = new JsonbDeserializer() { | ||
@Override | ||
protected Object readValue(byte[] bytes) throws javax.json.bind.JsonbException { | ||
throw ex | ||
} | ||
} | ||
|
||
replay ex | ||
|
||
try { | ||
deserializer.deserialize('{"hello":"世界"}'.getBytes(Strings.UTF_8)) | ||
fail() | ||
} catch (DeserializationException se) { | ||
assertEquals 'Unable to deserialize bytes into a java.lang.Object instance: foo', se.getMessage() | ||
assertSame ex, se.getCause() | ||
} | ||
|
||
verify ex | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uhm, you were changing a line which I didn’t touch. Did you mean to do that?
I did not implement the
Map
constructor for jsonb (nor did any of you for gson)...