Skip to content

Commit

Permalink
Fixes #162 better handling when a string "looks" like JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Jump committed Oct 1, 2020
1 parent 0373a9b commit 7433a1a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 16 deletions.
6 changes: 6 additions & 0 deletions uSync8.BackOffice/SyncHandlers/SyncHandlerRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -802,10 +802,14 @@ public virtual IEnumerable<uSyncAction> ReportFolder(string folder, HandlerSetti
{
List<uSyncAction> actions = new List<uSyncAction>();


var files = GetImportFiles(folder);

int count = 0;
int total = files.Count();

logger.Debug(handlerType, "ReportFolder: {folder} ({count} files)", folder, total);

foreach (string file in files)
{
count++;
Expand Down Expand Up @@ -906,6 +910,8 @@ protected IEnumerable<uSyncAction> ReportItem(string file, HandlerSettings confi
{
try
{
logger.Debug(handlerType, "Report Item {file}", file);

var node = syncFileService.LoadXElement(file);
if (ShouldImport(node, config))
{
Expand Down
10 changes: 2 additions & 8 deletions uSync8.ContentEdition/Mapping/Mappers/GridMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Umbraco.Core;
using Umbraco.Core.Services;

using uSync8.Core;
using uSync8.Core.Dependency;

namespace uSync8.ContentEdition.Mapping.Mappers
Expand Down Expand Up @@ -94,14 +95,7 @@ private string ProcessGridValues(string gridContent, Func<IEnumerable<ISyncMappe
var result = callback(mappers, alias, value);
if (result != string.Empty)
{
if (result.DetectIsJson())
{
control["value"] = JToken.Parse(result);
}
else
{
control["value"] = result;
}
control["value"] = result.GetJsonTokenValue();
}
}
}
Expand Down
10 changes: 2 additions & 8 deletions uSync8.ContentEdition/Mapping/SyncNestedValueMapperBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Umbraco.Core.Models;
using Umbraco.Core.Services;

using uSync8.Core;
using uSync8.Core.Dependency;

namespace uSync8.ContentEdition.Mapping
Expand Down Expand Up @@ -47,14 +48,7 @@ protected JObject GetExportProperties(JObject item, IContentType docType)
if (value != null)
{
var mappedVal = SyncValueMapperFactory.GetExportValue(value, property.PropertyEditorAlias);
if (mappedVal.DetectIsJson())
{
item[property.Alias] = JToken.Parse(mappedVal);
}
else
{
item[property.Alias] = mappedVal;
}
item[property.Alias] = mappedVal.GetJsonTokenValue();
}
}
}
Expand Down
39 changes: 39 additions & 0 deletions uSync8.Core/Extensions/JsonExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

using Newtonsoft.Json.Linq;

using Umbraco.Core;

namespace uSync8.Core
{
public static class JsonExtensions
{
/// <summary>
/// get a JToken value of a string.
/// </summary>
/// <remarks>
/// if the string is valid JSON then you will get a parsed
/// version of the json.
///
/// if it isn't then you just get the string value (which will cast
/// automatically to JToken when you need it).
/// </remarks>
public static JToken GetJsonTokenValue(this string value)
{
if (value.DetectIsJson())
{
try
{
return JToken.Parse(value);
}
catch
{
// error parsing, so it's not actually json
// it just might look like it a bit.
return value;
}
}

return value;
}
}
}
1 change: 1 addition & 0 deletions uSync8.Core/uSync8.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
<Compile Include="Dependency\SyncDependencyCollection.cs" />
<Compile Include="Dependency\uSyncDependency.cs" />
<Compile Include="Extensions\ChangeListExtensions.cs" />
<Compile Include="Extensions\JsonExtensions.cs" />
<Compile Include="Extensions\ListExtensions.cs" />
<Compile Include="Extensions\StringExtensions.cs" />
<Compile Include="Extensions\XElementExtensions.cs" />
Expand Down

0 comments on commit 7433a1a

Please sign in to comment.