public static AdaptiveCard Parse(string json) { AdaptiveElementParserRegistration elementRegistration = new AdaptiveElementParserRegistration(); elementRegistration.Set("ActionSet", new ActionSetParser()); return(AdaptiveCard.FromJsonString(json, elementRegistration, null).AdaptiveCard); }
public void ElementParserRegistraton_CustomElementTest() { AdaptiveActionParserRegistration actionParserRegistration = new AdaptiveActionParserRegistration(); AdaptiveElementParserRegistration elementParserRegistration = new AdaptiveElementParserRegistration(); List <AdaptiveWarning> warnings = new List <AdaptiveWarning>(); elementParserRegistration.Set("TestCustomElement", new TestElementParser()); IAdaptiveElementParser testElementParserRetrieved = elementParserRegistration.Get("TestCustomElement"); Assert.IsNotNull(testElementParserRetrieved); Assert.IsNotNull(testElementParserRetrieved as TestElementParser); String testCard = "{" + " \"type\":\"AdaptiveCard\"," + " \"version\":\"1.0\"," + " \"body\":" + " [" + " {" + " \"type\":\"TestCustomElement\"," + " \"internalTextBlock\":" + " {" + " \"type\":\"TextBlock\"," + " \"text\":\"Here is some text\"" + " }" + " }" + " ]" + "}"; AdaptiveCard card = AdaptiveCard.FromJsonString(testCard, elementParserRegistration, actionParserRegistration).AdaptiveCard; Assert.IsNotNull(card); Assert.AreEqual(1, card.Body.Count); IAdaptiveCardElement cardElement = card.Body[0]; Assert.IsNotNull(cardElement); Assert.AreEqual(ElementType.Custom, cardElement.ElementType); Assert.AreEqual("TestCustomElement", cardElement.ElementTypeString); TestCustomElement customElement = card.Body[0] as TestCustomElement; Assert.IsNotNull(customElement); Assert.AreEqual(ElementType.TextBlock, customElement.InternalTextBlock.ElementType); }
internal static JsonParseToastResult ParseToast(string json, FeatureSet currFeatureSet) { JsonToastContent toastContent = null; JsonParseToastResult result = new JsonParseToastResult(); int payloadSize = System.Text.Encoding.UTF8.GetByteCount(json); if (payloadSize > PAYLOAD_SIZE_LIMIT) { result.Errors.Add(new ParseError(ParseErrorType.ErrorButRenderAllowed, $"Your payload exceeds the 5 KB size limit (it is {payloadSize.ToString("N0")} Bytes). Please reduce your payload, or else Windows will not display it.")); } var settings = new JsonSerializerSettings() { MissingMemberHandling = MissingMemberHandling.Error, Error = new EventHandler <ErrorEventArgs>((sender, args) => { HandleError(result.Errors, args); }), ContractResolver = new DefaultContractResolver() { NamingStrategy = new JsonToastNamingStrategy() } }; try { toastContent = JsonConvert.DeserializeObject <JsonToastContent>(json, settings); if (toastContent.Type != "AdaptiveCard") { result.Errors.Add(new ParseError(ParseErrorType.ErrorButRenderAllowed, "\"type\": \"AdaptiveCard\" must be specified in your payload.")); } if (toastContent.Version == null) { result.Errors.Add(new ParseError(ParseErrorType.ErrorButRenderAllowed, "\"version\" property must be specified in your payload.")); } var actionReg = new AdaptiveActionParserRegistration(); // Can't override the Submit parser so can't know whether dev actually sent -ms-systemActivationType="dismiss" or if we translated it actionReg.Set("MsAction.Dismiss", new DismissActionParser(result.Errors)); actionReg.Set("MsAction.Snooze", new SnoozeActionParser(result.Errors)); AdaptiveElementParserRegistration elementRegistration = new AdaptiveElementParserRegistration(); elementRegistration.Set("ActionSet", new ActionSetParser()); var cardParseResult = AdaptiveCard.FromJsonString(json, elementRegistration, actionReg); toastContent.Card = cardParseResult.AdaptiveCard; foreach (var error in cardParseResult.Errors) { result.Errors.Add(new ParseError(ParseErrorType.ErrorButRenderAllowed, error.Message)); } foreach (var warning in cardParseResult.Warnings) { result.Errors.Add(new ParseError(ParseErrorType.Warning, warning.Message)); } } catch (Exception ex) { // Json parse exceptions are handled by the error handler and already reported if (!(ex is JsonReaderException)) { result.Errors.Add(new ParseError(ParseErrorType.Error, ex.Message)); } } result.Content = toastContent; return(result); }