Add -SkipUnsupportedTypes parameter to ConvertTo-Json #26521
Closed
+222
−1
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.
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