Working directly with certain .NET types, such as Newtonsoft.Json's, is broken: output formatting
See original GitHub issueNote:
-
I don’t know whether / what other libraries are affected, or whether Newtonsoft.Json is an unusual one.
-
The bug surfaces only if the input JSON text contains an array.
-
The bug is a regression from Windows PowerShell, where it doesn’t surface.
-
The error is ultimately the same as in #10652:
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.
Steps to reproduce
# Parse JSON text into a [JObject] instance.
# Note: To surface the bug, the text must contain an ARRAY
$obj = [Newtonsoft.Json.JsonConvert]::DeserializeObject('{ "foo": [ "bar" ] }')
# Apply default output formatting, which fails due to the bug:
{ $obj | Out-String } | Should -Not -Throw
Expected behavior
The test should succeed.
Actual behavior
The test fails, because the implicit output formatting threw an exception.
Target type System.Collections.IEnumerator is not a value type or a non-abstract class.
(Parameter 'targetType')
PowerShell Core 7.0.0-preview.4
Issue Analytics
- State:
- Created 4 years ago
- Comments:12 (6 by maintainers)
Top Results From Across the Web
Could not load file or assembly 'Newtonsoft.Json' or one of ...
To solve this, I ensured all my projects used the same version by running the following command and checking the results:
Read more >Migrate from Newtonsoft.Json to System.Text.Json - .NET
Json accepts non-string values, such as a number or the literals true and false , for deserialization to properties of type string.
Read more >Newtonsoft Json package
I had to remove the newtonsoft.Json that some addons are using in my project, because in 2020.x it gets stuck in a loop...
Read more >data binding and serialization issue with System.Text.Json ...
I am using the new System.Text.Json in asp.net core and I noticed that the grid does not bind to the model when using...
Read more >Working with Newtonsoft.Json in C# & VB
In this article, we will see how to work with standard value types, custom value types, compress structures to types, and transform from...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
PropertyGetterWorker method enumerates recursively and it is called for a value making conversion to enumerator with JValue.JValueDynamicProxy.TryConvert - the last method throw. I think only debug shows that happens in PropertyGetterWorker.
Since no regression is from Windows PowerShell I think it makes no sense to invest to NewtonSoft Json.NET. If there are other similar scenarios, then we can consider improving this PropertyGetterWorker method.