Skip to content

Commit 20e5968

Browse files
committed
corrections,
handling nested arrays, exceeding depth with arrays
1 parent 383c92a commit 20e5968

File tree

1 file changed

+29
-33
lines changed

1 file changed

+29
-33
lines changed

ConvertTo-CSON.ps1

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,7 @@ function ConvertTo-Cson {
9797
'false'
9898
}
9999
}
100-
elseif ($item -isnot [enum]) {
101-
$item
102-
}
103-
elseif ($EnumsAsStrings) {
100+
elseif ($item -isnot [enum] -or $EnumsAsStrings) {
104101
$item.ToString() | writeStringValue
105102
}
106103
else {
@@ -110,42 +107,45 @@ function ConvertTo-Cson {
110107
)"
111108
}
112109

113-
if ($level -le $Depth) {
114-
# write out key name, if one was supplied from the parent object
115-
if ($name) {
116-
"$indention$(
117-
# if a property name is not all simple characters or start with numeric digit, it must be quoted and escaped
118-
if ($name -match '[^\p{L}\d_]|^\d') {
119-
# property name requires escaping
120-
$name | writeStringValue
121-
}
122-
else {
123-
$name
124-
}
125-
):$(
126-
if ($item -is [array]) {
127-
' ['
128-
}
129-
elseif ($item -is [ValueType] -or $item -is [string]) {
130-
" $(writeValue $item '')"
131-
}
132-
)"
133-
}
134-
else {
110+
# write out key name, if one was supplied from the parent object
111+
if ($name) {
112+
"$indention$(
113+
# if a property name is not all simple characters or start with numeric digit, it must be quoted and escaped
114+
if ($name -match '[^\p{L}\d_]|^\d') {
115+
# property name requires escaping
116+
$name | writeStringValue
117+
}
118+
else {
119+
$name
120+
}
121+
):$(
135122
if ($item -is [array]) {
136-
"$indention[" # add array start token if property is an array
123+
' ['
137124
}
138-
elseif ($item -is [valuetype] -or $item -is [string]) {
139-
writeValue $item "$indention"
125+
elseif ($level -ge $Depth -or $item -is [ValueType] -or $item -is [string]) {
126+
" $(writeValue $item '')"
140127
}
128+
)"
129+
}
130+
else {
131+
if ($level -lt $Depth -and $item -is [array]) {
132+
"$indention[" # add array start token if property is an array
133+
}
134+
elseif ($level -ge $Depth -or $item -is [valuetype] -or $item -is [string]) {
135+
writeValue $item "$indention"
141136
}
137+
}
142138

139+
if ($level -lt $Depth) {
143140
if ($item -is [array]) {
144141
# handle arrays, iterate through the items in the array
145142
foreach ($subitem in $item) {
146143
if ($subitem -is [valuetype] -or $subitem -is [string]) {
147144
writeValue $subitem "$indention$Indent"
148145
}
146+
elseif ($subitem -is [array]) {
147+
writeProperty $null $subitem "$indention$Indent" ($level + 1)
148+
}
149149
else {
150150
"$indention$indent{"
151151
writeProperty $null $subitem "$indention$Indent" ($level + 1)
@@ -162,10 +162,6 @@ function ConvertTo-Cson {
162162
}
163163
}
164164
}
165-
else {
166-
# exceeded maximum depth, convert object to string
167-
"$indention$($item | writeStringValue)"
168-
}
169165
}
170166

171167
# start writing the property list, the property list should be an object, has no name, and starts at base level

0 commit comments

Comments
 (0)