I am running a Start-Job which returns a nested hashtable. This table/object can contain nested arrays or nested hashtables or even hashtables in arrays. When i try to serialize it using the "New-Object System.Web.Script.Serialization.JavaScriptSerializer",
I get the following error: "A circular reference was detected while serializing an object of type 'System.Management.Automation.PSParameterizedProperty'."
Here is my code:
[void] ([System.Reflection.Assembly]::LoadWithPartialName("System.Web.Extensions"))
$sscript = {
Write-Output -InputObject @(@{name= "maros"},@{name= "Peter"})
}
$j = Start-Job -ScriptBlock $sscript | Wait-Job | Receive-Job
$javaScriptSerializer = New-Object System.Web.Script.Serialization.JavaScriptSerializer
$javaScriptSerializer.MaxJsonLength = [System.Int32]::MaxValue
$javaScriptSerializer.RecursionLimit = 99
$javaScriptSerializer.Serialize($j)
results in :
Exception calling "Serialize" with "1" argument(s): "A circular reference was detected while serializing an object of type
'System.Management.Automation.PSParameterizedProperty'."
At C:\Users\takacm\AppData\Local\Temp\1a3fa01e-7904-4697-a682-ec12bb2d7625.ps1:13 char:1
+ $javaScriptSerializer.Serialize($j)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : InvalidOperationException
However, replacing $j with the same input as the script block output does not throw any error and works as expected !
$javaScriptSerializer = New-Object System.Web.Script.Serialization.JavaScriptSerializer
$javaScriptSerializer.MaxJsonLength = [System.Int32]::MaxValue
$javaScriptSerializer.RecursionLimit = 99
$javaScriptSerializer.Serialize(@(@{name= "maros"},@{name= "Peter"}))
results in: [{"name":"maros"},{"name":"Peter"}]
Any ideas as to why the Receive-Job Cmdlet causes my output to throw an error when serialized ?
Any help with this would be very much appreciated.
Thanks a lot
Maros
by the way, I am running PS2.0 and can`t upgrade to PS3 or PS4.