Esempio n. 1
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var job = new Job();

            var jObject = JObject.Load(reader);

            foreach (var property in jObject.Properties())
            {
                if (property.Name == "name")
                {
                    job.Name = property.Value?.ToString();
                    continue;
                }

                if (property.Name == "serial")
                {
                    job.Serial = Convert.ToBoolean(property.Value?.ToString());
                    continue;
                }

                if (property.Name == "serial_groups")
                {
                    job.SerialGroups = JsonConvert.DeserializeObject <List <string> >(property.Value?.ToString());
                    continue;
                }

                if (property.Name == "plan")
                {
                    job.Plan = JsonConvert.DeserializeObject <List <JobPlan> >(property.Value.ToString(), Converter.Settings);
                    continue;
                }

                if (property.Name.StartsWith("_merge"))
                {
                    var mergeCall = new MergeCall();
                    mergeCall.Name   = property.Name;
                    mergeCall.Method = property.Value?.ToString();
                    job.MergeCall    = mergeCall;
                }
            }

            return(job);
        }
Esempio n. 2
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            JobPlan jobPlan = null;

            if (objectType == typeof(JobPlan))
            {
                jobPlan = new JobPlan();
            }
            else if (objectType == typeof(JobPlanEnsure))
            {
                jobPlan = new JobPlanEnsure();
            }

            if (reader.Value?.ToString()?.StartsWith("_anchor_call") != null)
            {
                var jobPlanAnchorCall = new JobPlan();
                jobPlanAnchorCall.AnchorCall        = new AnchorCall();
                jobPlanAnchorCall.AnchorCall.Method = reader.Value.ToString();
                return(jobPlanAnchorCall);
            }

            var jObject = JObject.Load(reader);

            foreach (var property in jObject.Properties())
            {
                if (property.Name.StartsWith("_anchor_list_decl"))
                {
                    jobPlan.TaskAnchorDeclaration        = new AnchorDeclaration();
                    jobPlan.TaskAnchorDeclaration.Method = property.Name;
                }

                if (property.Name == "in_parallel")
                {
                    var stepsFound = false;
                    var pObject    = JsonConvert.DeserializeObject(property.Value.ToString());
                    if (pObject is JObject pjObj)
                    {
                        foreach (var pjProp in pjObj)
                        {
                            if (pjProp.Key == "steps")
                            {
                                stepsFound         = true;
                                jobPlan.InParallel = JsonConvert.DeserializeObject <List <JobPlan> >(pjProp.Value.ToString(), Converter.Settings);
                            }
                        }
                    }

                    if (!stepsFound)
                    {
                        jobPlan.InParallel = JsonConvert.DeserializeObject <List <JobPlan> >(property.Value.ToString(), Converter.Settings);
                    }
                    continue;
                }

                if (property.Name == "do")
                {
                    jobPlan.Do = JsonConvert.DeserializeObject <List <JobPlan> >(property.Value.ToString(), Converter.Settings);
                    continue;
                }

                if (property.Name == "try")
                {
                    jobPlan.Try = JsonConvert.DeserializeObject <JobPlan>(property.Value.ToString(), Converter.Settings);
                }

                if (property.Name == "task")
                {
                    jobPlan.Task = property.Value?.ToString();
                    continue;
                }

                if (property.Name == "config")
                {
                    if (property.Value.ToString().StartsWith("_anchor_call"))
                    {
                        jobPlan.ConfigAnchorCall        = new AnchorCall();
                        jobPlan.ConfigAnchorCall.Method = property.Value.ToString();
                    }
                    else
                    {
                        jobPlan.Config = JsonConvert.DeserializeObject <JobPlanConfig>(property.Value.ToString(), Converter.Settings);
                    }
                    continue;
                }

                if (property.Name.StartsWith("config_anchor_decl_"))
                {
                    jobPlan.ConfigAnchorDeclaration        = new AnchorDeclaration();
                    jobPlan.ConfigAnchorDeclaration.Method = property.Name;
                    jobPlan.Config = JsonConvert.DeserializeObject <JobPlanConfig>(property.Value.ToString(), Converter.Settings);
                }

                if (property.Name == "set_pipeline")
                {
                    jobPlan.SetPipeline = property.Value.ToString();
                    continue;
                }

                if (property.Name == "var_files")
                {
                    jobPlan.VarFiles = JsonConvert.DeserializeObject <List <string> >(property.Value?.ToString());
                    continue;
                }

                if (property.Name == "get")
                {
                    jobPlan.Get = property.Value.ToString();
                    continue;
                }

                if (property.Name == "put")
                {
                    jobPlan.Put = property.Value.ToString();
                    continue;
                }

                if (property.Name == "attempts")
                {
                    jobPlan.Attempts = Convert.ToInt32(property.Value.ToString());
                    continue;
                }

                if (property.Name == "trigger")
                {
                    jobPlan.Trigger = property.Value.ToString();
                    continue;
                }

                if (property.Name == "passed")
                {
                    jobPlan.Passed = JsonConvert.DeserializeObject <List <string> >(property.Value?.ToString());
                    continue;
                }

                if (property.Name == "resource")
                {
                    jobPlan.Resource = property.Value?.ToString();
                    continue;
                }

                if (property.Name == "ensure")
                {
                    jobPlan.Ensure = new JobPlanEnsure();
                    if (property.Value.GetType() == typeof(JValue))
                    {
                        jobPlan.Ensure.Value = property.Value?.ToString();
                    }
                    else if (property.Value.GetType() == typeof(JObject))
                    {
                        jobPlan.Ensure = JsonConvert.DeserializeObject <JobPlanEnsure>(property.Value.ToString(), Converter.Settings);
                    }
                    continue;
                }

                if (property.Name == "image")
                {
                    jobPlan.Image = property.Value?.ToString();
                    continue;
                }

                if (property.Name == "file")
                {
                    jobPlan.File = property.Value?.ToString();
                    continue;
                }

                if (property.Name == "params")
                {
                    jobPlan.Params = JsonConvert.DeserializeObject <Dictionary <string, dynamic> >(property.Value?.ToString());
                    continue;
                }

                if (property.Name == "get_params")
                {
                    jobPlan.GetParams = JsonConvert.DeserializeObject <Dictionary <string, dynamic> >(property.Value?.ToString());
                    continue;
                }

                if (property.Name == "put_params")
                {
                    jobPlan.PutParams = JsonConvert.DeserializeObject <Dictionary <string, dynamic> >(property.Value?.ToString());
                    continue;
                }

                if (property.Name == "input_mapping")
                {
                    jobPlan.InputMapping = JsonConvert.DeserializeObject <Dictionary <string, string> >(property.Value?.ToString());
                    continue;
                }

                if (property.Name == "output_mapping")
                {
                    jobPlan.OutputMapping = JsonConvert.DeserializeObject <Dictionary <string, string> >(property.Value?.ToString());
                }

                if (property.Name.StartsWith("_merge"))
                {
                    var mergeCall = new MergeCall();
                    mergeCall.Name    = property.Name;
                    mergeCall.Method  = property.Value?.ToString();
                    jobPlan.MergeCall = mergeCall;
                }
            }

            return(jobPlan);
        }