- Notifications
You must be signed in to change notification settings - Fork8.1k
Add -SkipUnsupportedTypes parameter to ConvertTo-Json#26521
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:master
Are you sure you want to change the base?
Conversation
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.
Pull request overview
This PR adds a new-SkipUnsupportedTypes switch parameter toConvertTo-Json that allows graceful handling of dictionaries with non-string keys (such asException.Data). When enabled, the cmdlet silently skips dictionary entries with non-string keys instead of throwing aNonStringKeyInDictionary error, enabling serialization of objects that were previously impossible to convert to JSON.
Key changes:
- Added
-SkipUnsupportedTypesswitch parameter to control error handling behavior - Extended
ConvertToJsonContextstruct with new field and constructor overload while maintaining backward compatibility through constructor chaining - Implemented skip logic in
ProcessDictionarymethod to conditionally bypass non-string key entries
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs | Added newSkipUnsupportedTypes switch parameter with documentation and passed it to the context constructor |
src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/JsonObject.cs | AddedSkipUnsupportedTypes field toConvertToJsonContext, created new 7-parameter constructor, updated existing constructors to chain with defaultfalse value, and implemented skip logic inProcessDictionary method |
test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 | Added comprehensive test suite with 17 tests covering functionality, backward compatibility, constructor overloads, and edge cases with mixed dictionaries and nested objects |
💡Add Copilot custom instructions for smarter, more guided reviews.Learn how to get started.
iSazonov commentedDec 12, 2025
@yotsuda Looking the PR I think we need WG conclusion. I request them in related issue. You can share your thoughts there. Thanks for your efforts! |
PR Summary
Adds a new
-SkipUnsupportedTypesswitch parameter toConvertTo-Jsonthat allows dictionaries with non-string keys (likeException.Data) to be converted to JSON by silently skipping the problematic entries instead of throwing an error.Fixes#5749
PR Context
Problem
When converting objects containing dictionaries with non-string keys (such as
Exception.Datawhich usesIDictionarywithobjectkeys),ConvertTo-JsonthrowsNonStringKeyInDictionaryerror, making it impossible to serialize such objects.Solution
Added a new
-SkipUnsupportedTypesswitch parameter that, when specified, silently skips dictionary entries with non-string keys instead of throwing an error. This allows users to convert objects like exceptions to JSON while gracefully handling unsupported dictionary entries.PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerChanges Made
1.
ConvertToJsonCommand.cs(+9 lines)SkipUnsupportedTypesswitch parameter2.
JsonObject.cs(+36 lines, -1 line)SkipUnsupportedTypesfield toConvertToJsonContextstructskipUnsupportedTypesparameterProcessDictionarymethod to skip non-string key entries when enabled3.
ConvertTo-Json.Tests.ps1(+177 lines)Total: 3 files changed, 221 insertions(+), 1 deletion(-)
Behavior Examples
Before (throws error)
After (with -SkipUnsupportedTypes)
Without -SkipUnsupportedTypes (unchanged behavior)
Testing
Test Categories (17 tests total)
Backward Compatibility Tests (7 tests) - Pass on both production and build versions:
New Feature Tests (10 tests) - Pass on build version only:
Test Results
Implementation Details
Scope
This change only affects dictionary entries with non-string keys. All other JSON conversion behavior remains unchanged.
Design Decisions
Backward Compatibility
-SkipUnsupportedTypes) unchanged