Esempio n. 1
0
        /// <summary>
        /// Converts the specified JSON string to a list of <see cref="IMutableMessage"/>.
        /// </summary>
        public static IList <IMutableMessage> ParseMessages(string content)
        {
            if (null == content || (content = content.Trim()).Length == 0)
            {
                return(null);
            }

            IList <IDictionary <string, object> > list = null;
            bool tryNext = false;

            try
            {
                list = ObjectConverter.Deserialize <IList <IDictionary <string, object> > >(content);
            }
            catch (ArgumentException) { tryNext = true; }
            catch (InvalidOperationException) { tryNext = true; }

            if (tryNext)
            {
                // DEBUG
                //Trace.TraceInformation("Try to parse to IDictionary from JSON content: {0}", content);
                try
                {
                    IDictionary <string, object> obj
                        = ObjectConverter.Deserialize <IDictionary <string, object> >(content);

                    if (null != obj)
                    {
                        list = new List <IDictionary <string, object> >();
                        list.Add(obj);
                    }
                }
                catch (ArgumentException ex)
                {
                    // DEBUG
                    Trace.TraceWarning("Failed to parse invalid JSON content: {1}{0}{2}",
                                       Environment.NewLine, content, ex.ToString());
                }
                catch (InvalidOperationException ex)
                {
                    // DEBUG
                    Trace.TraceWarning("Exception when parsing to IDictionary from JSON content: {1}{0}{2}",
                                       Environment.NewLine, content, ex.ToString());
                }
            }

            IList <IMutableMessage> result = new List <IMutableMessage>();

            if (list != null)
            {
                foreach (IDictionary <string, object> message in list)
                {
                    if (message != null && message.Count > 0)
                    {
                        result.Add(new DictionaryMessage(message));
                    }
                }
            }

            return(result);
        }
Esempio n. 2
0
 /// <summary>
 /// Get option or default value.
 /// </summary>
 /// <param name="optionName">The option name.</param>
 /// <param name="defaultValue">The default value.</param>
 /// <returns>The option or default value.</returns>
 /// <seealso cref="GetOption(string)"/>
 public virtual T GetOption <T>(string optionName, T defaultValue) where T : struct
 {
     return(ObjectConverter.ToPrimitive <T>(this.GetOption(optionName), defaultValue));
 }
Esempio n. 3
0
 /// <summary>
 /// Represents this message as a JSON string.
 /// </summary>
 public override string ToString()
 {
     return(ObjectConverter.Serialize(this as IDictionary <string, object>));
 }