-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve Gson and JsonParser trailing data handling #2123
base: main
Are you sure you want to change the base?
Improve Gson and JsonParser trailing data handling #2123
Conversation
* | ||
* @author Inderjeet Singh | ||
* @author Joel Leitch | ||
*/ | ||
public class JsonParserTest extends TestCase { | ||
public class GsonParsingTest extends TestCase { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have renamed this class because there exists already a JsonParserTest
(in a different package), and this test class is mainly checking the behavior of Gson
methods.
0da3a56
to
ee70912
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ran this against all of Google's tests and did not find any problems, except:
- references to a now-deleted internal method, which I think needs to be undeleted
- tests that check for specific exceptions that are now different
I did have to merge with HEAD, and that implied adding a couple of @Test
annotations to the now-JUnit4 JsonParserTest
.
*/ | ||
public static JsonElement parse(JsonReader reader) throws JsonParseException { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm finding a surprisingly large number of references to this method in Google's source base (including third-party code). In some cases it is because people have copied RuntimeTypeAdapterFactory
into their own projects. So I think we probably need to keep this overload. Then of course we could continue to call it from RuntimeTypeAdapterFactory
instead of the new (..., false)
version.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, before #1959 RuntimeTypeAdapterFactory
was using the internal Streams
class. I have now added back that method, but marked it as deprecated to make users aware that it is declared in an internal Gson class, I hope that is ok.
Then of course we could continue to call it from
RuntimeTypeAdapterFactory
instead of the new(..., false)
version.
The RuntimeTypeAdapterFactory
variant which was still using this method was actually a copy of the class from the extras
module. I have now removed that copy (and the enclosing test class RuntimeTypeAdapterFactoryFunctionalTest
) and have moved the adjusted test code to the RuntimeTypeAdapterFactoryTest
class in the extras
module to avoid code duplication.
Are these tests related to JSON with trailing data / multiple top level JSON values? And are the newly thrown exceptions more reasonable? |
Thanks for prompting me to look at this in more detail. I think there are problems with the new code. I haven't looked at all the test failures, but the one I did look at failed because this: JsonParser.parseString("$$$$////") gets a |
The problem is that
This indicates that the first With the changes of this PR the
If you don't want this to be changed, then the end of document checks can most likely be removed (or reduced to a |
Improves the trailing data handling for
Gson
andJsonParser
, mainly addressing the following issues:null
, e.g.null[]
, because it erroneously considered anull
to always mean that the input was emptyreader.peek() != JsonToken.END_DOCUMENT
was done separately, where theJsonReader
was in strict mode again and thereforepeek()
itself threw aMalformedJsonException
.With the new code added by this pull request the custom
JsonSyntaxException
is now thrown for multiple top-level JSON elements; however, aMalformedJsonException
could likely still occur if the trailing data is not valid JSON data, but that is probably acceptable.