Ejemplo n.º 1
0
        /// <summary>
        /// Parse and create a subclass of <see cref="XenMessage"/>.
        /// </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 XenMessage output)
        {
            object temp;
            var    converted = TryParse(typeof(XenMessage), message, out temp);

            output = temp as XenMessage;
            return(converted);
        }
Ejemplo 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(IXenMessageFinder finder, string message, out XenMessage output)
        {
            try
            {
                object temp;

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

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

                var msgTemp    = temp as XenMessage;
                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 XenMessage;
                output = actionObject;

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