Integer claim fails with GSON
See original GitHub issueThe following works with Jackson but fails with GSON because the claim value becomes non-integer during build
.
I have
private static final int VERSION = 9; // an integer
I do
Jwts.builder()
.claim("version", VERSION) // ends up as "version": 9 in the JSON
.setExpiration(expires)
.signWith(SECRET_KEY, SignatureAlgorithm.HS256)
.compact();
and then
Jwts.parserBuilder()
.require("version", VERSION) // version is still 9 here
.setSigningKey(SECRET)
.build() // version seems to become 9.0
.parseClaimsJws(token); //fails with GSON
Issue Analytics
- State:
- Created 3 years ago
- Comments:11 (5 by maintainers)
Top Results From Across the Web
How can I prevent gson from converting integers to doubles
1) You have to create custom JsonDeserializer and not JsonSerializer like in your question. 2) I don't think this behavior comes from Double...
Read more >How To Serialize And Deserialize Interfaces In Java Using Gson
Our interface declares three features that all cars share. public interface Car { String model(); int maxSpeed(); String type(); }. Step 2:.
Read more >Serializing and Deserializing a List with Gson - Baeldung
One common use case is to serialize and deserialize a list of POJOs. Consider the class: public class MyClass { private int id; ......
Read more >Google GSON for JSON Processing - Spring Framework Guru
In order to use GSON, you need the JAR files of the GSON library in your project classpath. If you are using Maven,...
Read more >Mangling JSON numbers - TechEmpower Blog
If we have a long (64-bit integer) that we serialize into JSON, we might be in trouble ... This problem only occurs with...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I agree with you that looking at the decimal point
.
or theE
is the right way to determine the number format. This is what I would have done as well. Also agree with what you say regarding floating point numbers and lost precision.The problem with GSON I believe is that it doesn’t have a definition of what’s expected on the Java side, as we’re using
Object
here. If we would have the JWT explicitly coded as a class, this should work, because then GSON encounters a property which isInteger
,Long
, whatever, and it will properly convert it.If I would write a JWT library with the things we have just found out, I would take the approach of defining an abstract
JwtBase.java
class with the security stuff and common properties, and then have the developer extend it with their custom JWT containing their custom claims. This I believe would work in GSON.As for my part, I’ve been using Jackson, but thanks for discussing.
Closed due to inactivity.