Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Open
yotsuda wants to merge1 commit intoPowerShell:master
base:master
Choose a base branch
Loading
fromyotsuda:fix-issue-5749-skipunsupportedtypes

Conversation

@yotsuda
Copy link
Contributor

PR Summary

Adds a new-SkipUnsupportedTypes switch parameter toConvertTo-Json that 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 asException.Data which usesIDictionary withobject keys),ConvertTo-Json throwsNonStringKeyInDictionary 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)

  • AddedSkipUnsupportedTypes switch parameter

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

  • AddedSkipUnsupportedTypes field toConvertToJsonContext struct
  • Added new 7-parameter constructor overload withskipUnsupportedTypes parameter
  • Modified existing 3-parameter and 6-parameter constructors to chain to 7-parameter version
  • Added skip logic inProcessDictionary 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

EnvironmentPassedFailedStatus
Build version17/170✅ All pass
Production version7/1710✅ 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

@iSazonoviSazonov added the CL-GeneralIndicates that a PR should be marked as a general cmdlet change in the Change Log labelNov 24, 2025
Copy link
Contributor

CopilotAI 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 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-SkipUnsupportedTypes switch parameter to control error handling behavior
  • ExtendedConvertToJsonContext struct with new field and constructor overload while maintaining backward compatibility through constructor chaining
  • Implemented skip logic inProcessDictionary 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.

FileDescription
src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.csAdded newSkipUnsupportedTypes switch parameter with documentation and passed it to the context constructor
src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/JsonObject.csAddedSkipUnsupportedTypes 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.ps1Added 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!

yotsuda reacted with thumbs up emoji

@microsoft-github-policy-servicemicrosoft-github-policy-servicebot removed the Review - NeededThe PR is being reviewed labelDec 12, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

Copilot code reviewCopilotCopilot left review comments

@SteveL-MSFTSteveL-MSFTAwaiting requested review from SteveL-MSFT

At least 1 approving review is required to merge this pull request.

Assignees

No one assigned

Labels

CL-GeneralIndicates that a PR should be marked as a general cmdlet change in the Change Log

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Add Parameter to ConvertTo-Json to ignore unsupported properties

2 participants

@yotsuda@iSazonov

[8]ページ先頭

©2009-2025 Movatter.jp