Skip to content

Commit 5efb09f

Browse files
committed
corrections, enhancement
correct handling of depth limit for some objects enhance support of hashtable with blank keys.
1 parent 20e5968 commit 5efb09f

File tree

1 file changed

+40
-29
lines changed

1 file changed

+40
-29
lines changed

ConvertTo-CSON.ps1

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -83,26 +83,30 @@ function ConvertTo-Cson {
8383
function writeValue ($item, [string]$indention) {
8484
# write a property value
8585
"$indention$(
86-
if (($item -is [string]) -or ($item -is [char])) {
87-
# handle strings or characters
88-
$item | writeStringValue
86+
if (($item -is [string]) -or ($item -is [char]) -or (($item -is [enum]) -and $EnumsAsStrings) -or ($level -ge $Depth)) {
87+
# handle strings or characters, or objects exceeding the max depth
88+
"$item" | writeStringValue
8989
}
90-
else {
91-
if ($item -is [boolean]) {
92-
# handle boolean type
93-
if ($item) {
94-
'true'
95-
}
96-
else {
97-
'false'
98-
}
99-
}
100-
elseif ($item -isnot [enum] -or $EnumsAsStrings) {
101-
$item.ToString() | writeStringValue
102-
}
90+
elseif ($item -is [boolean]) {
91+
# handle boolean type
92+
if ($item) {
93+
'true'
94+
}
10395
else {
104-
$item.value__
96+
'false'
10597
}
98+
}
99+
elseif ($item -is [datetime]) {
100+
# specifically format date/time to ISO 8601
101+
$item.ToString('o') | writeStringValue
102+
}
103+
elseif ($item -isnot [enum]) {
104+
# assuming a [valuetype] that doesn't need special treatment
105+
$item
106+
}
107+
else {
108+
# specifically out the enum value
109+
$item.value__
106110
}
107111
)"
108112
}
@@ -119,19 +123,19 @@ function ConvertTo-Cson {
119123
$name
120124
}
121125
):$(
122-
if ($item -is [array]) {
123-
' ['
126+
if (($item -is [array]) -and ($level -lt $Depth)) {
127+
' [' # add array start token if property is an array
124128
}
125-
elseif ($level -ge $Depth -or $item -is [ValueType] -or $item -is [string]) {
129+
elseif (($item -is [ValueType]) -or ($item -is [string]) -or ($level -ge $Depth)) {
126130
" $(writeValue $item '')"
127131
}
128132
)"
129133
}
130134
else {
131-
if ($level -lt $Depth -and $item -is [array]) {
135+
if (($item -is [array]) -and ($level -lt $Depth)) {
132136
"$indention[" # add array start token if property is an array
133137
}
134-
elseif ($level -ge $Depth -or $item -is [valuetype] -or $item -is [string]) {
138+
elseif (($item -is [valuetype]) -or ($item -is [string]) -or ($level -ge $Depth)) {
135139
writeValue $item "$indention"
136140
}
137141
}
@@ -140,30 +144,37 @@ function ConvertTo-Cson {
140144
if ($item -is [array]) {
141145
# handle arrays, iterate through the items in the array
142146
foreach ($subitem in $item) {
143-
if ($subitem -is [valuetype] -or $subitem -is [string]) {
147+
if (($subitem -is [valuetype]) -or ($subitem -is [string])) {
144148
writeValue $subitem "$indention$Indent"
145149
}
146150
elseif ($subitem -is [array]) {
147-
writeProperty $null $subitem "$indention$Indent" ($level + 1)
151+
writeProperty '' $subitem "$indention$Indent" ($level + 1)
148152
}
149153
else {
150154
"$indention$indent{"
151-
writeProperty $null $subitem "$indention$Indent" ($level + 1)
155+
writeProperty '' $subitem "$indention$Indent" ($level + 1)
152156
"$indention$Indent}"
153157
}
154158
}
155159
"$indention]"
156160
}
157161
elseif ($item -isnot [valuetype] -and $item -isnot [string]) {
158162
# handle objects by recursing with writeProperty
159-
# iterate through the items (force to a PSCustomObject for consistency)
160-
foreach ($property in ([PSCustomObject]$item).psobject.Properties) {
161-
writeProperty $property.Name $property.Value $(if ($level -ge 0) { "$indention$Indent" } else { $indention }) ($level + 1)
163+
if ($item.GetType().Name -in 'HashTable', 'OrderedDictionary') {
164+
# process what we assume is a hashtable object
165+
foreach ($hash in $item.GetEnumerator()) {
166+
writeProperty $hash.Key $hash.Value $(if ($level -ge 0) { "$indention$Indent" } else { $indention }) ($level + 1)
167+
}
168+
} else {
169+
# iterate through the items (force to a PSCustomObject for consistency)
170+
foreach ($property in ([PSCustomObject]$item).psobject.Properties) {
171+
writeProperty $property.Name $property.Value $(if ($level -ge 0) { "$indention$Indent" } else { $indention }) ($level + 1)
172+
}
162173
}
163174
}
164175
}
165176
}
166177

167178
# start writing the property list, the property list should be an object, has no name, and starts at base level
168-
(writeProperty $null $InputObject '' (-1)) -join $(if (-not $IsCoreCLR -or $IsWindows) { "`r`n" } else { "`n" })
179+
(writeProperty '' $InputObject '' (-1)) -join $(if (-not $IsCoreCLR -or $IsWindows) { "`r`n" } else { "`n" })
169180
}

0 commit comments

Comments
 (0)