-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Add configurable maximum depth in ConvertFrom-Json with -Depth
#8199
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
Merged
iSazonov
merged 8 commits into
PowerShell:master
from
adamgauthier:convertfrom-json-max-depth
Feb 20, 2019
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3f6391d
[Feature] Add configurable maximum depth in ConvertFrom-Json
adamgauthier ba5ee60
[fixup!] [feature] Fix StyleCop issues and address code review
adamgauthier 7c6d50d
[fixup!] [feature] Remove 0 option for no maximum
adamgauthier c71ff00
Merge remote-tracking branch 'upstream/master' into convertfrom-json-…
adamgauthier 27c141f
Remove colon for parameters per convention
adamgauthier 541524e
[fixup!] [feature] Use ValidateRange in New-NestedJson
adamgauthier eecc899
[fixup!] [feature] Use colon for -AsHashtable
adamgauthier dc39854
[fixup!] [feature] Capitalize JSON in comments and use Parameter()
markekraus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
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.
Wondering if a custom type
SerializerSettingsto encapsulate default 1024maxDepthand falsereturnHashtablehere would be better than adding overloads.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.
if there are other settings we would want in the future, then that would be a better approach
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 would consider the option to set
-Depthless than0for no maximum depth.0or$Nullare often the result of a wrong calculation. Besides,0could technically still be a desired "depth".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.
@iRon7
How so? Wouldn't that mean you want the deserialization to always fail? Is that really a use case? Also, the underlying
Newtonsoft.Jsondoes not allow 0 or less asMaxDepth, it throws at runtime.Uh oh!
There was an error while loading. Please reload this page.
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.
For a sample recurring hash table like:
A
$ht | ConvertTo-Json -Depth 1results in:I was expecting for
$ht | ConvertTo-Json -Depth 0to result in:But I indeed get an error:
Anyways, wouldn't it be better to get either a completely flat output or an error in case of a (incorrect calculated/unassigned/mistyped variable)
Depthof0or$Null?I think a negative
Depthwould just be more knowingly chosen.In other words, I do not really expect it to be a desired used case, but in the case of a scripting failure, I think a flat result or an error is more clear then a possible performance issue.
Just a thought, the decision is up to you...
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 still of the opinion that unlimited is not needed. 1 to int32.MaxValue is sufficient. Any JSON over Int32.MaxValue is either broken or contrived.
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.
In the real world, I expect the default of 1024 to be sufficient, so I would be ok with not needing unlimited.
-Depth 0for only the top level makes sense and consistent withGet-ChildItem -Depth 0There 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.
@iRon7 after thinking it through, I agree a negative value might be better than
0.@markekraus I understand powershell needs to be secure by default but I think this is an opportunity to provide the option for people to just say "I know what I'm doing, get out of my way". Sure, you could have them type
-Depth:([int]::MaxValue), but feels less convenient in my opinion and it would still have the overhead (however small) of the serializer repeatedly checking to make sure max depth isn't exceeded (not the case if you passnull).What do you guys think of having
-Depth -1for unlimited and maybe throw an error onBeginProcessing()if-Depthis0?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's not about security.. it's about absurdity. The only JSON objects that are that deep are contrived (made to be deep for fun or to specifically test depth limits) or broken (whatever produced the JSON had some issue). 1024 is already absurdly deep for a JSON object. We all ready catch outliers with the default setting. Between 1024 and Int32.MaxValue is a the realm of intrigue only. We don't need to have special case logic for that. We don't even really need infinite depth. What is the target audience for such a feature? Who really has objects that deep?
Just because we can, doesn't mean we should I still stand by my statement that 1 to int32.MaxValue is sufficient.