Пример #1
0
 public static bool TryExecuteStringMethod(this JsonElement e, string methodName, List <JsonElement> arguments, out JsonElement result)
 {
     try
     {
         result = e.ExecuteStringMethod(methodName, arguments);
         return(true);
     }
     catch
     {
         result = JsonElementFactory.CreateNull();
         return(false);
     }
 }
Пример #2
0
        private static bool ExecuteStringMethodToUpperOrToLowerIfMatched(JsonElement e, string methodName, List <JsonElement> arguments, out JsonElement result)
        {
            bool isUpper = methodName == "toUpper" || methodName == "toUpperCase";
            bool isLower = methodName == "toLower" || methodName == "toLowerCase";

            if (isUpper || isLower)
            {
                if (arguments.Count > 0)
                {
                    throw new JsonPatwayMethodNotSupportedException($"Method string.{methodName} expected 0 arguments but got: {arguments.Count}");
                }

                string ret = e.GetString();
                ret    = isUpper ? ret.ToUpper() : ret.ToLower();
                result = JsonElementFactory.CreateString(ret);
                return(true);
            }

            result = JsonElementFactory.CreateNull();
            return(false);
        }
Пример #3
0
        private static bool ExecuteStringStartsWithEndsWithOrContainsIfMatched(JsonElement e, string methodName, List <JsonElement> arguments, out JsonElement result)
        {
            if (methodName == "startsWith" || methodName == "endsWith" || methodName == "contains")
            {
                EnsureStartsWithEndsWithOrContainsArgumentsAreValid(methodName, arguments);

                string executedOn = e.GetString();
                string argument   = arguments[0].GetString();

                if (arguments.Count == 2 && arguments[1].IsTruthy())
                {
                    executedOn = executedOn.ToLower();
                    argument   = argument.ToLower();
                }

                bool resultBool;
                if (methodName == "startsWith")
                {
                    resultBool = executedOn.StartsWith(argument);
                }
                else if (methodName == "endsWith")
                {
                    resultBool = executedOn.EndsWith(argument);
                }
                else
                {
                    resultBool = executedOn.Contains(argument);
                }

                result = JsonElementFactory.CreateBool(resultBool);
                return(true);
            }

            result = JsonElementFactory.CreateNull();
            return(false);
        }