Example #1
0
 private void Populate(AdaptiveTypedElement element)
 {
     if (element.Elements != null)
     {
         foreach (var el in element.Elements)
         {
             Elements[el.Key] = el.Value;
         }
     }
 }
Example #2
0
        private void HandleAdditionalProperties(AdaptiveTypedElement te)
        {
            // https://stackoverflow.com/questions/34995406/nullvaluehandling-ignore-influences-deserialization-into-extensiondata-despite

            // The default behavior of JsonExtensionData is to include properties if the VALUE could not be set, including abstract properties or default values
            // We don't want to deserialize any properties that exist on the type into AdditionalProperties, so this removes them
            te.AdditionalProperties
            .Select(prop => te.GetType().GetRuntimeProperties()
                    .SingleOrDefault(p => p.Name.Equals(prop.Key, StringComparison.OrdinalIgnoreCase)))
            .Where(p => p != null)
            .ToList()
            .ForEach(p => te.AdditionalProperties.Remove(p.Name));

            foreach (var prop in te.AdditionalProperties)
            {
                Warnings.Add(new AdaptiveWarning(-1, $"Unknown property '{prop.Key}' on '{te.Type}'"));
            }
        }
Example #3
0
        public ElementRepeater(AdaptiveTypedElement sourceElement)
        {
            AdaptiveCard card = new AdaptiveCard("1.1");

            if (sourceElement is AdaptiveElement bodyElement)
            {
                card.Body.Add(bodyElement);
            }
            else if (sourceElement is AdaptiveAction action)
            {
                card.Actions.Add(action);
            }
            else
            {
                throw new Exception("Unknown type");
            }
            _cardJson = card.ToJson();
        }
        private void HandleAdditionalProperties(AdaptiveTypedElement te)
        {
            // https://stackoverflow.com/questions/34995406/nullvaluehandling-ignore-influences-deserialization-into-extensiondata-despite

            // The default behavior of JsonExtensionData is to include properties if the VALUE could not be set, including abstract properties or default values
            // We don't want to deserialize any properties that exist on the type into AdditionalProperties, so this function removes them

            // Create a list of known property names
            List <String> knownPropertyNames             = new List <String>();
            IEnumerable <PropertyInfo> runtimeProperties = te.GetType().GetRuntimeProperties();

            foreach (PropertyInfo runtimeProperty in runtimeProperties)
            {
                // Check if the property has a JsonPropertyAttribute with the value set
                String jsonPropertyName = null;
                foreach (var attribute in runtimeProperty.CustomAttributes)
                {
                    if (attribute.AttributeType == typeof(Newtonsoft.Json.JsonPropertyAttribute) &&
                        attribute.ConstructorArguments.Count == 1)
                    {
                        jsonPropertyName = attribute.ConstructorArguments[0].Value as String;
                        break;
                    }
                }

                // Add the json property name if present, otherwise use the runtime property name
                knownPropertyNames.Add(jsonPropertyName != null ? jsonPropertyName : runtimeProperty.Name);
            }

            te.AdditionalProperties
            .Select(prop => knownPropertyNames
                    .SingleOrDefault(p => p.Equals(prop.Key, StringComparison.OrdinalIgnoreCase)))
            .Where(p => p != null)
            .ToList()
            .ForEach(p => te.AdditionalProperties.Remove(p));

            foreach (var prop in te.AdditionalProperties)
            {
                Warnings.Add(new AdaptiveWarning((int)WarningStatusCode.UnknownElementType, $"Unknown property '{prop.Key}' on '{te.Type}'"));
            }
        }
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var jObject = JObject.Load(reader);

            string typeName = GetElementTypeName(objectType, jObject);

            if (TypedElementTypes.Value.TryGetValue(typeName, out var type))
            {
                string objectId = jObject.Value <string>("id");
                if (objectId == null)
                {
                    if (typeof(AdaptiveInput).GetTypeInfo().IsAssignableFrom(type.GetTypeInfo()))
                    {
                        throw new AdaptiveSerializationException($"Required property 'id' not found on '{typeName}'");
                    }
                }

                // add id of element to ParseContext
                AdaptiveInternalID internalID = AdaptiveInternalID.Current();
                if (type != typeof(AdaptiveCard))
                {
                    internalID = AdaptiveInternalID.Next();
                    ParseContext.PushElement(objectId, internalID);
                }

                var result = (AdaptiveTypedElement)Activator.CreateInstance(type);
                try
                {
                    serializer.Populate(jObject.CreateReader(), result);
                }
                catch (JsonSerializationException) { }

                // remove id of element from ParseContext
                if (type != typeof(AdaptiveCard))
                {
                    ParseContext.PopElement();
                }

                HandleAdditionalProperties(result);
                return(result);
            }
            else // We're looking at an unknown element
            {
                string             objectId   = jObject.Value <string>("id");
                AdaptiveInternalID internalID = AdaptiveInternalID.Next();

                // Handle deserializing unknown element
                ParseContext.PushElement(objectId, internalID);
                AdaptiveTypedElement result = null;
                if (ParseContext.Type == ParseContext.ContextType.Element)
                {
                    result = (AdaptiveTypedElement)Activator.CreateInstance(typeof(AdaptiveUnknownElement));
                    serializer.Populate(jObject.CreateReader(), result);
                }
                else // ParseContext.Type == ParseContext.ContextType.Action
                {
                    result = (AdaptiveTypedElement)Activator.CreateInstance(typeof(AdaptiveUnknownAction));
                    serializer.Populate(jObject.CreateReader(), result);
                }
                ParseContext.PopElement();

                Warnings.Add(new AdaptiveWarning(-1, $"Unknown element '{typeName}'"));
                return(result);
            }
        }
Example #6
0
 public AdaptiveFallbackElement(AdaptiveTypedElement elem)
 {
     Type    = AdaptiveFallbackType.Content;
     Content = elem;
 }