-
Notifications
You must be signed in to change notification settings - Fork 7
/
JSON 1.7.ps1
742 lines (697 loc) · 27.8 KB
/
JSON 1.7.ps1
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
#requires -version 2.0
# Version History:
# v 0.5 - First Public version
# v 1.0 - Made ConvertFrom-Json work with arbitrary JSON
# - switched to xsl style sheets for ConvertTo-JSON
# v 1.1 - Changed ConvertFrom-Json to handle single item results
# v 1.2 - CodeSigned to make a fellow geek happy
# v 1.3 - Changed ConvertFrom-Json to handle zero item results (I hope)
# v 1.4 - Added -File parmeter set to ConvertFrom-Json
# - Cleaned up some error messages
# v 1.5 - Corrected handling of arrays
# v 1.6 - Corrected pipeline binding on ConvertFrom-Json
# v 1.7 - Added a New-Json which converts simple hashtables...
#
# There is no help (yet) because I keep forgetting that I haven't written help yet.
# Full RoundTrip capability:
#
# > ls | ConvertTo-Json | ConvertFrom-Json
# > ps | ConvertTo-Json | Convert-JsonToXml | Convert-XmlToJson | convertFrom-Json
#
# You may frequently want to use the DEPTH or NoTypeInformation parameters:
#
# > ConvertTo-Json -Depth 2 -NoTypeInformation
#
# But then you have to specify the type when you reimport (and you can't do that for deep objects).
# This problem also occurs if you convert the result of a SELECT statement (ie: PSCustomObject).
# For Example:
#
# > PS | Select PM, WS, CPU, ID, ProcessName |
# >> ConvertTo-json -NoType |
# >> convertfrom-json -Type System.Diagnostics.Process
#
# However, you *can* use PSOjbect as your type when re-importing:
#
# > $Json = Get-Process |
# >> Select PM, WS, CPU, ID, ProcessName, @{n="SnapshotTime";e={Get-Date}} |
# >> ConvertTo-Json -NoType
#
# > $Json | ConvertFrom-json -Type PSObject
Add-Type -AssemblyName System.ServiceModel.Web, System.Runtime.Serialization
$utf8 = [System.Text.Encoding]::UTF8
function Write-Stream {
PARAM(
[Parameter(Position=0)]$stream,
[Parameter(ValueFromPipeline=$true)]$string
)
PROCESS {
$bytes = $utf8.GetBytes($string)
$stream.Write( $bytes, 0, $bytes.Length )
}
}
function Read-Stream {
PARAM(
[Parameter(Position=0,ValueFromPipeline=$true)]$Stream
)
process {
$bytes = $Stream.ToArray()
[System.Text.Encoding]::UTF8.GetString($bytes,0,$bytes.Length)
}}
function Convert-JsonToXml {
PARAM([Parameter(ValueFromPipeline=$true)][string[]]$json)
BEGIN {
$mStream = New-Object System.IO.MemoryStream
}
PROCESS {
$json | Write-Stream -Stream $mStream
}
END {
$mStream.Position = 0
try
{
$jsonReader = [System.Runtime.Serialization.Json.JsonReaderWriterFactory]::CreateJsonReader($mStream,[System.Xml.XmlDictionaryReaderQuotas]::Max)
$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 New-Json {
[CmdletBinding()]
param([Parameter(ValueFromPipeline=$true)][HashTable]$InputObject)
begin {
$ser = @{}
$jsona = @()
}
process {
$jsoni =
foreach($input in $InputObject.GetEnumerator() | Where { $_.Value } ) {
if($input.Value -is [Hashtable]) {
'"'+$input.Key+'": ' + (New-JSon $input.Value)
} else {
$type = $input.Value.GetType()
if(!$Ser.ContainsKey($Type)) {
$Ser.($Type) = New-Object System.Runtime.Serialization.Json.DataContractJsonSerializer $type
}
$stream = New-Object System.IO.MemoryStream
$Ser.($Type).WriteObject( $stream, $Input.Value )
'"'+$input.Key+'": ' + (Read-Stream $stream)
}
}
$jsona += "{`n" +($jsoni -join ",`n")+ "`n}"
}
end {
if($jsona.Count -gt 1) {
"[$($jsona -join ",`n")]"
} else {
$jsona
}
}}
## Rather than rewriting ConvertTo-Xml ...
Function ConvertTo-Json {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true,Position=1,ValueFromPipeline=$true)]$InputObject
,
[Parameter(Mandatory=$false)][Int]$Depth=1
,
[Switch]$NoTypeInformation
)
END {
## You must output ALL the input at once
## ConvertTo-Xml outputs differently if you just have one, so your results would be different
$input | ConvertTo-Xml -Depth:$Depth -NoTypeInformation:$NoTypeInformation -As Document | Convert-CliXmlToJson
}
}
Function Convert-CliXmlToJson {
PARAM(
[Parameter(ValueFromPipeline=$true)][Xml.XmlNode]$xml
)
BEGIN {
$xmlToJsonXsl = @'
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!--
CliXmlToJson.xsl
Copyright (c) 2006,2008 Doeke Zanstra
Copyright (c) 2009 Joel Bennett
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer. Redistributions in binary
form must reproduce the above copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other materials provided with
the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
-->
<xsl:output indent="no" omit-xml-declaration="yes" method="text" encoding="UTF-8" media-type="text/x-json"/>
<xsl:strip-space elements="*"/>
<!--contant-->
<xsl:variable name="d">0123456789</xsl:variable>
<!-- ignore document text -->
<xsl:template match="text()[preceding-sibling::node() or following-sibling::node()]"/>
<!-- string -->
<xsl:template match="text()">
<xsl:call-template name="escape-string">
<xsl:with-param name="s" select="."/>
</xsl:call-template>
</xsl:template>
<!-- Main template for escaping strings; used by above template and for object-properties
Responsibilities: placed quotes around string, and chain up to next filter, escape-bs-string -->
<xsl:template name="escape-string">
<xsl:param name="s"/>
<xsl:text>"</xsl:text>
<xsl:call-template name="escape-bs-string">
<xsl:with-param name="s" select="$s"/>
</xsl:call-template>
<xsl:text>"</xsl:text>
</xsl:template>
<!-- Escape the backslash (\) before everything else. -->
<xsl:template name="escape-bs-string">
<xsl:param name="s"/>
<xsl:choose>
<xsl:when test="contains($s,'\')">
<xsl:call-template name="escape-quot-string">
<xsl:with-param name="s" select="concat(substring-before($s,'\'),'\\')"/>
</xsl:call-template>
<xsl:call-template name="escape-bs-string">
<xsl:with-param name="s" select="substring-after($s,'\')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="escape-quot-string">
<xsl:with-param name="s" select="$s"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- Escape the double quote ("). -->
<xsl:template name="escape-quot-string">
<xsl:param name="s"/>
<xsl:choose>
<xsl:when test="contains($s,'"')">
<xsl:call-template name="encode-string">
<xsl:with-param name="s" select="concat(substring-before($s,'"'),'\"')"/>
</xsl:call-template>
<xsl:call-template name="escape-quot-string">
<xsl:with-param name="s" select="substring-after($s,'"')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="encode-string">
<xsl:with-param name="s" select="$s"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- Replace tab, line feed and/or carriage return by its matching escape code. Can't escape backslash
or double quote here, because they don't replace characters (� becomes \t), but they prefix
characters (\ becomes \\). Besides, backslash should be seperate anyway, because it should be
processed first. This function can't do that. -->
<xsl:template name="encode-string">
<xsl:param name="s"/>
<xsl:choose>
<!-- tab -->
<xsl:when test="contains($s,'	')">
<xsl:call-template name="encode-string">
<xsl:with-param name="s" select="concat(substring-before($s,'	'),'\t',substring-after($s,'	'))"/>
</xsl:call-template>
</xsl:when>
<!-- line feed -->
<xsl:when test="contains($s,'
')">
<xsl:call-template name="encode-string">
<xsl:with-param name="s" select="concat(substring-before($s,'
'),'\n',substring-after($s,'
'))"/>
</xsl:call-template>
</xsl:when>
<!-- carriage return -->
<xsl:when test="contains($s,'
')">
<xsl:call-template name="encode-string">
<xsl:with-param name="s" select="concat(substring-before($s,'
'),'\r',substring-after($s,'
'))"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise><xsl:value-of select="$s"/></xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- number (no support for javascript mantise) -->
<xsl:template match="text()[not(string(number())='NaN' or
(starts-with(.,'0' ) and . != '0'))]">
<xsl:value-of select="."/>
</xsl:template>
<!-- boolean, case-insensitive -->
<xsl:template match="text()[translate(.,'TRUE','true')='true']">true</xsl:template>
<xsl:template match="text()[translate(.,'FALSE','false')='false']">false</xsl:template>
<!-- root object(s) -->
<xsl:template match="*" name="base">
<xsl:if test="not(preceding-sibling::*)">
<xsl:choose>
<xsl:when test="count(../*)>1"><xsl:text>[</xsl:text></xsl:when>
<xsl:otherwise><xsl:text>{</xsl:text></xsl:otherwise>
</xsl:choose>
</xsl:if>
<xsl:call-template name="escape-string">
<xsl:with-param name="s" select="name()"/>
</xsl:call-template>
<xsl:text>:</xsl:text>
<!-- check type of node -->
<xsl:choose>
<!-- null nodes -->
<xsl:when test="count(child::node())=0">null</xsl:when>
<!-- other nodes -->
<xsl:otherwise>
<xsl:apply-templates select="child::node()"/>
</xsl:otherwise>
</xsl:choose>
<!-- end of type check -->
<xsl:if test="following-sibling::*">,</xsl:if>
<xsl:if test="not(following-sibling::*)">
<xsl:choose>
<xsl:when test="count(../*)>1"><xsl:text>]</xsl:text></xsl:when>
<xsl:otherwise><xsl:text>}</xsl:text></xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>
<!-- properties of objects -->
<xsl:template match="*[count(../*[name(../*)=name(.)])=count(../*) and count(../*)>1]">
<xsl:variable name="inArray" select="translate(local-name(),'OBJECT','object')='object' or ../@Type[starts-with(.,'System.Collections') or contains(.,'[]') or (contains(.,'[') and contains(.,']'))]"/>
<xsl:if test="not(preceding-sibling::*)">
<xsl:choose>
<xsl:when test="$inArray"><xsl:text>[</xsl:text></xsl:when>
<xsl:otherwise>
<xsl:text>{</xsl:text>
<xsl:if test="../@Type">
<xsl:text>"__type":</xsl:text>
<xsl:call-template name="escape-string">
<xsl:with-param name="s" select="../@Type"/>
</xsl:call-template>
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
<xsl:choose>
<xsl:when test="not(child::node())">
<xsl:call-template name="escape-string">
<xsl:with-param name="s" select="@Name"/>
</xsl:call-template>
<xsl:text>:null</xsl:text>
</xsl:when>
<xsl:when test="$inArray">
<xsl:apply-templates select="child::node()"/>
</xsl:when>
<!--
<xsl:when test="not(@Name) and not(@Type)">
<xsl:call-template name="escape-string">
<xsl:with-param name="s" select="local-name()"/>
</xsl:call-template>
<xsl:text>:</xsl:text>
<xsl:apply-templates select="child::node()"/>
</xsl:when>
-->
<xsl:when test="not(@Name)">
<xsl:call-template name="escape-string">
<xsl:with-param name="s" select="local-name()"/>
</xsl:call-template>
<xsl:text>:</xsl:text>
<xsl:apply-templates select="child::node()"/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="escape-string">
<xsl:with-param name="s" select="@Name"/>
</xsl:call-template>
<xsl:text>:</xsl:text>
<xsl:apply-templates select="child::node()"/>
</xsl:otherwise>
</xsl:choose>
<xsl:if test="following-sibling::*">,</xsl:if>
<xsl:if test="not(following-sibling::*)">
<xsl:choose>
<xsl:when test="$inArray"><xsl:text>]</xsl:text></xsl:when>
<xsl:otherwise><xsl:text>}</xsl:text></xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>
<!-- convert root element to an anonymous container -->
<xsl:template match="/">
<xsl:apply-templates select="node()"/>
</xsl:template>
</xsl:stylesheet>
'@
}
PROCESS {
if(Get-Member -InputObject $xml -Name root) {
Write-Verbose "Ripping to Objects"
$xml = $xml.root.Objects
} else {
Write-Verbose "Was already Objects"
}
Convert-Xml -Xml $xml -Xsl $xmlToJsonXsl
}
}
Function ConvertFrom-Xml {
[CmdletBinding(DefaultParameterSetName="AutoType")]
PARAM(
[Parameter(ValueFromPipeline=$true,Mandatory=$true,Position=1)]
[Xml.XmlNode]
$xml
,
[Parameter(Mandatory=$true,ParameterSetName="ManualType")]
[Type]$Type
,
[Switch]$ForceType
)
PROCESS{
if($xml.Item("root") -ne $null) {
return $xml.root.Objects | ConvertFrom-Xml
} elseif($xml.Item("Objects") -ne $null) {
return $xml.Objects | ConvertFrom-Xml
}
$propbag = @{}
foreach($name in Get-Member -InputObject $xml -MemberType Properties | Where-Object{$_.Name -notmatch "^__|type"} | Select-Object -ExpandProperty name) {
Write-Verbose "$Name Type: $($xml.$Name.type)"
$propbag."$Name" = Convert-Properties $xml."$name"
}
if(!$Type -and $xml.HasAttribute("__type")) { $Type = $xml.__Type }
if($ForceType -and $Type) {
try {
$output = New-Object $Type -Property $propbag
} catch {
$output = New-Object PSObject -Property $propbag
$output.PsTypeNames.Insert(0, $xml.__type)
}
} else {
if( $propbag.Count -eq 0) {
$output = New-Object PSObject
}
else {
$output = New-Object PSObject -Property $propbag
}
if($Type) {
$output.PsTypeNames.Insert(0, $Type)
}
}
Write-Output $output
}
}
Function Convert-Properties {
param($InputObject)
switch( $InputObject.type ) {
"object" {
return (ConvertFrom-Xml -Xml $InputObject)
break
}
"string" {
$MightBeADate = $InputObject.get_InnerText() -as [DateTime]
## Strings that are actually dates (*grumble* JSON is crap)
if($MightBeADate -and $propbag."$Name" -eq $MightBeADate.ToString("G")) {
return $MightBeADate
} else {
return $InputObject.get_InnerText()
}
break
}
"number" {
$number = $InputObject.get_InnerText()
if($number -eq ($number -as [int])) {
return $number -as [int]
} elseif($number -eq ($number -as [double])) {
return $number -as [double]
} else {
return $number -as [decimal]
}
break
}
"boolean" {
return [bool]::parse($InputObject.get_InnerText())
}
"null" {
return $null
}
"array" {
[object[]]$Items = $( foreach( $item in $InputObject.GetEnumerator() ) {
Convert-Properties $item
} )
return $Items
}
default {
return $InputObject
break
}
}
}
Function ConvertFrom-Json {
[CmdletBinding(DefaultParameterSetName="StringInput")]
PARAM(
[Parameter(Mandatory=$true,Position=1,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,ParameterSetName="File")]
[Alias("PSPath")]
[string]$File
,
[Parameter(ValueFromPipeline=$true,Mandatory=$true,Position=1,ParameterSetName="StringInput")]
[string]$InputObject
,
[Parameter(Mandatory=$true)]
[Type]$Type
,
[Switch]$ForceType
)
BEGIN {
[bool]$AsParameter = $PSBoundParameters.ContainsKey("File") -or $PSBoundParameters.ContainsKey("InputObject")
}
PROCESS {
if($PSCmdlet.ParameterSetName -eq "File") {
[string]$InputObject = @(Get-Content $File) -Join "`n"
$null = $PSBoundParameters.Remove("File")
}
else
{
$null = $PSBoundParameters.Remove("InputObject")
}
[Xml.XmlElement]$xml = (Convert-JsonToXml $InputObject).Root
if($xml) {
if($xml.Objects) {
$xml.Objects.Item.GetEnumerator() | ConvertFrom-Xml @PSBoundParameters
}elseif($xml.Item -and $xml.Item -isnot [System.Management.Automation.PSParameterizedProperty]) {
$xml.Item | ConvertFrom-Xml @PSBoundParameters
}else {
$xml | ConvertFrom-Xml @PSBoundParameters
}
} else {
Write-Error "Failed to parse JSON with JsonReader"
}
}
}
#########
### The JSON library is dependent on Convert-Xml from my Xml script module
function Convert-Node {
param(
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
[System.Xml.XmlReader]$XmlReader,
[Parameter(Position=1,Mandatory=$true,ValueFromPipeline=$false)]
[System.Xml.Xsl.XslCompiledTransform]$StyleSheet
)
PROCESS {
$output = New-Object IO.StringWriter
$StyleSheet.Transform( $XmlReader, $null, $output )
Write-Output $output.ToString()
}
}
function Convert-Xml {
#.Synopsis
# The Convert-XML function lets you use Xslt to transform XML strings and documents.
#.Description
#.Parameter Content
# Specifies a string that contains the XML to search. You can also pipe strings to Select-XML.
#.Parameter Namespace
# Specifies a hash table of the namespaces used in the XML. Use the format @{<namespaceName> = <namespaceUri>}.
#.Parameter Path
# Specifies the path and file names of the XML files to search. Wildcards are permitted.
#.Parameter Xml
# Specifies one or more XML nodes to search.
#.Parameter Xsl
# Specifies an Xml StyleSheet to transform with...
[CmdletBinding(DefaultParameterSetName="Xml")]
PARAM(
[Parameter(Position=1,ParameterSetName="Path",Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
[Alias("PSPath")]
[String[]]$Path
,
[Parameter(Position=1,ParameterSetName="Xml",Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
[Alias("Node")]
[System.Xml.XmlNode[]]$Xml
,
[Parameter(ParameterSetName="Content",Mandatory=$true,ValueFromPipeline=$true)]
[ValidateNotNullOrEmpty()]
[String[]]$Content
,
[Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$false)]
[ValidateNotNullOrEmpty()]
[Alias("StyleSheet")]
[String[]]$Xslt
)
BEGIN {
$StyleSheet = New-Object System.Xml.Xsl.XslCompiledTransform
if(Test-Path @($Xslt)[0] -ErrorAction 0) {
Write-Verbose "Loading Stylesheet from $(Resolve-Path @($Xslt)[0])"
$StyleSheet.Load( (Resolve-Path @($Xslt)[0]) )
} else {
Write-Verbose "$Xslt"
$StyleSheet.Load(([System.Xml.XmlReader]::Create((New-Object System.IO.StringReader ($Xslt -join "`n")))))
}
[Text.StringBuilder]$XmlContent = [String]::Empty
}
PROCESS {
switch($PSCmdlet.ParameterSetName) {
"Content" {
$null = $XmlContent.AppendLine( $Content -Join "`n" )
}
"Path" {
foreach($file in Get-ChildItem $Path) {
Convert-Node -Xml ([System.Xml.XmlReader]::Create((Resolve-Path $file))) $StyleSheet
}
}
"Xml" {
foreach($node in $Xml) {
Convert-Node -Xml (New-Object Xml.XmlNodeReader $node) $StyleSheet
}
}
}
}
END {
if($PSCmdlet.ParameterSetName -eq "Content") {
[Xml]$Xml = $XmlContent.ToString()
Convert-Node -Xml $Xml $StyleSheet
}
}
}
New-Alias fromjson ConvertFrom-Json
New-Alias tojson ConvertTo-Json
#New-Alias ipjs Import-Json
#New-Alias epjs Export-Json
#Import-Json, Export-Json,
# SIG # Begin signature block
# MIIRDAYJKoZIhvcNAQcCoIIQ/TCCEPkCAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB
# gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR
# AgEAAgEAAgEAAg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