question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Integer claim fails with GSON

See original GitHub issue

The 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

image

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
oliverhauslercommented, Aug 19, 2020

I agree with you that looking at the decimal point . or the E 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 is Integer, 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.

0reactions
stale[bot]commented, Dec 25, 2020

Closed due to inactivity.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found