Skip to content

Commit fc1b746

Browse files
author
Claus
committed
Merge branch 'UmbrellaInc-patch/ic-jtoken-tryparse' into dev
# Conflicts: # src/Umbraco.Deploy.Contrib.Connectors/ValueConnectors/InnerContentConnector.cs
2 parents e3c77f3 + 88b5938 commit fc1b746

File tree

1 file changed

+69
-47
lines changed

1 file changed

+69
-47
lines changed

src/Umbraco.Deploy.Contrib.Connectors/ValueConnectors/InnerContentConnector.cs

Lines changed: 69 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ public string GetValue(Property property, ICollection<ArtifactDependency> depend
102102

103103
// test if the value is a json object (thus could be a nested complex editor)
104104
// 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))
107107
{
108108
parsedValue = jtokenValue;
109109
}
@@ -174,60 +174,82 @@ public void SetValue(IContentBase content, string alias, string value)
174174
if (!mocks.TryGetValue(contentType, out mockContent))
175175
mockContent = mocks[contentType] = new Content("IC_" + Guid.NewGuid(), -1, contentType);
176176

177-
if (innerContentItem.PropertyValues != null)
177+
if (innerContentItem.PropertyValues != null)
178178
{
179-
foreach (var key in innerContentItem.PropertyValues.Keys.ToArray())
180-
{
181-
var propertyType = contentType.CompositionPropertyTypes.FirstOrDefault(x => x.Alias == key);
182179

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+
}
224225
}
225226

226227
// InnerContent does not use formatting when serializing JSON values
227228
value = JArray.FromObject(innerContent).ToString(Formatting.None);
228229
content.SetValue(alias, value);
229230
}
230231

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+
231253
/// <summary>
232254
/// The typed value stored for Inner Content
233255
/// </summary>

0 commit comments

Comments
 (0)