public static bool mathlessthanorequalto(object[] list) { bool result = false; if (list.Length >= 2) { decimal lshDecimal = (decimal)ReflectionHelper.GetTypedValue(typeof(decimal), list[0], EvaluationMode.Strict); decimal rhsDecimal = (decimal)ReflectionHelper.GetTypedValue(typeof(decimal), list[1], EvaluationMode.Strict); result = lshDecimal <= rhsDecimal; } 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)); }
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 void RecursiveEvaluate(JToken parentToken, string inputJson, JArray parentArray, JToken currentArrayToken, JUSTContext localContext) { if (parentToken == null) { return; } JEnumerable <JToken> tokens = parentToken.Children(); List <JToken> selectedTokens = null; Dictionary <string, JToken> tokensToReplace = null; List <JToken> tokensToDelete = null; List <string> loopProperties = null; JArray arrayToForm = null; JObject dictToForm = null; List <JToken> tokenToForm = null; List <JToken> tokensToAdd = null; bool isLoop = false; foreach (JToken childToken in tokens) { if (childToken.Type == JTokenType.Array && (parentToken as JProperty)?.Name.Trim() != "#") { JArray arrayToken = childToken as JArray; List <object> itemsToAdd = new List <object>(); foreach (JToken arrEl in childToken.Children()) { 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); } arrayToken.RemoveAll(); foreach (object itemToAdd in itemsToAdd) { if (itemToAdd is Array) { foreach (var item in itemToAdd as Array) { arrayToken.Add(Utilities.GetNestedData(item)); } } else { arrayToken.Add(JToken.FromObject(itemToAdd)); } } } 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; JEnumerable <JToken> arrayValues = values.Children(); foreach (JToken arrayValue in arrayValues) { 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, parentArray, currentArrayToken, localContext); JProperty clonedProperty = new JProperty(functionResult.ToString(), ParseFunction(property.Value.ToString(), inputJson, parentArray, currentArrayToken, localContext)); 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, parentArray, currentArrayToken, 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>(); } foreach (JToken grandChildToken in childToken.Children()) { 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")) { ExpressionHelper.TryParseFunctionNameAndArguments(property.Name, out string functionName, out string arguments); var token = currentArrayToken != null && functionName == "loopwithincontext" ? currentArrayToken : GetInputToken(localContext); var strArrayToken = ParseArgument(inputJson, parentArray, currentArrayToken, arguments, localContext) as string; bool isDictionary = false; JToken arrayToken; try { arrayToken = token.SelectToken(strArrayToken); if (arrayToken is IDictionary <string, JToken> dict) //JObject is a dictionary { isDictionary = true; JArray arr = new JArray(); foreach (var item in dict) { arr.Add(new JObject { { item.Key, item.Value } }); } arrayToken = arr; } } catch { var multipleTokens = token.SelectTokens(strArrayToken); arrayToken = new JArray(multipleTokens); } JArray array = arrayToken as JArray; if (array != null) { using (IEnumerator <JToken> elements = array.GetEnumerator()) { arrayToForm = new JArray(); if (!isDictionary) { while (elements.MoveNext()) { JToken clonedToken = childToken.DeepClone(); RecursiveEvaluate(clonedToken, inputJson, array, elements.Current, localContext); foreach (JToken replacedProperty in clonedToken.Children()) { arrayToForm.Add(replacedProperty); } } } else { dictToForm = new JObject(); while (elements.MoveNext()) { JToken clonedToken = childToken.DeepClone(); RecursiveEvaluate(clonedToken, inputJson, array, elements.Current, localContext); foreach (JToken replacedProperty in clonedToken.Children().Select(t => t.First)) { dictToForm.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) { foreach (JToken selectedToken in selectedTokens) { if (selectedToken != null) { JEnumerable <JToken> copyChildren = selectedToken.Children(); foreach (JToken copyChild in copyChildren) { 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(); JEnumerable <JToken> copyChildren = tokenToReplace.Value.Children(); foreach (JToken copyChild in copyChildren) { 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) { foreach (string selectedToken in tokensToDelete) { JToken tokenToRemove = parentToken.SelectToken(selectedToken); if (tokenToRemove != null) { tokenToRemove.Ancestors().First().Remove(); } } } if (tokensToAdd != null) { foreach (JToken token in tokensToAdd) { (parentToken as JObject).Add((token as JProperty).Name, (token as JProperty).Value); } } if (tokenToForm != null) { foreach (JToken token in tokenToForm) { foreach (JProperty childToken in token.Children()) { (parentToken as JObject).Add(childToken.Name, childToken.Value); } } } if (parentToken is JObject jObject) { jObject.Remove("#"); } if (loopProperties != null) { foreach (string propertyToDelete in loopProperties) { if (dictToForm == null && arrayToForm == null && parentToken.Count() <= 1) { parentToken.Replace(JValue.CreateNull()); } else { (parentToken as JObject).Remove(propertyToDelete); } } } if (dictToForm != null) { parentToken.Replace(dictToForm); } else if (arrayToForm != null) { if (parentToken.Parent != null && parentToken.Parent is JArray arr) { foreach (var item in arrayToForm) { arr.Add(item); } if (!parentToken.HasValues) { var tmp = parentToken; parentToken = arr; tmp.Remove(); } } else if (parentToken.Parent != null) { parentToken.Replace(arrayToForm); } } }
public static decimal todecimal(object val, JUSTContext context) { return(decimal.Round((decimal)ReflectionHelper.GetTypedValue(typeof(decimal), val, context.EvaluationMode), context.DefaultDecimalPlaces)); }
public static object toboolean(object val, JUSTContext context) { return(ReflectionHelper.GetTypedValue(typeof(bool), val, context.EvaluationMode)); }