public static string CaseTransform(string propertyName, CaseTransformType type) { if (string.IsNullOrWhiteSpace(propertyName)) { return(propertyName); } string result = string.Empty; switch (type) { case CaseTransformType.CamelCase: result = Char.ToLowerInvariant(propertyName[0]) + propertyName.Substring(1); break; case CaseTransformType.LowerCase: result = propertyName.ToLowerInvariant(); break; case CaseTransformType.UpperCase: result = propertyName.ToUpperInvariant(); break; case CaseTransformType.OriginalCase: result = propertyName; break; default: throw new NotImplementedException(); } return(result); }
internal static string NormalizePath(string path, CaseTransformType caseTransformType) { // check for most common path errors on create. This is not // absolutely necessary, but it allows us to already catch mistakes // on creation of the patch document rather than on execute. if (path.Contains(".") || path.Contains("//") || path.Contains(" ") || path.Contains("\\")) { throw new JsonPatchException(string.Format("Provided string is not a valid path: {0}", path), null); } if (!(path.StartsWith("/"))) { path = "/" + path; } switch (caseTransformType) { case CaseTransformType.LowerCase: return(path.ToLowerInvariant()); case CaseTransformType.UpperCase: return(path.ToUpperInvariant()); case CaseTransformType.CamelCase: if (path.Length > 1) { return("/" + Char.ToLowerInvariant(path[1]) + path.Substring(2)); } else { return(path); } case CaseTransformType.OriginalCase: return(path); default: throw new NotImplementedException(); } }
private static string GetPath(Expression expr, CaseTransformType caseTransformType, bool firstTime) { switch (expr.NodeType) { case ExpressionType.ArrayIndex: var binaryExpression = (BinaryExpression)expr; if (ContinueWithSubPath(binaryExpression.Left.NodeType, false)) { var leftFromBinaryExpression = GetPath(binaryExpression.Left, caseTransformType, false); return(leftFromBinaryExpression + "/" + CaseTransform(binaryExpression.Right.ToString(), caseTransformType)); } else { return(CaseTransform(binaryExpression.Right.ToString(), caseTransformType)); } case ExpressionType.Call: var methodCallExpression = (MethodCallExpression)expr; if (ContinueWithSubPath(methodCallExpression.Object.NodeType, false)) { var leftFromMemberCallExpression = GetPath(methodCallExpression.Object, caseTransformType, false); return(leftFromMemberCallExpression + "/" + CaseTransform(GetIndexerInvocation(methodCallExpression.Arguments[0]), caseTransformType)); } else { return(CaseTransform(GetIndexerInvocation(methodCallExpression.Arguments[0]), caseTransformType)); } case ExpressionType.Convert: return(GetPath(((UnaryExpression)expr).Operand, caseTransformType, false)); case ExpressionType.MemberAccess: var memberExpression = expr as MemberExpression; if (ContinueWithSubPath(memberExpression.Expression.NodeType, false)) { var left = GetPath(memberExpression.Expression, caseTransformType, false); // if there's a JsonProperty attribute, we must return the PropertyName // from the attribute rather than the member name var jsonPropertyAttribute = memberExpression.Member.GetCustomAttributes( typeof(JsonPropertyAttribute), false); if (jsonPropertyAttribute.Length > 0) { // get value var castedAttribrute = jsonPropertyAttribute[0] as JsonPropertyAttribute; return(left + "/" + CaseTransform(castedAttribrute.PropertyName, caseTransformType)); } return(left + "/" + CaseTransform(memberExpression.Member.Name, caseTransformType)); } else { // Same here: if there's a JsonProperty attribute, we must return the PropertyName // from the attribute rather than the member name var jsonPropertyAttribute = memberExpression.Member.GetCustomAttributes( typeof(JsonPropertyAttribute), false); if (jsonPropertyAttribute.Length > 0) { // get value var castedAttribrute = jsonPropertyAttribute[0] as JsonPropertyAttribute; return(CaseTransform(castedAttribrute.PropertyName, caseTransformType)); } return(CaseTransform(memberExpression.Member.Name, caseTransformType)); } case ExpressionType.Parameter: // Fits "x => x" (the whole document which is "" as JSON pointer) return(firstTime ? "" : null); default: return(""); } }
/// <summary> /// Returns the path from a given expression. Takes JsonProperty into account. /// </summary> /// <typeparam name="T">Class type</typeparam> /// <typeparam name="TProp">Property type on class</typeparam> /// <param name="expr">Expression the path must be returned from</param> /// <returns>Property path</returns> public static string GetPath <T, TProp>(Expression <Func <T, TProp> > expr, CaseTransformType caseTransformType) where T : class { return("/" + GetPath(expr.Body, caseTransformType, true)); }
public JsonPatchDocument(CaseTransformType caseTransformType) { Operations = new List <Operation <TModel> >(); ContractResolver = new DefaultContractResolver(); CaseTransformType = caseTransformType; }
/// <summary> /// Create a new JsonPatchDocument, and pass in a CaseTransformType and /// custom contract resolver to use when applying the document. /// </summary> /// <param name="contractResolver">A custom IContractResolver</param> /// <param name="caseTransformType">Defines the case used when seralizing the object to JSON</param> public JsonPatchDocument(IContractResolver contractResolver, CaseTransformType caseTransformType) : this(new List <Operation <T> >(), contractResolver, caseTransformType) { }
/// <summary> /// Create a new JsonPatchDocument from a list of operations and pass in a CaseTransformType /// </summary> /// <param name="operations">A list of operations</param> /// <param name="caseTransformType">Defines the case used when seralizing the object to JSON</param> public JsonPatchDocument(List <Operation <T> > operations, CaseTransformType caseTransformType) : this(operations, new DefaultContractResolver(), caseTransformType) { }
public JsonPatchDocument(List <Operation <T> > operations, IContractResolver contractResolver, CaseTransformType caseTransformType) { Operations = operations; ContractResolver = contractResolver; CaseTransformType = caseTransformType; }
/// <summary> /// Create a new JsonPatchDocument and pass in a CaseTransformType /// </summary> /// <param name="caseTransformType">Defines the case used when seralizing the object to JSON</param> public JsonPatchDocument(CaseTransformType caseTransformType) : this(new List <Operation>(), new DefaultContractResolver(), caseTransformType) { }