@@ -83,26 +83,30 @@ function ConvertTo-Cson {
83
83
function writeValue ($item , [string ]$indention ) {
84
84
# write a property value
85
85
" $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
89
89
}
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
+ }
103
95
else {
104
- $item .value__
96
+ ' false '
105
97
}
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__
106
110
}
107
111
) "
108
112
}
@@ -119,19 +123,19 @@ function ConvertTo-Cson {
119
123
$name
120
124
}
121
125
) :$ (
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
124
128
}
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 ) ) {
126
130
" $ ( writeValue $item ' ' ) "
127
131
}
128
132
) "
129
133
}
130
134
else {
131
- if ($level -lt $Depth -and $item -is [ array ] ) {
135
+ if (( $item -is [ array ]) -and ( $level -lt $Depth ) ) {
132
136
" $indention [" # add array start token if property is an array
133
137
}
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 ) ) {
135
139
writeValue $item " $indention "
136
140
}
137
141
}
@@ -140,30 +144,37 @@ function ConvertTo-Cson {
140
144
if ($item -is [array ]) {
141
145
# handle arrays, iterate through the items in the array
142
146
foreach ($subitem in $item ) {
143
- if ($subitem -is [valuetype ] -or $subitem -is [string ]) {
147
+ if (( $subitem -is [valuetype ]) -or ( $subitem -is [string ]) ) {
144
148
writeValue $subitem " $indention$Indent "
145
149
}
146
150
elseif ($subitem -is [array ]) {
147
- writeProperty $null $subitem " $indention$Indent " ($level + 1 )
151
+ writeProperty ' ' $subitem " $indention$Indent " ($level + 1 )
148
152
}
149
153
else {
150
154
" $indention$indent {"
151
- writeProperty $null $subitem " $indention$Indent " ($level + 1 )
155
+ writeProperty ' ' $subitem " $indention$Indent " ($level + 1 )
152
156
" $indention$Indent }"
153
157
}
154
158
}
155
159
" $indention ]"
156
160
}
157
161
elseif ($item -isnot [valuetype ] -and $item -isnot [string ]) {
158
162
# 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
+ }
162
173
}
163
174
}
164
175
}
165
176
}
166
177
167
178
# 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 " })
169
180
}
0 commit comments