@@ -69,30 +69,11 @@ function ConvertTo-Cson {
69
69
}
70
70
}
71
71
72
- function writeStringValue ([ string ] $value ) {
72
+ filter writeStringValue {
73
73
# write an escaped CSON string property value
74
74
# the purpose of making this a function, is a single place to change the escaping function used
75
75
# TODO: escape more characters!
76
- " "" $ ( $value -replace ' ([\x00-\x1F\x85\u2028\u2029])|([\\"]|#\{)' , $escape_replacer ) "" "
77
- }
78
-
79
- function writePropertyName ([string ]$value , [bool ]$isArray ) {
80
- # write an property name, processed as required for CSON
81
- # the purpose of making this a function, is a single place to change the escaping function used
82
- " $ (
83
- # if a property name is not all simple characters or start with numeric digit, it must be quoted and escaped
84
- if ($value -match ' [^\p{L}\d_]|^\d' ) {
85
- # property name requires escaping
86
- writeStringValue $value
87
- }
88
- else {
89
- $value
90
- }
91
- ) :$ (
92
- if ($isArray ) {
93
- ' ['
94
- }
95
- ) "
76
+ " "" $ ( $_ -replace ' ([\x00-\x1F\x85\u2028\u2029])|([\\"]|#\{)' , $escape_replacer ) "" "
96
77
}
97
78
98
79
function writeProperty ([string ]$name , $item , [string ]$indention , [int32 ]$level ) {
@@ -104,7 +85,7 @@ function ConvertTo-Cson {
104
85
" $indention $ (
105
86
if (($item -is [string ]) -or ($item -is [char ])) {
106
87
# handle strings or characters
107
- writeStringValue $item
88
+ $item | writeStringValue
108
89
}
109
90
else {
110
91
if ($item -is [boolean ]) {
@@ -120,7 +101,7 @@ function ConvertTo-Cson {
120
101
$item
121
102
}
122
103
elseif ($EnumsAsStrings ) {
123
- writeStringValue ( $item.ToString ())
104
+ $item.ToString () | writeStringValue
124
105
}
125
106
else {
126
107
$item.value__
@@ -133,9 +114,19 @@ function ConvertTo-Cson {
133
114
# write out key name, if one was supplied from the parent object
134
115
if ($name ) {
135
116
" $indention $ (
136
- writePropertyName $name ($item -is [array ])
137
- ) $ (
138
- if ($item -is [ValueType ] -or $item -is [string ]) {
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 ]) {
139
130
" $ ( writeValue $item ' ' ) "
140
131
}
141
132
) "
@@ -173,10 +164,10 @@ function ConvertTo-Cson {
173
164
}
174
165
else {
175
166
# exceeded maximum depth, convert object to string
176
- " $indention $ ( writeStringValue $item ) "
167
+ " $indention $ ( $item | writeStringValue ) "
177
168
}
178
169
}
179
170
180
171
# start writing the property list, the property list should be an object, has no name, and starts at base level
181
- (writeProperty $null $InputObject ' ' (-1 )) -join $ (if (-not $IsCoreCLR -or $IsWindows ) {" `r`n " } else {" `n " })
172
+ (writeProperty $null $InputObject ' ' (-1 )) -join $ (if (-not $IsCoreCLR -or $IsWindows ) { " `r`n " } else { " `n " })
182
173
}
0 commit comments