private static void ClearObject(JToken finalObject, IPath attributePath) { //Assume that object allreay exists JToken token = finalObject[attributePath.AttributePath]; IPath nextLevelAttribute = attributePath.ValuePath; JToken internalProperty = token[nextLevelAttribute.AttributePath]; if (nextLevelAttribute.ValuePath != null) { internalProperty.Remove(); } else { ColumnsUtility.ClearObject(token, nextLevelAttribute); } }
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)); }