Open aslatter opened 3 years ago
@aslatter I noticed that since Powershell 6 the ConvertFrom-Json supports comments that start with two forward slashes and multi-line comments. I went on to test some stuff out and see what would be acceptable in jsonc format to get a better understanding of how Powershell 7 could be interpreting it.
This is valid in jsonc:
I'm curious if because ConvertFrom-Json takes a JavaScript Object Notation (JSON) formatted string and JSON start with an object with key-value pairs inside it that it takes your array and puts it into a global empty key and everything else becomes a value?
In my (admittedly quick) reading of RFC 7159 (or http://www.json.org) it looks like a JSON document may be any JSON value (array, object, or scalar), not just an object.
I just ran into this as well, with PowerShell 7.4.
Bug still exists as described
To nail down the bug, which affects nested arrays too:
ConvertFrom-Json
is built on NewtonSoft.Json, and uses it as follows (from what I can tell, NewtonSoft.Json offers no control over how it handles comments, based on the settings available via the JsonSerializer
class):
As desired, it ignores comments:
By contrast, it unexpectedly not only preserves comments inside arrays (whether nested or not), but inappropriately converts them to string elements.
So it looks like the fix is simple as enclosing the above assignments in if (element.Type != Comment) { ... }
According to #7436 ConvertFrom-Json should support "JSONC"-style comments present in JSON objects without issue.
However this doesn't seem to work if the comments are in a "top level" array - instead the comments are interpreted as strings-elements within the array.
Steps to reproduce
Expected behavior
Either an error (due to comments not being supported) or:
Actual behavior
Environment data