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

            var     count    = 0;
            IScript script   = 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:
                    case 1:
                        script = new InlineScript(reader.ReadString());
                        break;

                    case 2:
                        script = new IndexedScript(reader.ReadString());
                        break;

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

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

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

            script.Lang   = language;
            script.Params = parameters;
            return(script);
        }
        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);
            }

            IScript script = null;

            if (dict.ContainsKey("inline"))
            {
                var inline = dict["inline"].ToString();
                script = new InlineScript(inline);
            }
            if (dict.ContainsKey("source"))
            {
                var inline = dict["source"].ToString();
                script = new InlineScript(inline);
            }
            if (dict.ContainsKey("file"))
            {
                var file = dict["file"].ToString();
                script = new FileScript(file);
            }
            if (dict.ContainsKey("id"))
            {
                var id = dict["id"].ToString();
                script = new IndexedScript(id);
            }

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

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

            return(script);
        }
        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);
            }

            IScript script = null;

            if (dict.TryGetValue("inline", out JToken inlineToken))
            {
                var inline = inlineToken.ToString();
                script = new InlineScript(inline);
            }
            if (dict.TryGetValue("source", out JToken sourceToken))
            {
                var inline = sourceToken.ToString();
                script = new InlineScript(inline);
            }
            if (dict.TryGetValue("id", out JToken idToken))
            {
                var id = idToken.ToString();
                script = new IndexedScript(id);
            }

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

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

            return(script);
        }