@@ -102,8 +102,8 @@ public string GetValue(Property property, ICollection<ArtifactDependency> depend
102
102
103
103
// test if the value is a json object (thus could be a nested complex editor)
104
104
// if that's the case we'll need to add it as a json object instead of string to avoid it being escaped
105
- var jtokenValue = parsedValue != null && parsedValue . ToString ( ) . DetectIsJson ( ) ? JToken . Parse ( parsedValue . ToString ( ) ) : null ;
106
- if ( jtokenValue != null )
105
+ JToken jtokenValue ;
106
+ if ( TryParseJToken ( parsedValue , out jtokenValue ) )
107
107
{
108
108
parsedValue = jtokenValue ;
109
109
}
@@ -174,60 +174,82 @@ public void SetValue(IContentBase content, string alias, string value)
174
174
if ( ! mocks . TryGetValue ( contentType , out mockContent ) )
175
175
mockContent = mocks [ contentType ] = new Content ( "IC_" + Guid . NewGuid ( ) , - 1 , contentType ) ;
176
176
177
- if ( innerContentItem . PropertyValues != null )
177
+ if ( innerContentItem . PropertyValues != null )
178
178
{
179
- foreach ( var key in innerContentItem . PropertyValues . Keys . ToArray ( ) )
180
- {
181
- var propertyType = contentType . CompositionPropertyTypes . FirstOrDefault ( x => x . Alias == key ) ;
182
179
183
- if ( propertyType == null )
184
- {
185
- LogHelper . Debug < InnerContentConnector > ( $ "No Property Type found with alias { key } on Content Type { contentType . Alias } ") ;
186
- continue ;
187
- }
188
-
189
- // throws if not found - no need for a null check
190
- var propValueConnector = ValueConnectors . Get ( propertyType ) ;
191
-
192
- var rowValue = innerContentItem . PropertyValues [ key ] ;
193
-
194
- if ( rowValue != null )
195
- {
196
- propValueConnector . SetValue ( mockContent , propertyType . Alias , rowValue . ToString ( ) ) ;
197
- var convertedValue = mockContent . GetValue ( propertyType . Alias ) ;
198
- // integers needs to be converted into strings
199
- if ( convertedValue is int )
200
- {
201
- innerContentItem . PropertyValues [ key ] = convertedValue . ToString ( ) ;
202
- }
203
- else
204
- {
205
- // test if the value is a json object (thus could be a nested complex editor)
206
- // if that's the case we'll need to add it as a json object instead of string to avoid it being escaped
207
- var jtokenValue = convertedValue . ToString ( ) . DetectIsJson ( ) ? JToken . Parse ( convertedValue . ToString ( ) ) : null ;
208
- if ( jtokenValue != null )
209
- {
210
- innerContentItem . PropertyValues [ key ] = jtokenValue ;
211
- }
212
- else
213
- {
214
- innerContentItem . PropertyValues [ key ] = convertedValue ;
215
- }
216
- }
217
- }
218
- else
219
- {
220
- innerContentItem . PropertyValues [ key ] = rowValue ;
221
- }
222
- }
223
- }
180
+ foreach ( var key in innerContentItem . PropertyValues . Keys . ToArray ( ) )
181
+ {
182
+ var propertyType = contentType . CompositionPropertyTypes . FirstOrDefault ( x => x . Alias == key ) ;
183
+
184
+ if ( propertyType == null )
185
+ {
186
+ LogHelper . Debug < InnerContentConnector > ( $ "No Property Type found with alias { key } on Content Type { contentType . Alias } ") ;
187
+ continue ;
188
+ }
189
+
190
+ // throws if not found - no need for a null check
191
+ var propValueConnector = ValueConnectors . Get ( propertyType ) ;
192
+
193
+ var rowValue = innerContentItem . PropertyValues [ key ] ;
194
+
195
+ if ( rowValue != null )
196
+ {
197
+ propValueConnector . SetValue ( mockContent , propertyType . Alias , rowValue . ToString ( ) ) ;
198
+ var convertedValue = mockContent . GetValue ( propertyType . Alias ) ;
199
+ // integers needs to be converted into strings
200
+ if ( convertedValue is int )
201
+ {
202
+ innerContentItem . PropertyValues [ key ] = convertedValue . ToString ( ) ;
203
+ }
204
+ else
205
+ {
206
+ // test if the value is a json object (thus could be a nested complex editor)
207
+ // if that's the case we'll need to add it as a json object instead of string to avoid it being escaped
208
+ JToken jtokenValue ;
209
+ if ( TryParseJToken ( convertedValue , out jtokenValue ) )
210
+ {
211
+ innerContentItem . PropertyValues [ key ] = jtokenValue ;
212
+ }
213
+ else
214
+ {
215
+ innerContentItem . PropertyValues [ key ] = convertedValue ;
216
+ }
217
+ }
218
+ }
219
+ else
220
+ {
221
+ innerContentItem . PropertyValues [ key ] = rowValue ;
222
+ }
223
+ }
224
+ }
224
225
}
225
226
226
227
// InnerContent does not use formatting when serializing JSON values
227
228
value = JArray . FromObject ( innerContent ) . ToString ( Formatting . None ) ;
228
229
content . SetValue ( alias , value ) ;
229
230
}
230
231
232
+ private static bool TryParseJToken ( object value , out JToken json )
233
+ {
234
+ json = default ( JToken ) ;
235
+
236
+ if ( value == null )
237
+ return false ;
238
+
239
+ var s = value . ToString ( ) ;
240
+ if ( string . IsNullOrWhiteSpace ( s ) || s . DetectIsJson ( ) == false )
241
+ return false ;
242
+
243
+ // we're okay with an empty catch here as that just means 'json' will stay null and we return false
244
+ try
245
+ {
246
+ json = JToken . Parse ( s ) ;
247
+ }
248
+ catch { }
249
+
250
+ return json != null ;
251
+ }
252
+
231
253
/// <summary>
232
254
/// The typed value stored for Inner Content
233
255
/// </summary>
0 commit comments