Exemplo n.º 1
0
        /// <inheritdoc/>
        internal override void Process(JObject source, JObject transform, JsonTransformationContextLogger logger)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (transform == null)
            {
                throw new ArgumentNullException(nameof(transform));
            }

            foreach (JProperty transformNode in transform.Properties()
                     .Where(p => JdtUtilities.IsJdtSyntax(p.Name)))
            {
                string verb = JdtUtilities.GetJdtSyntax(transformNode.Name);
                if (verb != null)
                {
                    if (!this.ValidVerbs.Contains(verb))
                    {
                        throw JdtException.FromLineInfo(string.Format(Resources.ErrorMessage_InvalidVerb, verb), ErrorLocation.Transform, transformNode);
                    }
                }
            }

            this.Successor.Process(source, transform, logger);
        }
        /// <summary>
        /// Gets all the properties within the object that correspond to JDT syntax
        /// </summary>
        /// <param name="objectToSearch">The object to search</param>
        /// <returns>An enumerable of properties that start with the JDT prefix</returns>
        internal static IEnumerable <JProperty> GetJdtProperties(this JObject objectToSearch)
        {
            if (objectToSearch == null)
            {
                throw new ArgumentNullException(nameof(objectToSearch));
            }

            return(objectToSearch.Properties().Where(p => JdtUtilities.IsJdtSyntax(p.Name)));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Clones a <see cref="JObject"/> preserving the line information
        /// </summary>
        /// <param name="objectToClone">The object to clone</param>
        /// <returns>A clone of the object with its line info</returns>
        internal static JObject CloneWithLineInfo(this JObject objectToClone)
        {
            var loadSettings = new JsonLoadSettings()
            {
                LineInfoHandling = JdtUtilities.GetLineInfoHandling()
            };

            using (var objectReader = objectToClone.CreateReader())
            {
                return(JObject.Load(objectReader, loadSettings));
            }
        }
        private void SetTransform(Stream transformStream)
        {
            this.loadSettings = new JsonLoadSettings()
            {
                CommentHandling  = CommentHandling.Ignore,
                LineInfoHandling = JdtUtilities.GetLineInfoHandling()
            };

            using (StreamReader transformStreamReader = new StreamReader(transformStream))
                using (JsonTextReader transformReader = new JsonTextReader(transformStreamReader))
                {
                    this.transformObject = JObject.Load(transformReader, this.loadSettings);
                }
        }
        /// <inheritdoc/>
        internal override void Process(JObject source, JObject transform, JsonTransformationContextLogger logger)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (transform == null)
            {
                throw new ArgumentNullException(nameof(transform));
            }

            // JDT Verbs are not handled here
            foreach (JProperty transformNode in transform.Properties()
                     .Where(p => !JdtUtilities.IsJdtSyntax(p.Name)))
            {
                JToken nodeToTransform;
                if (source.TryGetValue(transformNode.Name, out nodeToTransform))
                {
                    // If the node is present in both transform and source, analyze the types
                    // If both are objects, that is a recursive transformation, not handled here
                    if (nodeToTransform.Type == JTokenType.Array && transformNode.Value.Type == JTokenType.Array)
                    {
                        // If the original and transform are arrays, merge the contents together
                        ((JArray)nodeToTransform).Merge(transformNode.Value.DeepClone());
                    }
                    else if (nodeToTransform.Type != JTokenType.Object || transformNode.Value.Type != JTokenType.Object)
                    {
                        // TO DO: Verify if object has JDT verbs. They shouldn't be allowed here because they won't be processed
                        // If the contents are different, execute the replace
                        source[transformNode.Name] = transformNode.Value.DeepClone();
                    }
                }
                else
                {
                    // If the node is not present in the original, add it
                    source.Add(transformNode.DeepClone());
                }
            }

            this.Successor.Process(source, transform, logger);
        }
        /// <inheritdoc/>
        internal override void Process(JObject source, JObject transform, JsonTransformationContextLogger logger)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (transform == null)
            {
                throw new ArgumentNullException(nameof(transform));
            }

            // Nodes that should be removed from the transform after they are handled
            var nodesToRemove = new List <string>();

            foreach (JProperty transformNode in transform.Properties()
                     .Where(p => p.Value.Type == JTokenType.Object && !JdtUtilities.IsJdtSyntax(p.Name)))
            {
                // We recurse into objects that do not correspond to JDT verbs and that exist in both source and transform
                JToken sourceChild;
                if (source.TryGetValue(transformNode.Name, out sourceChild) && sourceChild.Type == JTokenType.Object)
                {
                    ProcessTransform((JObject)sourceChild, (JObject)transformNode.Value, logger);

                    // If we have already recursed into that node, it should be removed from the transform
                    nodesToRemove.Add(transformNode.Name);
                }
            }

            // Remove all of the previously handled nodes
            // This is necessary so that a rename does not cause a node to be hadled twice
            nodesToRemove.ForEach(node => transform.Remove(node));

            // Continue to next transformation
            this.Successor.Process(source, transform, logger);
        }