-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Closed
Labels
Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aideally 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 moreIssue has had no activity for 6 months or more
Description
Note:
-
I don't know whether / what other libraries are affected, and whether Newtonsoft.Jsonis an unusual one.
-
The bug also exists in Windows PowerShell.
-
The error is ultimately the same as in Working directly with certain .NET types, such as Newtonsoft.Json's, is broken: output formatting #10650:
Target type System.Collections.IEnumerator is not a value type or a non-abstract class. (Parameter 'targetType')- note how the type name is an interface, not a class.
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 -BeNullOrEmptyExpected 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.barstringifies 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
Labels
Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aideally 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 moreIssue has had no activity for 6 months or more