Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
45 changed files
with
2,276 additions
and
988 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* 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 java.io.IOException; | ||
|
||
/** | ||
* A {@code Reader} reads an object from an input stream. | ||
* | ||
* @param <T> the type of object read. | ||
* @since JJWT_RELEASE_VERSION | ||
*/ | ||
public interface Reader<T> { | ||
|
||
/** | ||
* Reads an object from an input stream, but does not close it; the caller must close the stream as necessary. | ||
* | ||
* @param in the input stream. | ||
* @return the object read from the stream. | ||
* @throws IOException if there is a problem reading from the stream or creating the expected object. | ||
*/ | ||
T read(java.io.Reader in) throws IOException; | ||
} |
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,36 @@ | ||
/* | ||
* 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 java.io.IOException; | ||
|
||
/** | ||
* A {@code Writer} writes an object to an output stream. | ||
* | ||
* @param <T> the type of object to write. | ||
* @since JJWT_RELEASE_VERSION | ||
*/ | ||
public interface Writer<T> { | ||
|
||
/** | ||
* Writes {@code t} to the output stream, but does not close it; the caller must close the stream as necessary. | ||
* | ||
* @param out the output stream. | ||
* @param t the object to write. | ||
* @throws IOException if there is a problem writing. | ||
*/ | ||
void write(java.io.Writer out, T t) throws IOException; | ||
} |
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
58 changes: 58 additions & 0 deletions
58
extensions/gson/src/main/java/io/jsonwebtoken/gson/io/GsonReader.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,58 @@ | ||
/* | ||
* 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.gson.io; | ||
|
||
import com.google.gson.Gson; | ||
import io.jsonwebtoken.io.Reader; | ||
import io.jsonwebtoken.lang.Assert; | ||
|
||
import java.io.IOException; | ||
|
||
public class GsonReader<T> implements Reader<T> { | ||
|
||
private final Class<T> returnType; | ||
protected final Gson gson; | ||
|
||
public GsonReader() { | ||
this(GsonWriter.DEFAULT_GSON); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public GsonReader(Gson gson) { | ||
this(gson, (Class<T>) Object.class); | ||
} | ||
|
||
private GsonReader(Gson gson, Class<T> returnType) { | ||
Assert.notNull(gson, "gson cannot be null."); | ||
Assert.notNull(returnType, "Return type cannot be null."); | ||
this.gson = gson; | ||
this.returnType = returnType; | ||
} | ||
|
||
@Override | ||
public T read(java.io.Reader in) throws IOException { | ||
try { | ||
return readValue(in); | ||
} catch (Throwable t) { | ||
String msg = "Unable to read JSON as a " + returnType.getName() + " instance: " + t.getMessage(); | ||
throw new IOException(msg, t); | ||
} | ||
} | ||
|
||
protected T readValue(java.io.Reader reader) { | ||
return gson.fromJson(reader, returnType); | ||
} | ||
} |
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
Oops, something went wrong.