Exemplo n.º 1
0
        /// <summary>
        /// Attempts to create a filter from a string.
        /// </summary>
        /// <param name="value">The string.</param>
        /// <param name="rootPath">The root path.</param>
        /// <returns>The corresponding filter, or null.</returns>
        /// <remarks>This overload automatically prefixes each JSON path
        /// with the specified root path, if specified.</remarks>
        public static JsonFilter TryParse(string value, string rootPath)
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                return(Empty);
            }

            var paths = new List <PropertyPath>();

            if (!string.IsNullOrEmpty(rootPath))
            {
                if (rootPath.IndexOfAny(s_pathSeparators) != -1)
                {
                    return(null);
                }

                PropertyPath parsedRootPath = PropertyPath.TryParse(rootPath, null);
                if (parsedRootPath == null || parsedRootPath.IsExcluded)
                {
                    return(null);
                }

                string nextPrefix = "";
                foreach (var rootPathPart in parsedRootPath.Parts)
                {
                    paths.Add(PropertyPath.TryParse(nextPrefix + AnyProperty, null));
                    nextPrefix += rootPathPart + PropertySeparator;
                }
            }

            paths.AddRange(SplitFullPaths(value)
                           .Select(x => PropertyPath.TryParse(x, rootPath)));

            if (paths.Any(x => x == null))
            {
                return(null);
            }

            FilterNode rootNode = new FilterNode();

            foreach (PropertyPath path in paths.WhereNotNull())
            {
                rootNode.AddPath(path);
            }
            return(new JsonFilter(rootNode));
        }
Exemplo n.º 2
0
            public void AddPath(PropertyPath path)
            {
                FilterNode childNode = m_children.GetOrAddValue(path.Parts[0], () => new FilterNode());

                if (path.Parts.Count == 1)
                {
                    bool isIncluded = !path.IsExcluded;
                    if (childNode.m_isIncluded == null)
                    {
                        childNode.m_isIncluded = isIncluded;
                    }
                    else if (childNode.m_isIncluded != isIncluded)
                    {
                        childNode.m_isIncluded = null;
                    }
                }
                else
                {
                    childNode.AddPath(new PropertyPath(path.Parts.Skip(1), path.IsExcluded));
                }
            }