Using newtonsoft json.net with powershell

I have

function Foo($a, $b) { $o = @{} $oA = $a $oB = $b $post = @{} $post.X="x" $post.entity =$o $newton::SerializeObject($post) } 

then do

 foo "a" "b" 

I get

 Exception calling "SerializeObject" with "1" argument(s): "Self referencing loop detected for property 'Value' with type 'System.Management.Automation.PSParameterizedProperty'. Path 'entity.Members[0]'." 

but

 function Foo2($o) { $post = @{} $post.X="x" $post.entity =$o $newton::SerializeObject($post) } foo2 @{a="a"; b="b"} 

works great. Also

 function foo3($a, $b) { $o = @{} $oA = $a $oB = $b $newton::SerializeObject($o) } foo3 "a" "b" 

works but

 foo3 "a" 1 

can not

The latter can be made to work by doing

  $oB= [Int32]::Parse($b.Tostring()) 

That everything seems very strange

powershell v2 on windows 7, json.net 4.4.5

+7
source share
2 answers

The reference number of the self-regulatory cycle should be about ... the order in which you assign things. The following is an example:

 function Foo($a, $b) { $o = @{} $post = @{} $post.entity =$o $oA = $a $oB = $b $post.X="x" [Newtonsoft.Json.JsonConvert]::SerializeObject($post) } Foo "a" "b" {"entity":{"A":"a","B":"b"},"X":"x"} 

If you convert the type before passing it, it will keep your foo3 function common:

 function foo3($a, $b) { $o = @{} $oA = $a $oB = $b [Newtonsoft.Json.JsonConvert]::SerializeObject($o) } $var2 = [Int32]::Parse((1).Tostring()) Foo3 "a" $var2 {"A":"a","B":1} 
+2
source

JavaScriptSerializer from the .NET platform also has a similar issue with serializing PowerShell hashes. I suspect this is a little strange on a system like PowerShell. You can generally skip Json.Net and quit your own.

Below you can start with you. Most likely, it is not as reliable as the built-in ConvertTo-Json PowerShell 3 cmdlet, but I think it is mostly complete.

Here are all your examples in working condition.

 # See below for ConvertTo-Json.psm1 Import-Module ConvertTo-Json function Foo($a, $b) { $o = @{} $oA = $a $oB = $b $post = @{} $post.X="x" $post.entity =$o ConvertTo-Json $post } function Foo2($o) { $post = @{} $post.X="x" $post.entity =$o ConvertTo-Json $post } function foo3($a, $b) { $o = @{} $oA = $a $oB = $b ConvertTo-Json $o } PS> foo "a" "b" {"entity":{"A":"a","B":"b"},"X":"x"} PS> foo2 @{a="a"; b="b"} {"entity":{"a":"a","b":"b"},"X":"x"} PS> foo3 "a" "b" {"A":"a","B":"b"} PS> foo3 "a" 1 {"A":"a","B":1} 

And here is the PowerShell module that implements ConvertTo-Json .

 # Save these contents to Modules\ConvertTo-Json\ConvertTo-Json.psm1 in your # PowerShell documents folder, and load them in your $profile using the # "Import-Module ConvertTo-Json" cmdlet. This will make the ConvertTo-Json cmdlet # available for use. Set-StrictMode -Version Latest function convertToJsonNull($InputObject) { "null" } function convertToJsonArray($InputObject) { $value = ($InputObject | %{ convertToJson $_ }) -join ',' "[$value]" } function convertToJsonHash($InputObject) { $value = ($InputObject.Keys | %{ $name = $_ | asJsonString $itemValue = convertToJson ($InputObject[$_]) '"{0}":{1}' -f $name, $itemValue }) -join ',' "{$value}" } function convertToJsonObject($InputObject) { $value = ($InputObject | get-member -membertype *property | %{ $name = $_.Name $value = convertToJson ($InputObject.($name)) '"{0}":{1}' -f ($name | asJsonString), $value }) -join ',' "{$value}" } function convertToJsonString($InputObject) { '"{0}"' -f ($InputObject | asJsonString) } function convertToJsonBool($InputObject) { $InputObject.ToString().ToLower() } function convertToJsonNumeric($InputObject) { "$InputObject" } function convertToJsonDate($InputObject) { $epoch = [datetime]"1970-01-01T00:00:00Z" $elapsed = [long]($InputObject - $epoch).TotalMilliseconds '"\/Date({0})\/"' -f $elapsed } filter isNumeric() { $_ -is [byte] -or $_ -is [int16] -or $_ -is [int32] -or $_ -is [int64] -or $_ -is [sbyte] -or $_ -is [uint16] -or $_ -is [uint32] -or $_ -is [uint64] -or $_ -is [float] -or $_ -is [double] -or $_ -is [decimal] } filter asJsonString { ($_ -replace '\\', '\\') -replace '"', '\"' } function convertToJson($InputObject) { if ($InputObject -eq $null) { convertToJsonNull $InputObject } elseif ($InputObject -is [array]) { convertToJsonArray $InputObject } elseif ($InputObject -is [hashtable]) { convertToJsonHash $InputObject } elseif ($InputObject -is [datetime]) { convertToJsonDate $InputObject } elseif ($InputObject -is [string]) { convertToJsonString $InputObject } elseif ($InputObject -is [char]) { convertToJsonString $InputObject } elseif ($InputObject -is [bool]) { convertToJsonBool $InputObject } elseif ($InputObject | isNumeric) { convertToJsonNumeric $InputObject } else { convertToJsonObject $InputObject } } function ConvertTo-Json { [CmdletBinding()] param( [Parameter( ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true )] $InputObject ) convertToJson $InputObject } Export-ModuleMember -Function ConvertTo-Json 
+9
source

All Articles