Ejemplo n.º 1
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var token = reader.TokenType;

            switch (token)
            {
            case JsonToken.String:
            {
                AdaptiveFallbackElement adaptiveFallbackElement = new AdaptiveFallbackElement();

                string stringValue = (string)reader.Value;
                if (stringValue == "drop")
                {
                    adaptiveFallbackElement.Type = AdaptiveFallbackElement.AdaptiveFallbackType.Drop;
                }
                else
                {
                    throw new AdaptiveSerializationException("The only valid string value for the fallback property is 'drop'.");
                }
                return(adaptiveFallbackElement);
            }

            case JsonToken.StartObject:
            {
                var jObject = JObject.Load(reader);

                var  typeName = AdaptiveTypedElementConverter.GetElementTypeName(objectType, jObject);
                Type type;
                if (!AdaptiveTypedElementConverter.TypedElementTypes.Value.TryGetValue(typeName, out type))
                {
                    type = typeof(AdaptiveUnknownElement);
                }
                IsInFallback = true;
                string             objectId   = jObject.Value <string>("id");
                AdaptiveInternalID internalID = AdaptiveInternalID.Next();

                // Handle deserializing unknown element
                ParseContext.PushElement(objectId, internalID);

                var result = (AdaptiveTypedElement)Activator.CreateInstance(type);
                serializer.Populate(jObject.CreateReader(), result);
                ParseContext.PopElement();
                IsInFallback = false;

                AdaptiveFallbackElement adaptiveFallbackElement = new AdaptiveFallbackElement();
                adaptiveFallbackElement.Type    = AdaptiveFallbackElement.AdaptiveFallbackType.Content;
                adaptiveFallbackElement.Content = result;
                return(adaptiveFallbackElement);
            }

            default:
            {
                throw new AdaptiveSerializationException("Invalid value for fallback");
            }
            }
        }
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var jObject = JObject.Load(reader);

            if (jObject.Value <string>("type") != AdaptiveCard.TypeName)
            {
                throw new AdaptiveSerializationException($"Property 'type' must be '{AdaptiveCard.TypeName}'");
            }

            var typedElementConverter = new AdaptiveTypedElementConverter(_parseResult);

            var card = (AdaptiveCard)typedElementConverter.ReadJson(jObject.CreateReader(), objectType, existingValue, serializer);

            // If this is the root AdaptiveCard and missing a version we fail parsing.
            // The depth checks that cards within a Action.ShowCard don't require the version
            if (reader.Depth == 0 && card.Version == null)
            {
                throw new AdaptiveSerializationException("Required property 'version' not found on AdaptiveCard");
            }

            return(card);
        }
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var jObject = JObject.Load(reader);

            if (jObject.Value <string>("type") != AdaptiveCard.TypeName)
            {
                throw new AdaptiveSerializationException($"Property 'type' must be '{AdaptiveCard.TypeName}'");
            }

            if (reader.Depth == 0)
            {
                AdaptiveTypedElementConverter.BeginCard();

                if (jObject.Value <string>("version") == null)
                {
                    throw new AdaptiveSerializationException("Could not parse required key: version. It was not found.");
                }

                // If this is the root AdaptiveCard and missing a version we fail parsing.
                // The depth checks that cards within a Action.ShowCard don't require the version
                if (jObject.Value <string>("version") == "")
                {
                    throw new AdaptiveSerializationException("Property is required but was found empty: version");
                }

                if (new AdaptiveSchemaVersion(jObject.Value <string>("version")) > AdaptiveCard.KnownSchemaVersion)
                {
                    return(MakeFallbackTextCard(jObject));
                }
            }
            var typedElementConverter = serializer.ContractResolver.ResolveContract(typeof(AdaptiveTypedElement)).Converter;

            var card = (AdaptiveCard)typedElementConverter.ReadJson(jObject.CreateReader(), objectType, existingValue, serializer);

            card.Lang = ValidateLang(jObject.Value <string>("lang"));

            return(card);
        }