Skip to content

Instantly share code, notes, and snippets.

@matthewoestreich
Last active November 3, 2019 04:57
Show Gist options
  • Save matthewoestreich/09887be0343aadd73f8f7bd4122dd018 to your computer and use it in GitHub Desktop.
Save matthewoestreich/09887be0343aadd73f8f7bd4122dd018 to your computer and use it in GitHub Desktop.
Add JSON data to TreeView Component [System.Windows.Forms.TreeView]
function Add-JsonToTreeview {
########################################################################################
# #
# The MIT License #
# #
# Copyright (c) 2019 Matt Oestreich. http://mattoestreich.com #
# #
# Permission is hereby granted, free of charge, to any person obtaining a copy #
# of this software and associated documentation files (the "Software"), to deal #
# in the Software without restriction, including without limitation the rights #
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell #
# copies of the Software, and to permit persons to whom the Software is #
# furnished to do so, subject to the following conditions: #
# #
# The above copyright notice, accreditation to Matt Oestreich, and this permission #
# notice shall be included in all copies or substantial portions of the Software. #
# #
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, #
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN #
# THE SOFTWARE. #
# #
########################################################################################
<#
.SYNOPSIS
Add JSON to TreeView
.DESCRIPTION
Add JSON to TreeView (System.Windows.Forms.TreeView) Component
.PARAMETER Json
JSON data (`-Json` can be a `[String]` or converted JSON string (aka `[PsCustomObject]`) converted using `ConvertFrom-Json`). See examples for more details.
.PARAMETER TreeView
The TreeView (System.Windows.Forms.TreeView) that you want to add JSON to
.PARAMETER ParentNode
This is here for recursion - you will most likely not need to use this Parameter
9.99 times out of 10.00 you wil not need to use this Parameter
.EXAMPLE
PS C:\> $SomeTreeView = [System.Windows.Forms.TreeView]::new()
PS C:\> $myJsonString = '{ "Root": [{ "Child1": "Value1" }, { "Child2": "Value2" }, { "Child3": "Value3" }] }'
PS C:\> $myJsonConverted = $myJsonString | ConvertFrom-Json
PS C:\> Add-JsonToTreeview -Json $myJsonConverted -TreeView $SomeTreeView
.EXAMPLE
PS C:\> $SomeTreeView = [System.Windows.Forms.TreeView]::new()
PS C:\> $myJsonString = '{ "Root": [{ "Child1": "Value1" }, { "Child2": "Value2" }, { "Child3": "Value3" }] }'
PS C:\> Add-JsonToTreeview -Json $myJsonString -TreeView $SomeTreeView
.NOTES
Matt Oestreich | http://mattoestreich.com | https://github.com/oze4
#>
param (
[Parameter(Mandatory)]
$Json,
[Parameter(Mandatory)]
[Windows.Forms.TreeView]$TreeView,
[Parameter()]
[Windows.Forms.TreeNode]$ParentNode
)
begin {
function New-TreeViewNode {
param (
[Parameter(Mandatory)][string]$Value
)
$NewNode = [Windows.Forms.TreeNode]::new($Value)
$NewNode.Name = $Value
return $NewNode
}
function Add-ObjectToTreeView {
param (
[Parameter(Mandatory)][System.Object[]]$Object,
[Parameter()][Windows.Forms.TreeNode]$AddToNode,
[Parameter(Mandatory)][Windows.Forms.TreeView]$TargetTreeView
)
$counter = 1
foreach ($objectProp in $Object) {
if ((($objectProp | Get-Member -Type NoteProperty).Count) -gt 1) {
$objectProp = "{ `"$($counter)`": $($objectProp | ConvertTo-Json) }" | ConvertFrom-Json
$counter++
}
Add-JsonToTreeview -Json $objectProp -ParentNode $AddToNode -TreeView $TargetTreeView
}
}
function Find-ParentNode {
param (
[Parameter(Mandatory)][Windows.Forms.TreeView]$TreeView,
[Parameter(Mandatory)][AllowNull()][Windows.Forms.TreeNode]$TreeNode
)
$parent = $TreeView
if ($TreeNode) { $parent = $TreeNode }
return $parent
}
}
process {
switch ($Json.GetType().Name) {
'PsCustomObject' {
foreach ($jsonProperty in $Json.PsObject.Properties) {
$node = New-TreeViewNode -Value $jsonProperty.Name
(Find-ParentNode -TreeView $TreeView -TreeNode $ParentNode).Nodes.Add($node)
if ($jsonProperty.GetType().Name -eq 'Object[]') {
Add-ObjectToTreeView -Object $jsonProperty -AddToNode $node -TargetTreeView $TreeView
} else {
Add-JsonToTreeview -Json $jsonProperty.Value -ParentNode $node -TreeView $TreeView
}
}
}
'Object[]' {
Add-ObjectToTreeView -Object $Json -AddToNode $ParentNode -TargetTreeView $TreeView
}
'String' {
try {
Add-JsonToTreeview -Json ($Json | ConvertFrom-Json) -ParentNode $ParentNode -TreeView $TreeView
} catch {
(Find-ParentNode -TreeView $TreeView -TreeNode $ParentNode).Nodes.Add((New-TreeViewNode -Value $Json))
}
}
}
if ($Json -is [ValueType]) {
try {
(Find-ParentNode -TreeView $TreeView -TreeNode $ParentNode).Nodes.Add((New-TreeViewNode -Value $Json.ToString()))
} catch { }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment