@@ -97,10 +97,7 @@ function ConvertTo-Cson {
97
97
' false'
98
98
}
99
99
}
100
- elseif ($item -isnot [enum ]) {
101
- $item
102
- }
103
- elseif ($EnumsAsStrings ) {
100
+ elseif ($item -isnot [enum ] -or $EnumsAsStrings ) {
104
101
$item.ToString () | writeStringValue
105
102
}
106
103
else {
@@ -110,42 +107,45 @@ function ConvertTo-Cson {
110
107
) "
111
108
}
112
109
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
+ ) :$ (
135
122
if ($item -is [array ]) {
136
- " $indention [ " # add array start token if property is an array
123
+ ' [ '
137
124
}
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 ' ' ) "
140
127
}
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 "
141
136
}
137
+ }
142
138
139
+ if ($level -lt $Depth ) {
143
140
if ($item -is [array ]) {
144
141
# handle arrays, iterate through the items in the array
145
142
foreach ($subitem in $item ) {
146
143
if ($subitem -is [valuetype ] -or $subitem -is [string ]) {
147
144
writeValue $subitem " $indention$Indent "
148
145
}
146
+ elseif ($subitem -is [array ]) {
147
+ writeProperty $null $subitem " $indention$Indent " ($level + 1 )
148
+ }
149
149
else {
150
150
" $indention$indent {"
151
151
writeProperty $null $subitem " $indention$Indent " ($level + 1 )
@@ -162,10 +162,6 @@ function ConvertTo-Cson {
162
162
}
163
163
}
164
164
}
165
- else {
166
- # exceeded maximum depth, convert object to string
167
- " $indention $ ( $item | writeStringValue) "
168
- }
169
165
}
170
166
171
167
# start writing the property list, the property list should be an object, has no name, and starts at base level
0 commit comments