8000 Add -SkipUnsupportedTypes parameter to ConvertTo-Json by yotsuda · Pull Request #26521 · PowerShell/PowerShell · GitHub 8000
[go: up one dir, main page]

Skip to content

Conversation

@yotsuda
Copy link
Contributor
@yotsuda yotsuda commented Nov 24, 2025

PR Summary

Adds a new -SkipUnsupportedTypes switch parameter to ConvertTo-Json that allows dictionaries with non-string keys (like Exception.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.Data which uses IDictionary with object keys), ConvertTo-Json throws NonStringKeyInDictionary error, making it impossible to serialize such objects.

Solution

Added a new -SkipUnsupportedTypes switch 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

Changes Made

1. ConvertToJsonCommand.cs (+9 lines)

  • Added SkipUnsupportedTypes switch parameter

2. JsonObject.cs (+36 lines, -1 line)

  • Added SkipUnsupportedTypes field to ConvertToJsonContext struct
  • Added new 7-parameter constructor overload with skipUnsupportedTypes parameter
  • Modified existing 3-parameter and 6-parameter constructors to chain to 7-parameter version
  • Added skip logic in ProcessDictionary method to skip non-string key entries when enabled

3. ConvertTo-Json.Tests.ps1 (+177 lines)

  • Added 17 comprehensive tests covering new functionality and backward compatibility

Total: 3 files changed, 221 insertions(+), 1 deletion(-)

Behavior Examples

Before (throws error)

$ex = [System.Exception]::new("Test")
$ex.Data.Add(1, "one")
$ex | ConvertTo-Json
# Error: NonStringKeyInDictionary

After (with -SkipUnsupportedTypes)

$ex = [System.Exception]::new("Test")
$ex.Data.Add(1, "one")
$ex | ConvertTo-Json -SkipUnsupportedTypes
# Successfully converts, skipping the non-string key entry in Data

Without -SkipUnsupportedTypes (unchanged behavior)

$ex | ConvertTo-Json
# Still throws NonStringKeyInDictionary error (backward compatible)

Testing

Test Categories (17 tests total)

Backward Compatibility Tests (7 tests) - Pass on both production and build versions:

  • String key dictionaries work without changes
  • Default error behavior unchanged
  • Complex objects work without changes
  • 3-parameter constructor callable
  • 6-parameter constructor callable

New Feature Tests (10 tests) - Pass on build version only:

  • Skip dictionary with non-string keys with SkipUnsupportedTypes
  • Convert Exception with SkipUnsupportedTypes
  • Mixed dictionary handling
  • Nested objects with unsupported types
  • 7-parameter constructor with SkipUnsupportedTypes
  • SkipUnsupportedTypes defaults to false in existing constructors

Test Results

Environment Passed Failed Status
Build version 17/17 0 ✅ All pass
Production version 7/17 10 ✅ Expected

Implementation Details

Scope

This change only affects dictionary entries with non-string keys. All other JSON conversion behavior remains unchanged.

Design Decisions

  1. Switch parameter - Simple on/off behavior, defaults to off (backward compatible)
  2. Silent skip - No warning when skipping entries (consistent with similar PowerShell behaviors)
  3. Entry-level granularity - Only skips problematic entries, not entire dictionaries
  4. Constructor chaining - Existing constructors chain to new 7-parameter version for maintainability

Backward Compatibility

  • API Level: Existing 3-parameter and 6-parameter constructors preserved and work identically
  • Behavior Level: Default behavior (without -SkipUnsupportedTypes) unchanged
  • Test Verification: Backward compatibility tests pass on production PowerShell

@iSazonov iSazonov added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Nov 24, 2025
Copy link
Contributor
Copilot AI left a 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 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 -SkipUnsupportedTypes switch parameter to control error handling behavior
  • Extended ConvertToJsonContext struct with new field and constructor overload while maintaining backward compatibility through constructor chaining
  • Implemented skip logic in ProcessDictionary method 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.

@iSazonov
Copy link
Collaborator

@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!

@microsoft-github-policy-service microsoft-github-policy-service bot removed the Review - Needed The PR is being reviewed label Dec 12, 2025
@microsoft-github-policy-service microsoft-github-policy-service bot added the Review - Needed The PR is being reviewed label Dec 19, 2025
@yotsuda
Copy link
Contributor Author
yotsuda commented Dec 22, 2025

Closing in favor of #26637, which takes a different approach to fix #5749.

Instead of skipping dictionary entries with non-string keys, the new PR converts them to strings via ToString(). This preserves the data rather than silently dropping it.

@yotsuda yotsuda closed this Dec 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log Review - Needed The PR is being reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Parameter to ConvertTo-Json to ignore unsupported properties

2 participants

0