Ejemplo n.º 1
0
        public static JsonElement ExecuteArrayMethod(this JsonElement e, string methodName, List <JsonElement> arguments)
        {
            if (e.ValueKind != JsonValueKind.Array)
            {
                throw new ArgumentException($"ExecuteArrayMethod expected JsonElement of JsonValueKind.Array but got JsonValueKind.{e.ValueKind}");
            }

            if (methodName != "contains")
            {
                throw new JsonPatwayMethodNotSupportedException($"Method {methodName} does not exist on array");
            }

            if (arguments.Count != 1)
            {
                throw new JsonPatwayMethodNotSupportedException("Method contains on array accepts one and only one argument");
            }

            bool result = e.EnumerateArray().Contains(arguments[0], JsonElementEqualityComparer.Default);

            return(JsonElementFactory.CreateBool(result));
        }
Ejemplo n.º 2
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);
        }