| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
There was a problem hiding this comment.
Should this be a && so we check for existence of [ and ] ? If any of those is missing it is an invalid array anyways.
Sorry, something went wrong.
There was a problem hiding this comment.
No, this should be an '||' because we are trying to detect an invalid array. These are some examples:
Sorry, something went wrong.
There was a problem hiding this comment.
Trim will only remove newline characters at the start and end. To remove all newline characters use Replace instead. And .ToCharArray() is unnecessary.
Sorry, something went wrong.
There was a problem hiding this comment.
Fixed.
Sorry, something went wrong.
There was a problem hiding this comment.
Use a String.Empty instead of "". More efficient as it does not create a new zero length string.
Sorry, something went wrong.
There was a problem hiding this comment.
Fixed.
Sorry, something went wrong.
There was a problem hiding this comment.
Maybe a && here as well?
Sorry, something went wrong.
There was a problem hiding this comment.
Same as the comment above.
Sorry, something went wrong.
There was a problem hiding this comment.
Use [Environment]::NewLine instead
Sorry, something went wrong.
There was a problem hiding this comment.
Test code updated as per Jim's recommendation below.
Sorry, something went wrong.
There was a problem hiding this comment.
Can the variations be converted to use -TestCase for It? So if one of the Should fails the others are still run.
Sorry, something went wrong.
There was a problem hiding this comment.
I've implemented two different test cases instead.
Sorry, something went wrong.
There was a problem hiding this comment.
this should just be:
$array = [pscustomobject]@{ objectName = "object1Name"; objectValue = "object1Value" },
[pscustomobject]@{ objectName = "object2Name"; objectValue = "object2Value" }
Sorry, something went wrong.
There was a problem hiding this comment.
Great suggestion. I will update the test code.
Sorry, something went wrong.
There was a problem hiding this comment.
I'm wondering if this (or the above) needs to be done with a file at all. Does the following cover the case?
$result = "[1,","2,","3]" | convertfrom-json $result.count | Should be 3
Sorry, something went wrong.
There was a problem hiding this comment.
Test code updated.
Sorry, something went wrong.
There was a problem hiding this comment.
LGTM
Sorry, something went wrong.
|
tests are much cleaner now - LGTM |
Sorry, something went wrong.
|
James Truher (@JamesWTruher) can you mark your review as Approved? |
Sorry, something went wrong.
|
@PowerShell/powershell-maintainers : This is ready to be merged, please let me know if you need anything else. Thanks. |
Sorry, something went wrong.
There was a problem hiding this comment.
The comment says contains, but the code says starts with/ends with.
Besides that, it just looks more complicated than it needs to be, e.g. 2 calls to IndexOf, then strip newlines then spaces. Why not just use a regex, like:
if (Regex.Match(input, "(^\s*\[)|(\s*\]$)"))
{
JArray.Parse(input);
}I'd also like to see some sort of note about whether or not this is considered a NewtonSoft bug - ideally a link to an issue so we can track it.
Sorry, something went wrong.
There was a problem hiding this comment.
as these markers for
Do you mean these markers are for?
Sorry, something went wrong.
There was a problem hiding this comment.
The comment doesn't match the code. Carriage return is \r, but Environemnt.NewLine is \r\n on windows and \n on Unix. And what if the json string is has \r\n but we are calling ConvertFrom-Json on Unix?
Sorry, something went wrong.
There was a problem hiding this comment.
Do we care about other white spaces? For example what if there is \t?
By the way, new lines are white spaces too.
Sorry, something went wrong.
There was a problem hiding this comment.
white spaces include new lines.
[char]::IsWhiteSpace([environment]::NewLine, 0) True
Sorry, something went wrong.
There was a problem hiding this comment.
is can be
Typo
Sorry, something went wrong.
There was a problem hiding this comment.
The comments here are confusing to me -- what is the buffer here? Could you please make the comments easier to understand?
Sorry, something went wrong.
|
Francisco Gamino (@Francisco-Gamino) One more question -- why do you have to remove all white spaces before calling JArray.Parse? Won't it be enough to just trim the leading and trailing white spaces? |
Sorry, something went wrong.
|
Please update the title and description based on the instructions in Contributing - Pull Request Submission
|
Sorry, something went wrong.
|
Chatted with Francisco Gamino (@Francisco-Gamino) offline, and it turns out the JsonException thrown by JArray.Parse will be captured and re-thrown as an ArgumentException, and then the enclosing code will capture the ArgumentException and assume that it's caused by partial input and hen keep accumulating pipeline input. Francisco will add more comments to explain this process. |
Sorry, something went wrong.
…of strings which represent a JSON content.
…s as a single string.
|
Jason Shirk (@lzybkr): Thank you Jason for your suggestion. Using regular expressions makes the code much simpler. |
Sorry, something went wrong.
| // To work around this, we need to identify when input is a Json array, and then try to parse it via JArray.Parse(). | ||
|
|
||
| // If input starts with '[' or ends with ']' (ignoring white spaces). | ||
| if ((Regex.Match(input, @"(^\s*\[)|(\s*\]$)")).Success) |
There was a problem hiding this comment.
The reg expression used here seems not right ... Francisco Gamino (@Francisco-Gamino) let's discuss it tomorrow.
Sorry, something went wrong.
…ines. (PowerShell#3823) * Fixing ConvertFrom-Json on CoreCLR to be able to handle a collection of strings which represent a JSON content. * Adding test case for ConvertFrom-Json to process an array of PSObjects as a single string.
| Back | FazBrowse Home | New Git URL |
Addressing issue #3284: ConvertFrom-JSON fails to parse syntactically-correct JSON array
For PowerShell on CoreCLR, we use Json.Net. However, this deserialzer does not throw an exception if an invalid array is passed, so we need to add extra logic to handle this. An invalid array can occurred when the user reads a file via get-content as a collection of PSObjects. E.g.,
Added tests for the following: