functions/ConvertFrom-JsonWithArgs.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
<#
.SYNOPSIS
 
Deserializes the input object from JSON
.PARAMETER InputObject
 
JSON string
.PARAMETER MaxJsonLength
 
Maximum length to support for the input string, defaults to [int]::MaxValue
#>

function ConvertFrom-JsonWithArgs
{
    [CmdletBinding()]
    param
    (
        [parameter(ParameterSetName = 'object', ValueFromPipeline = $true, Position = 0, Mandatory = $true)]
        [string]$InputObject,

        [parameter(ParameterSetName = 'object', Position = 1, Mandatory = $false)]
        [int]$MaxJsonLength = [int]::MaxValue
    )

    process
    {
        function PopulateJsonFrom-Dictionary
        {
            param
            (
                [System.Collections.Generic.IDictionary`2[String,Object]]$InputObject
            )

            process
            {
                $returnObject = New-Object PSObject

                foreach($key in $InputObject.Keys)
                {
                    $pairObjectValue = $InputObject[$key]

                    if ($pairObjectValue -is [System.Collections.Generic.IDictionary`2].MakeGenericType([String],[Object]))
                    {
                        $pairObjectValue = PopulateJsonFrom-Dictionary $pairObjectValue
                    }
                    elseif ($pairObjectValue -is [System.Collections.Generic.ICollection`1].MakeGenericType([Object]))
                    {
                        $pairObjectValue = PopulateJsonFrom-Collection $pairObjectValue
                    }

                    $returnObject | Add-Member Noteproperty $key $pairObjectValue
                }

                return $returnObject
            }
        }

        function PopulateJsonFrom-Collection
        {
            param
            (
                [System.Collections.Generic.ICollection`1[Object]]$InputObject
            )

            process
            {
                $returnList = New-Object ([System.Collections.Generic.List`1].MakeGenericType([Object]))
                foreach($jsonObject in $InputObject)
                {
                    $jsonObjectValue = $jsonObject

                    if ($jsonObjectValue -is [System.Collections.Generic.IDictionary`2].MakeGenericType([String],[Object]))
                    {
                        $jsonObjectValue = PopulateJsonFrom-Dictionary $jsonObjectValue
                    }
                    elseif ($jsonObjectValue -is [System.Collections.Generic.ICollection`1].MakeGenericType([Object]))
                    {
                        $jsonObjectValue = PopulateJsonFrom-Collection $jsonObjectValue
                    }

                    $returnList.Add($jsonObjectValue) | Out-Null
                }

                return $returnList.ToArray()
            }
        }


        $scriptAssembly = [System.Reflection.Assembly]::LoadWithPartialName("System.Web.Extensions")

        $typeResolver = "public class JsonObjectTypeResolver : System.Web.Script.Serialization.JavaScriptTypeResolver
        {
          public override System.Type ResolveType(string id)
          {
            return typeof (System.Collections.Generic.Dictionary<string, object>);
          }
 
          public override string ResolveTypeId(System.Type type)
          {
            return string.Empty;
          }
        }"


        Add-Type -TypeDefinition $typeResolver -ReferencedAssemblies $scriptAssembly.FullName
        $jsonserial = New-Object System.Web.Script.Serialization.JavaScriptSerializer(New-Object JsonObjectTypeResolver)

        $jsonserial.MaxJsonLength = $MaxJsonLength

        $jsonTree = $jsonserial.DeserializeObject($InputObject)

        if ($jsonTree -is [System.Collections.Generic.IDictionary`2].MakeGenericType([String],[Object]))
        {
            $jsonTree = PopulateJsonFrom-Dictionary $jsonTree
        }
        elseif ($jsonTree -is [System.Collections.Generic.ICollection`1].MakeGenericType([Object]))
        {
            $jsonTree = PopulateJsonFrom-Collection $jsonTree
        }

        return $jsonTree
    }
}