/// <summary>
        /// Scrubs the json fields by their full names/paths.
        /// </summary>
        /// <param name="jsonData">The json data.</param>
        /// <param name="scrubFieldsPaths">The scrub fields paths.</param>
        /// <param name="scrubMask">The scrub mask.</param>
        /// <returns>System.String.</returns>
        public static string ScrubJsonFieldsByPaths(string jsonData, IEnumerable <string> scrubFieldsPaths, string scrubMask)
        {
            var fieldsPaths = scrubFieldsPaths as string[] ?? scrubFieldsPaths.ToArray();

            if (fieldsPaths.LongLength == 0)
            {
                return(jsonData);
            }

            JObject json = JObject.Parse(jsonData);

            foreach (var path in fieldsPaths)
            {
                JsonScrubber.ScrubJsonPath(json, path, scrubMask);
            }

            return(json.ToString());
        }
Example #2
0
        /// <summary>
        /// Scrubs the json path.
        /// </summary>
        /// <param name="jsonData">The json data.</param>
        /// <param name="scrubPath">The scrub path.</param>
        /// <param name="scrubMask">The scrub mask.</param>
        public static void ScrubJsonPath(JObject jsonData, string scrubPath, string scrubMask)
        {
            if (jsonData == null)
            {
                return;
            }

            JToken?jToken = JsonScrubber.FindJsonTokenSafelyUsingPath(jsonData, scrubPath);

            if (jToken != null)
            {
                var jProperty = jToken.Parent as JProperty;
                if (jProperty != null)
                {
                    jProperty.Replace(new JProperty(jProperty.Name, scrubMask));
                    return;
                }
            }

            //to deal with the possible dotted data element name we need to perform some acrobatics here:
            const int startingIndex = 0;
            int       indexLimit    = scrubPath.LastIndexOf('.');
            int       dotIndex      = scrubPath.IndexOf('.', startingIndex);

            while (dotIndex > 0 && dotIndex < indexLimit)
            {
                string dottedFieldPath = scrubPath.Substring(0, dotIndex);
                string dottedFieldName = scrubPath.Substring(dotIndex + 1);
                jToken = JsonScrubber.FindJsonTokenSafelyUsingPath(jsonData, dottedFieldPath);
                jToken = jToken?[dottedFieldName];
                if (jToken != null)
                {
                    //we found the dotted data element name, let's mask its value and return:
                    var jProperty = jToken.Parent as JProperty;
                    if (jProperty != null)
                    {
                        jProperty.Replace(new JProperty(jProperty.Name, scrubMask));
                        return;
                    }
                }
                dotIndex = scrubPath.IndexOf('.', dotIndex + 1);
            }
        }
Example #3
0
        /// <summary>
        /// Scrubs the json fields by their full names/paths.
        /// </summary>
        /// <param name="jsonData">The json data.</param>
        /// <param name="scrubFieldsPaths">The scrub fields paths.</param>
        /// <param name="scrubMask">The scrub mask.</param>
        public static void ScrubJsonFieldsByPaths(JProperty jsonData, IEnumerable <string> scrubFieldsPaths, string scrubMask)
        {
            if (jsonData == null)
            {
                return;
            }

            var fieldsPaths = scrubFieldsPaths as string[] ?? scrubFieldsPaths.ToArray();

            if (fieldsPaths.LongLength == 0)
            {
                return;
            }

            foreach (var path in fieldsPaths)
            {
                JsonScrubber.ScrubJsonPath(jsonData, path, scrubMask);
            }
        }