public static object valueof(string jsonPath, JUSTContext context) { JToken token = context.Input; JToken selectedToken = token.SelectToken(jsonPath); return(GetValue(selectedToken)); }
private static object CallCustomFunction(object[] parameters, JUSTContext localContext) { object[] customParameters = new object[parameters.Length - 3]; string functionString = string.Empty; string dllName = string.Empty; int i = 0; foreach (object parameter in parameters) { if (i == 0) { dllName = parameter.ToString(); } else if (i == 1) { functionString = parameter.ToString(); } else if (i != (parameters.Length - 1)) { customParameters[i - 2] = parameter; } i++; } int index = functionString.LastIndexOf("."); string className = functionString.Substring(0, index); string functionName = functionString.Substring(index + 1, functionString.Length - index - 1); className = className + "," + dllName; return(ReflectionHelper.caller(null, className, functionName, customParameters, true, localContext ?? GlobalContext)); }
public static bool existsandnotempty(string jsonPath, JUSTContext context) { JToken token = context.Input; JToken selectedToken = token.SelectToken(jsonPath); return(selectedToken != null && selectedToken.ToString().Trim() != string.Empty); }
public static bool exists(string jsonPath, JUSTContext context) { JToken token = context.Input; JToken selectedToken = token.SelectToken(jsonPath); return(selectedToken != null); }
public static JObject Transform(JObject transformer, JToken input, JUSTContext localContext = null) { (localContext ?? GlobalContext).Input = input; string inputJson = JsonConvert.SerializeObject(input); RecursiveEvaluate(transformer, inputJson, null, null, localContext); return(transformer); }
private static string Delete(string argument, string inputJson, JUSTContext localContext) { var result = ParseArgument(inputJson, null, null, argument, localContext) as string; if (result == null) { throw new ArgumentException("Invalid jsonPath for #delete!"); } return(result); }
public static object currentproperty(JArray array, JToken currentElement, JUSTContext context) { var prop = (currentElement.First as JProperty); if (prop == null && context.EvaluationMode == EvaluationMode.Strict) { throw new InvalidOperationException("Element is not a property: " + prop.ToString()); } return(prop.Name); }
public static string substring(string stringRef, int startIndex, int length, JUSTContext context) { try { return(stringRef.Substring(startIndex, length)); } catch { return(null); } }
private static JToken Copy(string argument, string inputJson, JUSTContext localContext) { var jsonPath = ParseArgument(inputJson, null, null, argument, localContext) as string; if (jsonPath == null) { throw new ArgumentException("Invalid jsonPath for #copy!"); } JToken selectedToken = GetInputToken(localContext).SelectToken(jsonPath); return(selectedToken); }
public static object average(JArray parsedArray, JUSTContext context) { decimal result = 0; if (parsedArray != null) { foreach (JToken token in parsedArray.Children()) { result += Convert.ToDecimal(token.ToString()); } } return(TypedNumber(result / parsedArray.Count)); }
public static JArray Transform(JArray transformerArray, string input, JUSTContext localContext = null) { var result = new JArray(); foreach (var transformer in transformerArray) { if (transformer.Type != JTokenType.Object) { throw new NotSupportedException($"Transformer of type '{transformer.Type}' not supported!"); } Transform(transformer as JObject, input, localContext); result.Add(transformer); } return(result); }
public static object xadd(object[] list) { JUSTContext context = list[list.Length - 1] as JUSTContext; decimal add = 0; for (int i = 0; i < list.Length - 1; i++) { if (list[i] != null) { add += (decimal)ReflectionHelper.GetTypedValue(typeof(decimal), list[i], context.EvaluationMode); } } return(TypedNumber(add)); }
public static object min(JArray parsedArray, JUSTContext context) { decimal result = decimal.MaxValue; if (parsedArray != null) { foreach (JToken token in parsedArray.Children()) { decimal thisValue = Convert.ToDecimal(token.ToString()); result = Math.Min(result, thisValue); } } return(TypedNumber(result)); }
public static object averageatpath(JArray parsedArray, string jsonPath, JUSTContext context) { decimal result = 0; if (parsedArray != null) { foreach (JToken token in parsedArray.Children()) { JToken selectedToken = token.SelectToken(jsonPath); result += Convert.ToDecimal(selectedToken.ToString()); } } return(TypedNumber(result / parsedArray.Count)); }
public static object minatpath(JArray parsedArray, string jsonPath, JUSTContext context) { decimal result = decimal.MaxValue; if (parsedArray != null) { foreach (JToken token in parsedArray.Children()) { JToken selectedToken = token.SelectToken(jsonPath); decimal thisValue = Convert.ToDecimal(selectedToken.ToString()); result = Math.Min(result, thisValue); } } return(TypedNumber(result)); }
public static JArray Transform(JArray transformerArray, JToken input, JUSTContext localContext = null) { var result = new JArray(); for (int i = 0; i < transformerArray.Count; i++) { var transformer = transformerArray[i]; if (transformer.Type != JTokenType.Object) { throw new NotSupportedException($"Transformer of type '{transformer.Type}' not supported!"); } Transform(transformer as JObject, input, localContext); result.Add(transformer); } return(result); }
private static JToken GetToken(object newValue, JUSTContext localContext) { JToken result = null; if (newValue != null) { if (newValue is JToken token) { result = token; } else { try { //JToken newToken = JToken.FromObject(newValue); if (newValue is IEnumerable <object> newArray) { result = new JArray(newArray); } else { result = new JValue(newValue); } } catch { if (IsStrictMode(localContext)) { throw; } if (IsFallbackToDefault(localContext)) { result = JValue.CreateNull(); } } } } else { result = JValue.CreateNull(); } return(result); }
public static string concatall(JArray parsedArray, JUSTContext context) { string result = null; if (parsedArray != null) { foreach (JToken token in parsedArray.Children()) { if (result == null) { result = string.Empty; } result += token.ToString(); } } return(result); }
public static JArray Transform(JArray transformerArray, string input, JUSTContext localContext = null) { var result = new JArray(); var items = transformerArray.ToArray(); int itemsLength = items.Length; for (int i = 0; i < itemsLength; i++) { var transformer = items[i]; if (transformer.Type != JTokenType.Object) { throw new NotSupportedException($"Transformer of type '{transformer.Type}' not supported!"); } Transform(transformer as JObject, input, localContext); result.Add(transformer); } return(result); }
public static int length(object val, JUSTContext context) { int result = 0; if (val is IEnumerable enumerable) { var enumerator = enumerable.GetEnumerator(); while (enumerator.MoveNext()) { result++; } } else { if (context.EvaluationMode == EvaluationMode.Strict) { throw new ArgumentException($"Argument not elegible for #length: {val.ToString()}"); } } return(result); }
public static string concatallatpath(JArray parsedArray, string jsonPath, JUSTContext context) { string result = null; if (parsedArray != null) { foreach (JToken token in parsedArray.Children()) { JToken selectedToken = token.SelectToken(jsonPath); if (result == null) { result = string.Empty; } result += selectedToken.ToString(); } } return(result); }
public static string Transform(string transformerJson, string inputJson, JUSTContext localContext = null) { JToken result = null; JToken transformerToken = JsonConvert.DeserializeObject <JToken>(transformerJson); switch (transformerToken.Type) { case JTokenType.Object: result = Transform(transformerToken as JObject, inputJson, localContext); break; case JTokenType.Array: result = Transform(transformerToken as JArray, inputJson, localContext); break; default: throw new NotSupportedException($"Transformer of type '{transformerToken.Type}' not supported!"); } string output = JsonConvert.SerializeObject(result); return(output); }
private static void RecursiveEvaluate(JToken parentToken, string inputJson, JArray parentArray, JToken currentArrayToken, JUSTContext localContext) { if (parentToken == null) { return; } JToken[] tokens = parentToken.Children().ToArray(); List <JToken> selectedTokens = null; Dictionary <string, JToken> tokensToReplace = null; List <JToken> tokensToDelete = null; List <string> loopProperties = null; JArray arrayToForm = null; List <JToken> tokenToForm = null; List <JToken> tokensToAdd = null; bool isLoop = false; int tokensLength = tokens.Length; for (int i = 0; i < tokensLength; i++) { JToken childToken = tokens[i]; if (childToken.Type == JTokenType.Array && (parentToken as JProperty).Name.Trim() != "#") { List <object> itemsToAdd = new List <object>(); JToken[] childTokens = childToken.Children().ToArray(); int childTokensLength = childTokens.Length; for (int j = 0; j < childTokensLength; j++) { JToken arrEl = childTokens[j]; object itemToAdd = arrEl.Value <JToken>(); if (arrEl.Type == JTokenType.String && arrEl.ToString().Trim().StartsWith("#")) { object value = ParseFunction(arrEl.ToString(), inputJson, parentArray, currentArrayToken, localContext); itemToAdd = value; } itemsToAdd.Add(itemToAdd); } JArray arrayToken = new JArray(itemsToAdd); } if (childToken.Type == JTokenType.Property) { JProperty property = childToken as JProperty; if (property.Name != null && property.Name == "#" && property.Value.Type == JTokenType.Array) { JArray values = property.Value as JArray; JToken[] arrayValues = values.Children().ToArray(); int arrayValuesLength = arrayValues.Length; for (int j = 0; j < arrayValuesLength; j++) { JToken arrayValue = arrayValues[j]; if (arrayValue.Type == JTokenType.String && ExpressionHelper.TryParseFunctionNameAndArguments( arrayValue.Value <string>().Trim(), out string functionName, out string arguments)) { if (functionName == "copy") { if (selectedTokens == null) { selectedTokens = new List <JToken>(); } selectedTokens.Add(Copy(arguments, inputJson, localContext)); } else if (functionName == "replace") { if (tokensToReplace == null) { tokensToReplace = new Dictionary <string, JToken>(); } var replaceResult = Replace(arguments, inputJson, localContext); tokensToReplace.Add(replaceResult.Key, replaceResult.Value); } else if (functionName == "delete") { if (tokensToDelete == null) { tokensToDelete = new List <JToken>(); } tokensToDelete.Add(Delete(arguments, inputJson, localContext)); } } } } if (property.Name != null && property.Value.ToString().Trim().StartsWith("#") && !property.Name.Contains("#eval") && !property.Name.Contains("#ifgroup") && !property.Name.Contains("#loop")) { object newValue = ParseFunction(property.Value.ToString(), inputJson, parentArray, currentArrayToken, localContext); property.Value = GetToken(newValue, localContext); } /* For looping*/ isLoop = false; if (property.Name != null && property.Name.Contains("#eval")) { ExpressionHelper.TryParseFunctionNameAndArguments(property.Name, out string functionName, out string functionString); object functionResult = ParseFunction(functionString, inputJson, null, null, localContext); JProperty clonedProperty = new JProperty(functionResult.ToString(), property.Value); if (loopProperties == null) { loopProperties = new List <string>(); } loopProperties.Add(property.Name); if (tokensToAdd == null) { tokensToAdd = new List <JToken> { clonedProperty }; } } if (property.Name != null && property.Name.Contains("#ifgroup")) { ExpressionHelper.TryParseFunctionNameAndArguments(property.Name, out string functionName, out string functionString); object functionResult = ParseFunction(functionString, inputJson, null, null, localContext); bool result = false; try { result = (bool)ReflectionHelper.GetTypedValue(typeof(bool), functionResult, GetEvaluationMode(localContext)); } catch { if (IsStrictMode(localContext)) { throw; } result = false; } if (result == true) { if (loopProperties == null) { loopProperties = new List <string>(); } loopProperties.Add(property.Name); RecursiveEvaluate(childToken, inputJson, parentArray, currentArrayToken, localContext); if (tokenToForm == null) { tokenToForm = new List <JToken>(); } var childTokens = childToken.Children().ToArray(); int childTokensLength = childTokens.Length; for (int j = 0; j < childTokensLength; j++) { JToken grandChildToken = childTokens[j]; tokenToForm.Add(grandChildToken.DeepClone()); } } else { if (loopProperties == null) { loopProperties = new List <string>(); } loopProperties.Add(property.Name); } isLoop = true; } if (property.Name != null && property.Name.Contains("#loop")) { string strArrayToken = property.Name.Substring(6, property.Name.Length - 7); var jsonToLoad = inputJson; if (currentArrayToken != null && property.Name.Contains("#loopwithincontext")) { strArrayToken = property.Name.Substring(19, property.Name.Length - 20); jsonToLoad = JsonConvert.SerializeObject(currentArrayToken); } JToken token = JsonConvert.DeserializeObject <JObject>(jsonToLoad); JToken arrayToken = null; if (strArrayToken.Contains("#")) { int sIndex = strArrayToken.IndexOf("#"); string sub1 = strArrayToken.Substring(0, sIndex); int indexOfENdFubction = GetIndexOfFunctionEnd(strArrayToken); if (indexOfENdFubction > sIndex && sIndex > 0) { string sub2 = strArrayToken.Substring(indexOfENdFubction + 1, strArrayToken.Length - indexOfENdFubction - 1); string functionResult = ParseFunction(strArrayToken.Substring(sIndex, indexOfENdFubction - sIndex + 1), inputJson, parentArray, currentArrayToken, localContext).ToString(); strArrayToken = sub1 + functionResult + sub2; } } try { arrayToken = token.SelectToken(strArrayToken); if (arrayToken is JObject) { arrayToken = new JArray(arrayToken); } } catch { var multipleTokens = token.SelectTokens(strArrayToken); arrayToken = new JArray(multipleTokens); } if (arrayToken == null) { arrayToForm = new JArray(); } else { JArray array = (JArray)arrayToken; JToken[] elements = array.ToArray(); int elementsLength = elements.Length; for (int j = 0; j < elementsLength; j++) { if (arrayToForm == null) { arrayToForm = new JArray(); } JToken clonedToken = childToken.DeepClone(); RecursiveEvaluate(clonedToken, inputJson, array, elements[j], localContext); var clonedTokens = clonedToken.Children().ToArray(); int clonedTokensLength = clonedTokens.Length; for (int k = 0; k < clonedTokensLength; k++) { JToken replacedProperty = clonedTokens[k]; arrayToForm.Add(replacedProperty); } } } if (loopProperties == null) { loopProperties = new List <string>(); } loopProperties.Add(property.Name); isLoop = true; } /*End looping */ } if (childToken.Type == JTokenType.String && childToken.Value <string>().Trim().StartsWith("#") && parentArray != null && currentArrayToken != null) { object newValue = ParseFunction(childToken.Value <string>(), inputJson, parentArray, currentArrayToken, localContext); JToken replaceToken = GetToken(newValue, localContext); childToken.Replace(replaceToken); } if (!isLoop) { RecursiveEvaluate(childToken, inputJson, parentArray, currentArrayToken, localContext); } } if (selectedTokens != null) { for (int i = 0; i < selectedTokens.Count; i++) { JToken selectedToken = selectedTokens[i]; if (selectedToken != null) { JToken[] copyChildren = selectedToken.Children().ToArray(); int copyChildrenLength = copyChildren.Length; for (int j = 0; j < copyChildrenLength; j++) { JToken copyChild = copyChildren[j]; JProperty property = copyChild as JProperty; (parentToken as JObject).Add(property.Name, property.Value); } } } } if (tokensToReplace != null) { foreach (KeyValuePair <string, JToken> tokenToReplace in tokensToReplace) { JToken selectedToken = (parentToken as JObject).SelectToken(tokenToReplace.Key); if (selectedToken != null && selectedToken is JObject) { JObject selectedObject = selectedToken as JObject; selectedObject.RemoveAll(); JToken[] copyChildren = tokenToReplace.Value.Children().ToArray(); int copyChildrenLength = copyChildren.Length; for (int i = 0; i < copyChildrenLength; i++) { JToken copyChild = copyChildren[i]; JProperty property = copyChild as JProperty; selectedObject.Add(property.Name, property.Value); } } if (selectedToken != null && selectedToken is JValue) { JValue selectedObject = selectedToken as JValue; selectedObject.Value = tokenToReplace.Value.ToString(); } } } if (tokensToDelete != null) { for (int i = 0; i < tokensToDelete.Count; i++) { string selectedToken = tokensToDelete[i].Path; JToken tokenToRemove = parentToken.SelectToken(selectedToken); if (tokenToRemove != null) { tokenToRemove.Ancestors().First().Remove(); } } } if (tokensToAdd != null) { for (int i = 0; i < tokensToAdd.Count; i++) { JToken token = tokensToAdd[i]; (parentToken as JObject).Add((token as JProperty).Name, (token as JProperty).Value); } } if (tokenToForm != null) { for (int i = 0; i < tokenToForm.Count; i++) { JToken token = tokenToForm[i]; JToken[] childTokens = token.Children().ToArray(); int childTokensLength = childTokens.Length; for (int j = 0; j < childTokensLength; j++) { JProperty childToken = (JProperty)childTokens[j]; (parentToken as JObject).Add(childToken.Name, childToken.Value); } } } if (parentToken is JObject) { (parentToken as JObject).Remove("#"); } if (loopProperties != null) { for (int i = 0; i < loopProperties.Count; i++) { string propertyToDelete = loopProperties[i]; (parentToken as JObject).Remove(propertyToDelete); } } if (arrayToForm != null) { parentToken.Replace(arrayToForm); } }
private static bool IsFallbackToDefault(JUSTContext localContext) { return(GetEvaluationMode(localContext) == EvaluationMode.FallbackToDefault); }
private static bool IsStrictMode(JUSTContext localContext) { return(GetEvaluationMode(localContext) == EvaluationMode.Strict); }
private static EvaluationMode GetEvaluationMode(JUSTContext localContext) { return(localContext?.EvaluationMode ?? GlobalContext.EvaluationMode); }
private static JToken GetInputToken(JUSTContext localContext) { return(localContext?.Input ?? GlobalContext.Input); }
public static JObject Transform(JObject transformer, JObject input, JUSTContext localContext = null) { string inputJson = JsonConvert.SerializeObject(input); return(Transform(transformer, inputJson, localContext)); }
private static object ParseArgument(string inputJson, JArray array, JToken currentArrayElement, string argument, JUSTContext localContext) { string trimmedArgument = argument; if (argument.Contains("#")) { trimmedArgument = argument.Trim(); } if (trimmedArgument.StartsWith("#")) { return(ParseFunction(trimmedArgument, inputJson, array, currentArrayElement, localContext)); } else { return(trimmedArgument); } }
private static object ParseFunction(string functionString, string inputJson, JArray array, JToken currentArrayElement, JUSTContext localContext) { try { object output = null; string functionName, argumentString; if (!ExpressionHelper.TryParseFunctionNameAndArguments(functionString, out functionName, out argumentString)) { return(functionName); } string[] arguments = ExpressionHelper.GetArguments(argumentString); var listParameters = new List <object>(); if (functionName == "ifcondition") { var condition = ParseArgument(inputJson, array, currentArrayElement, arguments[0], localContext); var value = ParseArgument(inputJson, array, currentArrayElement, arguments[1], localContext); var index = condition.ToString().ToLower() == value.ToString().ToLower() ? 2 : 3; output = ParseArgument(inputJson, array, currentArrayElement, arguments[index], localContext); } else { int i = 0; for (; i < (arguments?.Length ?? 0); i++) { listParameters.Add(ParseArgument(inputJson, array, currentArrayElement, arguments[i], localContext)); } listParameters.Add(localContext ?? GlobalContext); var parameters = listParameters.ToArray(); if (functionName == "currentvalue" || functionName == "currentindex" || functionName == "lastindex" || functionName == "lastvalue") { output = ReflectionHelper.caller(null, "JUST.Transformer", functionName, new object[] { array, currentArrayElement }, true, localContext ?? GlobalContext); } else if (functionName == "currentvalueatpath" || functionName == "lastvalueatpath") { output = ReflectionHelper.caller(null, "JUST.Transformer", functionName, new object[] { array, currentArrayElement, arguments[0] }, true, localContext ?? GlobalContext); } else if (functionName == "customfunction") { output = CallCustomFunction(parameters, localContext); } else if (localContext?.IsRegisteredCustomFunction(functionName) ?? false) { var methodInfo = localContext.GetCustomMethod(functionName); output = ReflectionHelper.InvokeCustomMethod(methodInfo, parameters, true, localContext ?? GlobalContext); } else if (GlobalContext.IsRegisteredCustomFunction(functionName)) { var methodInfo = GlobalContext.GetCustomMethod(functionName); output = ReflectionHelper.InvokeCustomMethod(methodInfo, parameters, true, localContext ?? GlobalContext); } else if (Regex.IsMatch(functionName, ReflectionHelper.EXTERNAL_ASSEMBLY_REGEX)) { output = ReflectionHelper.CallExternalAssembly(functionName, parameters, localContext ?? GlobalContext); } else if (functionName == "xconcat" || functionName == "xadd" || functionName == "mathequals" || functionName == "mathgreaterthan" || functionName == "mathlessthan" || functionName == "mathgreaterthanorequalto" || functionName == "mathlessthanorequalto" || functionName == "stringcontains" || functionName == "stringequals") { object[] oParams = new object[1]; oParams[0] = parameters; output = ReflectionHelper.caller(null, "JUST.Transformer", functionName, oParams, true, localContext ?? GlobalContext); } else { var input = ((JUSTContext)parameters.Last()).Input; if (currentArrayElement != null && functionName != "valueof") { ((JUSTContext)parameters.Last()).Input = JsonConvert.SerializeObject(currentArrayElement); } output = ReflectionHelper.caller(null, "JUST.Transformer", functionName, parameters, true, localContext ?? GlobalContext); ((JUSTContext)parameters.Last()).Input = input; } } return(output); } catch (Exception ex) { throw new Exception("Error while calling function : " + functionString + " - " + ex.Message, ex); } }