Updates to json2.4.1 to resolve issues with reading nested array objects

This commit is contained in:
Jason DeVito
2019-06-03 13:30:13 -05:00
parent 40b8ae6219
commit 2b79071bd8
2 changed files with 51 additions and 50 deletions

View File

@@ -11,8 +11,7 @@ namespace PepperDash.Core.JsonToSimpl
public class JsonToSimplArrayLookupChild : JsonToSimplChildObjectBase
{
public string SearchPropertyName { get; set; }
public string SearchPropertyValue { get; set; }
public string AppendToPathPrefix { get; set; }
public string SearchPropertyValue { get; set; }
int ArrayIndex;
@@ -46,11 +45,12 @@ namespace PepperDash.Core.JsonToSimpl
/// <param name="searchPropertyValue"></param>
public void InitializeWithAppend(string file, string key, string pathPrefix, string pathAppend,
string pathSuffix, string searchPropertyName, string searchPropertyValue)
{
base.Initialize(file, key, pathPrefix, pathSuffix);
{
string pathPrefixWithAppend = (pathPrefix != null ? pathPrefix : "") + GetPathAppend(pathAppend);
base.Initialize(file, key, pathPrefixWithAppend, pathSuffix);
SearchPropertyName = searchPropertyName;
SearchPropertyValue = searchPropertyValue;
AppendToPathPrefix = pathAppend;
SearchPropertyValue = searchPropertyValue;
}
@@ -63,11 +63,10 @@ namespace PepperDash.Core.JsonToSimpl
/// <returns></returns>
protected override string GetFullPath(string path)
{
return string.Format("{0}[{1}]{2}.{3}{4}",
PathPrefix == null ? "" : PathPrefix,
ArrayIndex,
GetPathAppend(),
path,
return string.Format("{0}[{1}].{2}{3}",
PathPrefix == null ? "" : PathPrefix,
ArrayIndex,
path,
PathSuffix == null ? "" : PathSuffix);
}
@@ -81,9 +80,8 @@ namespace PepperDash.Core.JsonToSimpl
/// Provides the path append for GetFullPath
/// </summary>
/// <returns></returns>
string GetPathAppend()
string GetPathAppend(string a)
{
var a = AppendToPathPrefix;
if (string.IsNullOrEmpty(a))
{
return "";
@@ -109,42 +107,45 @@ namespace PepperDash.Core.JsonToSimpl
if (Master.JsonObject == null)
throw new InvalidOperationException("Cannot do operations before master JSON has read");
if (PathPrefix == null)
throw new InvalidOperationException("Cannot do operations before PathPrefix is set");
var token = Master.JsonObject.SelectToken(PathPrefix);
if (token is JArray)
{
var array = token as JArray;
try
{
var item = array.FirstOrDefault(o =>
{
var prop = o[SearchPropertyName];
return prop != null && prop.Value<string>()
.Equals(SearchPropertyValue, StringComparison.OrdinalIgnoreCase);
});
if (item == null)
{
Debug.Console(1, "JSON Child[{0}] Array '{1}' '{2}={3}' not found: ", Key,
PathPrefix, SearchPropertyName, SearchPropertyValue);
this.LinkedToObject = false;
return false;
}
this.LinkedToObject = true;
ArrayIndex = array.IndexOf(item);
OnStringChange(GetFullPath(""), 0, JsonToSimplConstants.FullPathToArrayChange);
Debug.Console(1, "JSON Child[{0}] Found array match at index {1}", Key, ArrayIndex);
return true;
}
catch (Exception e)
{
Debug.Console(1, "JSON Child[{0}] Array '{1}' lookup error: '{2}={3}'\r{4}", Key,
PathPrefix, SearchPropertyName, SearchPropertyValue, e);
}
throw new InvalidOperationException("Cannot do operations before PathPrefix is set");
var token = Master.JsonObject.SelectToken(PathPrefix);
if (token is JArray)
{
var array = token as JArray;
try
{
var item = array.FirstOrDefault(o =>
{
var prop = o[SearchPropertyName];
return prop != null && prop.Value<string>()
.Equals(SearchPropertyValue, StringComparison.OrdinalIgnoreCase);
});
if (item == null)
{
Debug.Console(1, "JSON Child[{0}] Array '{1}' '{2}={3}' not found: ", Key,
PathPrefix, SearchPropertyName, SearchPropertyValue);
this.LinkedToObject = false;
return false;
}
this.LinkedToObject = true;
ArrayIndex = array.IndexOf(item);
OnStringChange(string.Format("{0}[{1}]", PathPrefix, ArrayIndex), 0, JsonToSimplConstants.FullPathToArrayChange);
Debug.Console(1, "JSON Child[{0}] Found array match at index {1}", Key, ArrayIndex);
return true;
}
catch (Exception e)
{
Debug.Console(1, "JSON Child[{0}] Array '{1}' lookup error: '{2}={3}'\r{4}", Key,
PathPrefix, SearchPropertyName, SearchPropertyValue, e);
}
}
else
{
Debug.Console(1, "JSON Child[{0}] Path '{1}' is not an array", Key, PathPrefix);
}
else
Debug.Console(1, "JSON Child[{0}] Path '{1}' is not an array", Key, PathPrefix);
return false;
}

View File

@@ -28,7 +28,7 @@ namespace PepperDash.Core.JsonToSimpl
/// This will be prepended to all paths to allow path swapping or for more organized
/// sub-paths
/// </summary>
public string PathPrefix { get; protected set; }
public string PathPrefix { get; protected set; }
/// <summary>
/// This is added to the end of all paths
@@ -285,7 +285,7 @@ namespace PepperDash.Core.JsonToSimpl
protected virtual string GetFullPath(string path)
{
return (PathPrefix != null ? PathPrefix : "") +
path + (PathSuffix != null ? PathSuffix : "");
path + (PathSuffix != null ? PathSuffix : "");
}
// Helpers for events