private static void AddObject(JObject finalObject, IPath attributePath, JToken itemValue) { JObject jobject = (JObject)finalObject[attributePath.AttributePath]; IPath nextlevelAttribe = attributePath.ValuePath; if (nextlevelAttribe == null) { finalObject.Add(attributePath.AttributePath, jobject); } else { JObject emptyObject = new JObject(); finalObject.Add(attributePath.AttributePath, emptyObject); JToken item = itemValue[nextlevelAttribe.AttributePath]; ColumnsUtility.AddObject(emptyObject, nextlevelAttribe, item); } }
private static T BuildResouce <T>(IEnumerable <string> requestedAttributes, IEnumerable <string> excludedAttributes, string[] allwaysRetuned, T resource) { JObject result = new JObject(); Type objectType = resource.GetType(); JObject projectedResrouce = JObject.FromObject(resource); if (!requestedAttributes.Any() && !excludedAttributes.Any()) { return(resource); } foreach (string requestedAtt in requestedAttributes) { if (Path.TryParse(requestedAtt, out IPath attributePath)) { JToken itemValue = projectedResrouce[attributePath.AttributePath]; if (itemValue != null) { switch (itemValue.Type) { case JTokenType.Array: if (!attributePath.SubAttributes.Any()) { result.Add(attributePath.AttributePath, itemValue); } else { AddArray(result, attributePath, itemValue); } break; case JTokenType.Object: ColumnsUtility.AddObject(result, attributePath, itemValue); break; default: result.Add(attributePath.AttributePath, itemValue); break; } } } } foreach (string requestedAtt in allwaysRetuned) { if (Path.TryParse(requestedAtt, out IPath attributePath)) { JToken itemValue = projectedResrouce[attributePath.AttributePath]; if (itemValue != null) { switch (itemValue.Type) { case JTokenType.Array: if (!attributePath.SubAttributes.Any()) { result.Add(attributePath.AttributePath, itemValue); } else { ColumnsUtility.AddArray(result, attributePath, itemValue); } break; case JTokenType.Object: ColumnsUtility.AddObject(result, attributePath, itemValue); break; default: result.Add(attributePath.AttributePath, itemValue); break; } } } } if (!requestedAttributes.Any() && excludedAttributes.Any()) { result = projectedResrouce; } foreach (string excluded in excludedAttributes) { if (Path.TryParse(excluded, out IPath attributePath)) { JToken itemValue = projectedResrouce[attributePath.AttributePath]; if (itemValue != null) { switch (itemValue.Type) { case JTokenType.Array: if (!attributePath.SubAttributes.Any()) { result[attributePath.AttributePath].Remove(); } else { ColumnsUtility.ClearArray(result, attributePath); } break; case JTokenType.Object: if (attributePath.ValuePath == null) { result[attributePath.AttributePath].Remove(); } else { ColumnsUtility.ClearObject(result, attributePath); } break; default: result[attributePath.AttributePath].Remove(); break; } } } } return((T)result.ToObject(objectType)); }