Use JSONTokener for application/json reponse parsing #8087
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.
I'm changing the way the core HTTP plugin parses JSON responses, because I encountered a problem with a specific response from an API:
The request in question returns an application/json response that is just a 64bit number literal.
Unfortunately, the way the parser in HttpRequestHandler.java works, is that it tries to parse number literals without a decimal point as a 32bit
Integer
, which fails for a number this size.Now, the easiest fix would have been to just change the type to
Long
here, but that seemed a bit short sighted, because there seem to have been a lot of changes to this parser for specific scenarios like this, so a new approach seemed the better choice. As you can see in my commits, I first tried to just copy&paste the parser logic that JSObject/JSArray use internally to literals, but that seemed silly to me, because at that point, why not just USE the internal parser instead of trying to recreate most of what it does.So my change gets rid of all the specific logic for all the different types of literals, and just uses the parser of the JSONTokener instead. In it's readLiteral method, the Tokener basically does the exact same thing the long if/else statement here did before, but better.
Now I just try to parse the input as first an object, then an array, and if that does not work, I just use the Tokener directly to let it parse the input as a literal.
I tested it with all of the literals that were processed here before and the result for all of them is the same as before, but now Long values work too, and we hopefully don't have to add more special cases to this method anymore.