Skip to content

Commit

Permalink
better data tollerance when jobjects contain jarrays and vice versa
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinJump committed Jun 2, 2021
1 parent 023de78 commit 29b88df
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
26 changes: 17 additions & 9 deletions uSync8.ContentEdition/Mapping/Mappers/NestedContentMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ public NestedContentMapper(

public override string GetExportValue(object value, string editorAlias)
{
var stringValue = GetValueAs<string>(value);
if (string.IsNullOrWhiteSpace(stringValue) || !stringValue.DetectIsJson()) return value.ToString();

var nestedJson = JsonConvert.DeserializeObject<JArray>(stringValue);
var nestedJson = GetItemArray(value);
if (nestedJson == null || !nestedJson.Any()) return value.ToString();

foreach (var item in nestedJson.Cast<JObject>())
Expand All @@ -49,11 +46,7 @@ public override string GetExportValue(object value, string editorAlias)

public override IEnumerable<uSyncDependency> GetDependencies(object value, string editorAlias, DependencyFlags flags)
{
var stringValue = GetValueAs<string>(value);
if (string.IsNullOrWhiteSpace(stringValue) || !stringValue.DetectIsJson())
return Enumerable.Empty<uSyncDependency>();

var nestedJson = JsonConvert.DeserializeObject<JArray>(stringValue);
var nestedJson = GetItemArray(value);
if (nestedJson == null || !nestedJson.Any())
return Enumerable.Empty<uSyncDependency>();

Expand All @@ -77,6 +70,21 @@ public override IEnumerable<uSyncDependency> GetDependencies(object value, strin

return dependencies;
}

private JArray GetItemArray(object value)
{
var stringValue = GetValueAs<string>(value);
if (string.IsNullOrWhiteSpace(stringValue) || !stringValue.DetectIsJson())
return null;

var token = JToken.Parse(stringValue);
switch (token)
{
case JArray array: return array;
case JObject obj: return new JArray(obj);
default: return null;
}
}
}
}

10 changes: 7 additions & 3 deletions uSync8.ContentEdition/Mapping/SyncNestedValueMapperBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,14 @@ protected JObject GetJsonValue(object value)
var stringValue = GetValueAs<string>(value);
if (string.IsNullOrWhiteSpace(stringValue)) return null;

var jsonValue = JsonConvert.DeserializeObject<JObject>(stringValue);
if (jsonValue == null) return null;
var token = JToken.Parse(stringValue);

return jsonValue;
switch(token)
{
case JObject obj: return obj;
case JArray array: return array.FirstOrDefault().ToObject<JObject>();
default: return null;
}
}

protected IContentType GetDocType(JObject json, string alias)
Expand Down

0 comments on commit 29b88df

Please sign in to comment.