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
Fix Issue #497 and add a testcase #699
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
package com.jayway.jsonpath; | ||
|
||
import com.jayway.jsonpath.spi.json.JsonOrgJsonProvider; | ||
import com.jayway.jsonpath.spi.mapper.JsonOrgMappingProvider; | ||
import org.json.JSONArray; | ||
import org.json.JSONObject; | ||
import org.junit.Test; | ||
|
@@ -76,4 +78,23 @@ public void read_book_length() { | |
assertThat(result).isEqualTo(4); | ||
} | ||
|
||
@Test | ||
public void Issue497() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please change method names to something more descriptive. I don't want to have to go to github to find out what this issue is. |
||
String json = "{\"foo\": \"bar\", \"emptyObject\": {},\"emptyList\":[]}"; | ||
Configuration config = Configuration.defaultConfiguration() | ||
.jsonProvider(new JsonOrgJsonProvider()) | ||
.mappingProvider(new JsonOrgMappingProvider()); | ||
Object result = JsonPath.using(config).parse(json).read("$..foo"); | ||
assertThat(result.toString()).isEqualTo("[\"bar\"]"); | ||
} | ||
|
||
@Test | ||
public void Issue497_2() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here bud. |
||
String json = "{\"foo\": \"bar\", \"emptyObject\": {\"emptyList\":[]},\"emptyList\":[]}"; | ||
Configuration config = Configuration.defaultConfiguration() | ||
.jsonProvider(new JsonOrgJsonProvider()) | ||
.mappingProvider(new JsonOrgMappingProvider()); | ||
Object result = JsonPath.using(config).parse(json).read("$..foo"); | ||
assertThat(result.toString()).isEqualTo("[\"bar\"]"); | ||
} | ||
} |
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.
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.
You could probably simplify this method with streams (Sorry for any syntax errors. I'm writing this in the browser):
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.
Yeah, thanks! I have seen all the suggestions you mentioned, and I will change it as soon as possible.
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 find that the return type of
jsonObject.names()
isJsonArray
, which doesn't have method liketoList
. But thejsonObject
has another methodkeySet
which return the set of key that is needed.(PS: the implementation of
jsonObject.names()
is that it iteratively adds the value inkeySet
intoJsonArray
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.
https://stleary.github.io/JSON-java/org/json/JSONObject.html#names--
https://stleary.github.io/JSON-java/org/json/JSONArray.html#toList--
JSONObject::names should return a JSONArray
JSONArray should have a method called toList() according to their documentation.
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.
Also, I updated the code snippet. Instead of casting (String) name you can do Object::toString
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.
Yeah, I find that the version of org.Json in JsonPath is 20140107, where
JSONArray
has no methodtoList
.toList
is introduced in version20160807
. What could I do?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.
Please check my latest two commits, thanks!
The code of method
.name()
is as follows, I think it is not necessary to use.name()
. Just using.keySet()
will be fine.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.
Looks good
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.
It is my first time to contribute to the open source community, sincerely thanks for your suggestions and approval !