Esempio n. 1
0
        /// <summary>
        /// Parse and create a subclass of <see cref="UIMessage"/>.
        /// </summary>
        /// <param name="message">JSON</param>
        /// <param name="output">Result</param>
        /// <returns>True if the message was parsed; otherwise, false.</returns>
        public static bool TryParse(string message, out UIMessage output)
        {
            object temp;
            var    converted = TryParse(typeof(UIMessage), message, out temp);

            output = temp as UIMessage;
            return(converted);
        }
Esempio n. 2
0
        /// <summary>
        /// Create an object of the type identified by <see cref="Action"/>.
        /// </summary>
        /// <param name="finder">Responsible for instantiating an object of the required type</param>
        /// <param name="message">JSON</param>
        /// <param name="output">Result</param>
        /// <returns>True if converted; otherwise, false.</returns>
        public static bool TryParseFromAction(IUIMessageFinder finder, string message, out UIMessage output)
        {
            try
            {
                object temp;

                var converted = TryParse(typeof(UnknownMessage), message, out temp);

                if (!converted)
                {
                    output = null;
                    return(false);
                }

                var msgTemp    = temp as UIMessage;
                var actionName = string.Empty;

                if (msgTemp != null)
                {
                    actionName = msgTemp.Action;
                }

                if (string.IsNullOrWhiteSpace(actionName))
                {
                    output = null;
                    return(false);
                }

                var actionType = finder.Find(actionName);

                if (actionType == null)
                {
                    output = null;
                    return(false);
                }

                object tempActionObject;
                var    parsed = TryParse(actionType, message, out tempActionObject);

                var actionObject = tempActionObject as UIMessage;
                output = actionObject;

                return(parsed && actionObject != null);
            }
            catch (Exception)
            {
                output = null;
                return(false);
            }
        }