Beispiel #1
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var o    = JObject.Load(reader);
            var dict = o.Properties().ToDictionary(p => p.Name, p => p.Value);

            if (!dict.HasAny())
            {
                return(null);
            }

            IScriptCondition scriptCondition = null;

            if (dict.ContainsKey("inline"))
            {
                var inline = dict["inline"].ToString();
                scriptCondition = new InlineScriptCondition(inline);
            }
            if (dict.ContainsKey("source"))
            {
                var inline = dict["source"].ToString();
                scriptCondition = new InlineScriptCondition(inline);
            }
            if (dict.ContainsKey("file"))
            {
                var file = dict["file"].ToString();
                scriptCondition = new FileScriptCondition(file);
            }
            if (dict.ContainsKey("id"))
            {
                var id = dict["id"].ToString();
                scriptCondition = new IndexedScriptCondition(id);
            }

            if (scriptCondition == null)
            {
                return(null);
            }

            if (dict.ContainsKey("lang"))
            {
                scriptCondition.Lang = dict["lang"].ToString();
            }
            if (dict.ContainsKey("params"))
            {
                scriptCondition.Params = dict["params"].ToObject <Dictionary <string, object> >();
            }

            return(scriptCondition);
        }
Beispiel #2
0
        public IScriptCondition Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
        {
            if (reader.GetCurrentJsonToken() != JsonToken.BeginObject)
            {
                return(null);
            }

            var count = 0;
            IScriptCondition            scriptCondition = null;
            string                      language        = null;
            Dictionary <string, object> parameters      = null;

            while (reader.ReadIsInObject(ref count))
            {
                if (AutomataDictionary.TryGetValue(reader.ReadPropertyNameSegmentRaw(), out var value))
                {
                    switch (value)
                    {
                    case 0:
                        scriptCondition = new InlineScriptCondition(reader.ReadString());
                        break;

                    case 1:
                        scriptCondition = new IndexedScriptCondition(reader.ReadString());
                        break;

                    case 2:
                        language = reader.ReadString();
                        break;

                    case 3:
                        parameters = formatterResolver.GetFormatter <Dictionary <string, object> >()
                                     .Deserialize(ref reader, formatterResolver);
                        break;
                    }
                }
            }

            if (scriptCondition == null)
            {
                return(null);
            }

            scriptCondition.Lang   = language;
            scriptCondition.Params = parameters;
            return(scriptCondition);
        }
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var o    = JObject.Load(reader);
            var dict = o.Properties().ToDictionary(p => p.Name, p => p.Value);

            if (!dict.HasAny())
            {
                return(null);
            }

            IScriptCondition scriptCondition = null;

            if (dict.TryGetValue("inline", out JToken inlineToken))
            {
                var inline = inlineToken.ToString();
                scriptCondition = new InlineScriptCondition(inline);
            }
            if (dict.TryGetValue("source", out JToken sourceToken))
            {
                var inline = sourceToken.ToString();
                scriptCondition = new InlineScriptCondition(inline);
            }
            if (dict.TryGetValue("id", out JToken idToken))
            {
                var id = idToken.ToString();
                scriptCondition = new IndexedScriptCondition(id);
            }

            if (scriptCondition == null)
            {
                return(null);
            }

            if (dict.TryGetValue("lang", out JToken langToken))
            {
                scriptCondition.Lang = langToken.ToString();
            }
            if (dict.TryGetValue("params", out JToken paramsToken))
            {
                scriptCondition.Params = paramsToken.ToObject <Dictionary <string, object> >();
            }

            return(scriptCondition);
        }