mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Make ConvertFrom-Json deserialize an array of objects with multiple lines. (#3823)
* Fixing ConvertFrom-Json on CoreCLR to be able to handle a collection of strings which represent a JSON content. * Adding test case for ConvertFrom-Json to process an array of PSObjects as a single string.
This commit is contained in:
committed by
Travis Plunk
parent
40446c80a7
commit
7aa7f3858c
+21
-1
@@ -228,7 +228,6 @@ Describe "Json Tests" -Tags "Feature" {
|
||||
|
||||
# add a ScriptProperty called IsOld which returns whether the version is an older version
|
||||
$versionObject | Add-Member -MemberType ScriptProperty -Name IsOld -Value { ($this.Major -le 3) }
|
||||
|
||||
$jstr = ConvertTo-Json $versionObject
|
||||
|
||||
# convert the JSON string to a JSON object
|
||||
@@ -1455,4 +1454,25 @@ Describe "Json Bug fixes" -Tags "Feature" {
|
||||
{
|
||||
RunJsonTest $testCase
|
||||
}
|
||||
|
||||
It "ConvertFrom-Json deserializes an array of PSObjects (in multiple lines) as a single string." {
|
||||
|
||||
# Create an array of PSCustomObjects, and serialize it
|
||||
$array = [pscustomobject]@{ objectName = "object1Name"; objectValue = "object1Value" },
|
||||
[pscustomobject]@{ objectName = "object2Name"; objectValue = "object2Value" }
|
||||
|
||||
# Serialize the array to a text file
|
||||
$filePath = Join-Path $TESTDRIVE test.json
|
||||
$array | ConvertTo-Json | Out-File $filePath -Encoding utf8
|
||||
|
||||
# Read the object as an array of PSObjects and deserialize it.
|
||||
$result = Get-Content $filePath | ConvertFrom-Json
|
||||
$result.Count | Should be 2
|
||||
}
|
||||
|
||||
It "ConvertFrom-Json deserializes an array of strings (in multiple lines) as a single string." {
|
||||
|
||||
$result = "[1,","2,","3]" | ConvertFrom-Json
|
||||
$result.Count | Should be 3
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user