示例#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 JsonElement?GetPropertyValue(JsonElement element, string propertyName)
        {
            if (propertyName == "length")
            {
                if (element.ValueKind == JsonValueKind.String)
                {
                    return(JsonElementFactory.CreateNumber(element.GetString().Length));
                }
                if (element.ValueKind == JsonValueKind.Array)
                {
                    return(JsonElementFactory.CreateNumber(element.GetArrayLength()));
                }
            }

            if (element.ValueKind == JsonValueKind.Object && element.TryGetProperty(propertyName, out JsonElement result))
            {
                return(result);
            }

            return(null);
        }
示例#3
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));
        }
示例#4
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);
        }
示例#5
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);
        }