Add -SkipUnsupportedTypes parameter to ConvertTo-Json#26521
Add -SkipUnsupportedTypes parameter to ConvertTo-Json#26521yotsuda wants to merge 1 commit intoPowerShell:masterfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds a new -SkipUnsupportedTypes switch parameter to ConvertTo-Json that allows graceful handling of dictionaries with non-string keys (such as Exception.Data). When enabled, the cmdlet silently skips dictionary entries with non-string keys instead of throwing a NonStringKeyInDictionary 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 new SkipUnsupportedTypes switch parameter with documentation and passed it to the context constructor |
src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/JsonObject.cs |
Added SkipUnsupportedTypes field to ConvertToJsonContext, created new 7-parameter constructor, updated existing constructors to chain with default false value, and implemented skip logic in ProcessDictionary 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.
|
@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