public bool TransformJson(TransformJsonArgs args)
        {
            if (!(args.Source is JObject jSource))
            {
                return(false);
            }
            var jmerge = jSource["$merge"];

            if (jmerge == null)
            {
                return(false);
            }

            JObject mergeObj = null;

            if (jmerge.Type == JTokenType.String)
            {
                var mergePath = jmerge.Value <string>();
                mergeObj = args.Source.Root.SelectToken(mergePath) as JObject;
            }
            else
            {
                mergeObj = jmerge as JObject;
            }

            if (mergeObj == null)
            {
                throw new InvalidOperationException("$merge jpath must point to a JObject");
            }

            jSource.Remove("$merge");
            jSource.Merge(mergeObj, new JsonMergeSettings {
            });
            return(true);
        }
Esempio n. 2
0
        /// <summary>
        /// Transforms specified jtoken using registered transformers recursively.
        /// Current implementation actually MUTATES source object, so keep it in mind
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public JToken Transform(JToken source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            var transformers = (Transformers ?? Array.Empty <ICanTransformJson>());
            var root         = source.Root;


            void TransformDfs(TransformJsonArgs args)
            {
                //first we go deeper and then try to tranform current source, if something was transformed, we go deep again, since children might have changed
                do
                {
                    if (args.Source == null || args.Source.Root != root)
                    {
                        break;
                    }
                    if (args.Source is JObject jobj)
                    {
                        foreach (var jprop in jobj.Properties())
                        {
                            var childArgs = new TransformJsonArgs(args)
                            {
                                Source = jprop.Value
                            };
                            TransformDfs(childArgs);
                        }
                    }
                } while (transformers.Select(t => {
                    if (args.Source == null || args.Source.Root != root)
                    {
                        return(t, transformed: false);
                    }
                    else
                    {
                        return(t, transformed: t.TransformJson(args));
                    }
                }).ToArray().Any(t => t.transformed));
            }

            var rootArgs = new TransformJsonArgs {
                Source = source
            };

            TransformDfs(rootArgs);
            return(rootArgs.Source);
        }
Esempio n. 3
0
        bool UseParameter(TransformJsonArgs args, JToken jUseParameter)
        {
            var parName = jUseParameter.Value <string>();

            if (args.Context.TryGetValue("paramByName", out object paramByNameObj) && paramByNameObj is Dictionary <string, JToken> paramByName)
            {
                if (paramByName.TryGetValue(parName, out var parValue))
                {
                    var newNode = parValue.DeepClone();
                    args.Source.Replace(newNode);
                    args.Source = newNode;
                    return(true);
                }
            }
            return(false);
        }
Esempio n. 4
0
        public bool TransformJson(TransformJsonArgs args)
        {
            if (!(args.Source is JObject jSource))
            {
                return(false);
            }
            if (jSource["$setParameters"] is JObject jSetParameters)
            {
                return(SetParameters(args, jSetParameters));
            }
            else if (jSource["$useParameter"] is JToken jUseParameter)
            {
                return(UseParameter(args, jUseParameter));
            }

            return(false);
        }
Esempio n. 5
0
        bool SetParameters(TransformJsonArgs args, JObject jSetParameters)
        {
            if (!(args.Context.TryGetValue("paramByName", out object paramByNameObj) && paramByNameObj is Dictionary <string, JToken> paramByName))
            {
                paramByName = new Dictionary <string, JToken>();
                args.Context["paramByName"] = paramByName;
            }

            foreach (var jProp in jSetParameters.Properties())
            {
                paramByName[jProp.Name] = jProp.Value;
            }

            (args.Source as JObject).Remove("$setParameters");

            //args.Source.Remove();
            //args.Source = null;

            return(false);
        }
        public bool TransformJson(TransformJsonArgs args)
        {
            if (!(args.Source is JObject jobj))
            {
                return(false);
            }
            var filePath = jobj["$file"]?.Value <string>();

            if (filePath == null)
            {
                return(false);
            }
            jobj.Remove("$file");
            using (var fr = File.OpenText(filePath))
                using (var jtr = new JsonTextReader(fr))
                {
                    var jfilecontent = JObject.ReadFrom(jtr);
                    jobj.Merge(jfilecontent);
                }
            return(true);
        }
Esempio n. 7
0
 /// <summary>
 /// Creates instance, allows to specify parent args
 /// </summary>
 /// <param name="parent"></param>
 public TransformJsonArgs(TransformJsonArgs parent = null)
 {
     this.Parent  = parent;
     this.Context = parent?.Context ?? new Dictionary <object, object>();
 }