Skip to content

Working directly with certain .NET types, such as Newtonsoft.Json's, is broken: implicit stringification #10652

@mklement0

Description

@mklement0

Note:

The problem in a nutshell: when [JValue] instances are implicitly converted to strings, they stringify to the empty string.

By contrast, calling .ToString() explicitly works as expected.

Steps to reproduce

# Parse JSON text into a [JObject] instance.
$obj = [Newtonsoft.Json.JsonConvert]::DeserializeObject('{ "foo": "bar"  }')

# Works as expected:
# Call .ToString() explicitly on the `.foo` property, whose value is a [JValue] instance.
$obj.foo.ToString()

# Problem 1: default output formatting yields '' interactively,
#                   and fails with Out-Default
{ $obj.foo | Out-Default } | Should -Not -Throw

# Problem 2: string interpolation yields ''
"$($obj.foo)" | Should -Not -BeNullOrEmpty

Expected behavior

All tests should succeed.

Actual behavior

Tests 2 and 3 fail.

  • Test 2 fails due to an exception, which points to the underlying problem:
Target type System.Collections.IEnumerator is not a value type or a non-abstract class. 
(Parameter 'targetType').
  • Test 3 fails, because (as with interactive output), $foo.bar stringifies to the empty string.

Environment data

PowerShell Core v7.0.0-preview.4 on macOS 10.14.6
Windows PowerShell v5.1.18362.145 on Microsoft Windows 10 Pro (64-bit; Version 1903, OS Build: 18362.295)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-No ActivityIssue has had no activity for 6 months or more

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions