newtonsoft.json.psm1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
$asm = [Reflection.Assembly]::LoadFile("$PSScriptRoot\lib\Newtonsoft.Json.dll") function ConvertFrom-JObject($obj) { if ($obj -is [Newtonsoft.Json.Linq.JArray]) { $a = @() foreach($entry in $obj.GetEnumerator()) { $a += @(convertfrom-jobject $entry) } return $a } elseif ($obj -is [Newtonsoft.Json.Linq.JObject]) { $h = [ordered]@{} foreach($kvp in $obj.GetEnumerator()) { $val = convertfrom-jobject $kvp.value if ($kvp.value -is [Newtonsoft.Json.Linq.JArray]) { $val = @($val) } $h += @{ "$($kvp.key)" = $val } } return $h } elseif ($obj -is [Newtonsoft.Json.Linq.JValue]) { return $obj.Value } else { return $obj } } function ConvertFrom-JsonNewtonsoft([Parameter(Mandatory=$true,ValueFromPipeline=$true)]$string) { $obj = [Newtonsoft.Json.JsonConvert]::DeserializeObject($string, [Newtonsoft.Json.Linq.JObject]) return ConvertFrom-JObject $obj } function ConvertTo-JsonNewtonsoft([Parameter(Mandatory=$true,ValueFromPipeline=$true)]$obj) { return [Newtonsoft.Json.JsonConvert]::SerializeObject($obj, [Newtonsoft.Json.Formatting]::Indented) } Export-ModuleMember -Function ConvertFrom-JsonNewtonsoft,ConvertTo-JsonNewtonsoft |