public void GetSegments_Expected_LastSegmentIsCorrect() { JsonPath path = new JsonPath("EnumerableData().NestedData.NestedData.Name", "EnumerableData.NestedData.NestedData.Name"); string expected = "Name"; string actual = path.GetSegements().Last().ToString(); Assert.AreEqual(expected, actual); }
public void GetSegments_Expected_CorrectSegmentCount() { JsonPath path = new JsonPath("EnumerableData().NestedData.Name", "EnumerableData.NestedData.Name"); int expected = 3; int actual = path.GetSegements().Count(); Assert.AreEqual(expected, actual); }
public IEnumerable <object> SelectEnumerable(IPath path) { if (path == null) { throw new ArgumentNullException("path"); } JsonPath jsonPath = path as JsonPath; if (jsonPath == null) { throw new Exception(string.Format("Path of type '{0}' expected, path of type '{1}' received.", typeof(JsonPath), path.GetType())); } List <object> returnData; if (path.ActualPath == JsonPath.SeperatorSymbol) { returnData = new List <object> { Data }; } else if (path.ActualPath == JsonPath.EnumerableSymbol + JsonPath.SeperatorSymbol) { IEnumerable enumerableData = Data as IEnumerable; returnData = new List <object>(); if (enumerableData != null) { IEnumerator enumerator = enumerableData.GetEnumerator(); enumerator.Reset(); while (enumerator.MoveNext()) { returnData.Add(enumerator.Current); } } } else { returnData = new List <object>(SelectEnumberable(jsonPath.GetSegements().ToList(), Data as JToken).Select(o => o.ToString())); } return(returnData); }
public object SelectScalar(IPath path) { if (path == null) { throw new ArgumentNullException("path"); } JsonPath jsonPath = path as JsonPath; if (jsonPath == null) { throw new Exception(string.Format("Path of type '{0}' expected, path of type '{1}' received.", typeof(JsonPath), path.GetType())); } JToken currentData = Data as JToken; if (currentData == null) { throw new Exception(string.Format("Type of {0} was expected for data, type of {1} was found instead.", typeof(JToken), Data.GetType())); } if (path.ActualPath == JsonPath.SeperatorSymbol) { //nothing to do here yet } else if (path.ActualPath == JsonPath.EnumerableSymbol + JsonPath.SeperatorSymbol) { var enumerableData = currentData as IEnumerable; IEnumerator enumerator = enumerableData.GetEnumerator(); enumerator.Reset(); while (enumerator.MoveNext()) { currentData = enumerator.Current as JToken; } } else { List <IPathSegment> pathSegments = jsonPath.GetSegements().ToList(); int segmentIndex = 0; while (currentData != null && segmentIndex < pathSegments.Count) { if (pathSegments[segmentIndex].IsEnumarable) { IEnumerable enumerableData = GetEnumerableValueForPathSegment(pathSegments[segmentIndex], currentData); if (enumerableData == null) { currentData = null; } else { IEnumerator enumerator = enumerableData.GetEnumerator(); enumerator.Reset(); while (enumerator.MoveNext()) { currentData = enumerator.Current as JToken; } } } else { currentData = GetScalarValueForPathSegement(pathSegments[segmentIndex], currentData); } segmentIndex++; } } string returnVal = ""; if (currentData != null) { returnVal = currentData.ToString(); } return(returnVal); }