forked from ilude/WindowsPowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.psm1
174 lines (156 loc) · 4.6 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#requires -version 2.0
# No help (yet) because I'm still changing and renaming everything every time I mess with this code
Add-Type -Assembly System.ServiceModel.Web, System.Runtime.Serialization
$utf8 = [System.Text.Encoding]::UTF8
function Convert-JsonToXml
{
PARAM([Parameter(ValueFromPipeline=$true)][string[]]$json)
BEGIN { $mStream = new-object System.IO.MemoryStream }
PROCESS {
$json | Write-String -Stream $mStream
}
END {
$mStream.Position = 0
$jsonReader = [System.Runtime.Serialization.Json.JsonReaderWriterFactory]::CreateJsonReader($mStream,[System.Xml.XmlDictionaryReaderQuotas]::Max)
try
{
$xml = new-object Xml.XmlDocument
$xml.Load($jsonReader)
$xml
}
finally
{
$jsonReader.Close()
$mStream.Dispose()
}
}
}
function Convert-XmlToJson
{
PARAM([Parameter(ValueFromPipeline=$true)][xml]$xml)
Process{
$mStream = new-object System.IO.MemoryStream
$jsonWriter = [System.Runtime.Serialization.Json.JsonReaderWriterFactory]::CreateJsonWriter($mStream)
try
{
$xml.Save($jsonWriter)
$bytes = $mStream.ToArray()
[System.Text.Encoding]::UTF8.GetString($bytes,0,$bytes.Length)
}
finally
{
$jsonWriter.Close()
$mStream.Dispose()
}
}
}
function ConvertFrom-Json {
PARAM( [Parameter(Mandatory=$true)][Type[]]$type, [Parameter(ValueFromPipeline=$true,Mandatory=$true)][String]$json )
PROCESS{
$ms = New-object IO.MemoryStream (,$utf8.GetBytes($json))
Import-Json $type $ms
$ms.dispose()
}
}
function Import-Json {
[CmdletBinding(DefaultParameterSetName="File")]
PARAM(
[Parameter(Mandatory=$true,Position=1)][Type[]]$type
,
[Parameter(Mandatory=$true,Position=2,ParameterSetName="Stream")][IO.Stream]$Stream
,
[Parameter(Mandatory=$true,Position=2,ParameterSetName="File")][String]$Path
)
BEGIN {
if($PSCmdlet.ParameterSetName -eq "File") {
$Stream = [IO.File]::Open($Path, "Read")
}
}
PROCESS{
if($type.Count -gt 1) {
$t,$types = @($type)
$js = New-Object System.Runtime.Serialization.Json.DataContractJsonSerializer $t, (,@($types))
} else {
$js = New-Object System.Runtime.Serialization.Json.DataContractJsonSerializer @($type)[0]
}
Write-Output $js.ReadObject($Stream)
}
END {
if($PSCmdlet.ParameterSetName -eq "File") {
$Stream.Dispose()
}
}
}
function Export-Json {
[CmdletBinding(DefaultParameterSetName="File")]
PARAM(
[Parameter(Mandatory=$true,Position=1)][Array]$InputObject
,
[Parameter(Mandatory=$true,Position=2,ParameterSetName="Stream")][IO.Stream]$Stream
,
[Parameter(Mandatory=$true,Position=2,ParameterSetName="File")][String]$Path
)
BEGIN {
if($PSCmdlet.ParameterSetName -eq "File") {
$Stream = [IO.File]::Open($Path, "Write")
}
}
PROCESS {
[type]$Type = @($InputObject)[0].GetType()
if($Type -isnot [Array]) { #$InputObject.Count -gt 1 -and
[type]$Type = "$($Type)[]"
}
[Type[]]$types = ($InputObject | select -expand PsTypeNames) | % { $_ -split "`n" -replace "^Selected\." } | Select -unique
#Write-Verbose $($Types | select -expand FullName | out-string)
#Write-Verbose "Stream: $($Stream.GetType())"
Write-Verbose "Output: $Type"
Write-Verbose "Input: $($InputObject.GetType())"
$js = New-Object System.Runtime.Serialization.Json.DataContractJsonSerializer $Type #, $Types #, ([int]::MaxValue), $false, $null, $false
$js.WriteObject( $stream, $InputObject )
}
END {
if($PSCmdlet.ParameterSetName -eq "File") {
$Stream.Dispose()
}
}
}
function ConvertTo-Json {
PARAM( [Parameter(ValueFromPipeline=$true,Mandatory=$true)]$object )
BEGIN {
[type]$lastType = $null
function Out-JsonString {
Param($items)
$ms = New-Object IO.MemoryStream
Export-Json $items.ToArray() $ms
$utf8.GetString( $ms.ToArray(), 0, $ms.Length )
$ms.Dispose()
}
}
PROCESS {
$thisType = $object.GetType()
if(!$lastType -or $lastType -ne $thisType) {
if($lastType) { Out-JsonString $items }
# make a new collection
$items = new-object "System.Collections.Generic.List[$thisType]"
}
$items.Add($object)
$lastType = $thisType
}
END {
Out-JsonString $items
}
}
function Write-String {
param([Parameter()]$stream,[Parameter(ValueFromPipeline=$true)]$string)
process {
$bytes = $utf8.GetBytes($string)
$stream.Write( $bytes, 0, $bytes.Length )
}
}
New-Alias fromjson ConvertFrom-Json
New-Alias tojson ConvertTo-Json
New-Alias cvfjs ConvertFrom-Json
New-Alias cvtjs ConvertTo-Json
New-Alias ipjs Import-Json
New-Alias epjs Export-Json
Export-ModuleMember -Function ConvertFrom-Json, Import-Json, Export-Json, ConvertTo-Json, Convert-JsonToXml, Convert-XmlToJson -Alias *