-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removed recently added Reader/Writer concepts for just overloaded met…
…hods in existing Serializer/Deserializer interfaces. This results in less change and is probably more intuitive for existing library users.
- Loading branch information
1 parent
9f19f0e
commit 7fb7b79
Showing
78 changed files
with
1,590 additions
and
2,411 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
72 changes: 72 additions & 0 deletions
72
api/src/main/java/io/jsonwebtoken/io/AbstractDeserializer.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,72 @@ | ||
/* | ||
* Copyright © 2023 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.io; | ||
|
||
import io.jsonwebtoken.lang.Assert; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.InputStream; | ||
|
||
/** | ||
* Convenient base class to use to implement {@link Deserializer}s, with only the {@link #doDeserialize(InputStream)}. | ||
* | ||
* @param <T> the type of object returned after deserialization | ||
* @since JJWT_RELEASE_VERSION | ||
*/ | ||
public abstract class AbstractDeserializer<T> implements Deserializer<T> { | ||
|
||
/** | ||
* EOF (End of File) marker, equal to {@code -1}. | ||
*/ | ||
protected static final int EOF = -1; | ||
|
||
private static final byte[] EMPTY_BYTES = new byte[0]; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public final T deserialize(byte[] bytes) throws DeserializationException { | ||
bytes = bytes == null ? EMPTY_BYTES : bytes; // null safe | ||
return deserialize(new ByteArrayInputStream(bytes)); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public final T deserialize(InputStream in) throws DeserializationException { | ||
Assert.notNull(in, "InputStream argument cannot be null."); | ||
try { | ||
return doDeserialize(in); | ||
} catch (Throwable t) { | ||
if (t instanceof DeserializationException) { | ||
throw (DeserializationException) t; | ||
} | ||
String msg = "Unable to deserialize: " + t.getMessage(); | ||
throw new DeserializationException(msg, t); | ||
} | ||
} | ||
|
||
/** | ||
* Reads the specified {@code InputStream} and returns the corresponding Java object. | ||
* | ||
* @param in the input stream to read | ||
* @return the deserialized Java object | ||
* @throws DeserializationException if there is a problem reading the stream or creating the expected Java object | ||
*/ | ||
protected abstract T doDeserialize(InputStream in) throws Exception; | ||
} |
46 changes: 46 additions & 0 deletions
46
api/src/main/java/io/jsonwebtoken/io/AbstractSerializer.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,46 @@ | ||
/* | ||
* Copyright © 2023 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.io; | ||
|
||
import io.jsonwebtoken.lang.Objects; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.OutputStream; | ||
|
||
public abstract class AbstractSerializer<T> implements Serializer<T> { | ||
|
||
@Override | ||
public final byte[] serialize(T t) throws SerializationException { | ||
ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
serialize(t, out); | ||
return out.toByteArray(); | ||
} | ||
|
||
@Override | ||
public void serialize(T t, OutputStream out) throws SerializationException { | ||
try { | ||
doSerialize(t, out); | ||
} catch (Throwable e) { | ||
if (e instanceof SerializationException) { | ||
throw (SerializationException) e; | ||
} | ||
String msg = "Unable to serialize object of type " + Objects.nullSafeClassName(t) + ": " + e.getMessage(); | ||
throw new SerializationException(msg, e); | ||
} | ||
} | ||
|
||
protected abstract void doSerialize(T t, OutputStream out) throws Exception; | ||
} |
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 was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.