Ejemplo n.º 1
0
        /// <summary>
        ///  尝试将JSON字符串反序列化为指定.NET类型。
        /// </summary>
        /// <typeparam name="T">要反序列化的对象的类型。</typeparam>
        /// <param name="value">要反序列化的JSON字符串。</param>
        /// <param name="defVal">反序列化失败默认值。</param>
        /// <returns></returns>
        public static T TryDeserialize <T>(string value, T defVal)
        {
            T result = default(T);

            TryUtil.Action(() =>
            {
                result = Deserialize <T>(value);
            }, (ex) =>
            {
                result = defVal;
            });
            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 尝试将JSON字符串反序列化为指定的匿名类型。
        /// </summary>
        /// <typeparam name="T">要反序列化的匿名对象类型。</typeparam>
        /// <param name="value">要反序列化的JSON字符串。</param>
        /// <param name="anonymousTypeObject">匿名类型对象。</param>
        /// <returns></returns>
        public static T TryDeserializeAnonymousType <T>(string value, T anonymousTypeObject)
        {
            T result = default(T);

            TryUtil.Action(() =>
            {
                result = DeserializeAnonymousType(value, anonymousTypeObject);
            }, (ex) =>
            {
                result = anonymousTypeObject;
            });
            return(result);
        }