PowerShell cmdlet is changing the type of the returned object -


this behavior mystifying!

consider following powershell script:

[reflection.assembly]::loadfrom("newtonsoft.json.dll") | out-null  function convertfrom-jsonnet {     [cmdletbinding()]     param(         [parameter(mandatory=$true)]         [string] $json     )      $o = [newtonsoft.json.linq.jobject]::parse($json)     write-host $o.gettype().name      return $o }  clear-host  $json = '{"test":"prop"}' $o1 = convertfrom-jsonnet '{"test":"prop"}' $o2 = [newtonsoft.json.linq.jobject]::parse($json)  write-host $o1.gettype().name write-host $o2.gettype().name 

you'd expect output be:

jobject jobject jobject 

but it's not! it's:

jobject jproperty jobject 

how possible? how type of object within function jobject, after it's passed out of function, it's jproperty?

sigh

yay powershell's inflexibility!

apparently, powershell "unroll" collections destined pipeline. in case, jobject implements icollection<keyvaluepair<string, jtoken>>. jobject's collection contains single jproperty, being "unrolled" pipeline. found this answer, shows rolling collection outer collection cause intended value placed in pipeline.

wouldn't nice if powershell had mechanism adding pipeline untouched? :)


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -