Example #1
0
        /// <summary>
        /// Deserializes a json string to an object
        /// </summary>
        /// <param name="jsonString">The json string representation of an object.</param>
        /// <typeparam name="T">The type to deserialize to.</typeparam>
        /// <returns>The object represented by the jsonString.</returns>
        public static T DeserializeJson <T>(this string jsonString, ISerializationSettings serializationSettings = null)
        {
            if (string.IsNullOrWhiteSpace(jsonString))
            {
                return(default(T));
            }
            DataContractJsonSerializer ser = DataContractJsonSerializerHelpers.GetSerializer <T>(serializationSettings);
            T obj;

            using (Stream stream = jsonString.ToStream())
            {
                obj = (T)ser.ReadObject(stream);
            }
            return(obj);
        }
        /// <summary>
        /// Serializes an object to json if possible
        /// </summary>
        /// <param name="target">The object to serialize</param>
        /// <param name="loggingService">The logging service to use</param>
        /// <param name="throwOnFail">If the serialization should throw upon failure to serialize</param>
        /// <param name="serializationSettings">The object that provides known types and data contract resolvers</param>
        /// <returns>The serialized json string representation of an object</returns>
        /// <exception cref="System.Exception">
        /// Thrown when the serialization fails, and will be the type that serializer throws. Not thrown if <c>throwOnFail</c> is false.
        /// </exception>
        public static string SerializeJson <T>(this T target, ILoggingService loggingService  = null, bool throwOnFail = true
                                               , ISerializationSettings serializationSettings = null)
        {
            if (target == null)
            {
                return(string.Empty);
            }

            try
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    DataContractJsonSerializer ds = DataContractJsonSerializerHelpers.GetSerializer <T>(serializationSettings);
                    ds.WriteObject(stream, target);
                    string jsonString = stream.GetString();
                    stream.Close();
                    return(jsonString);
                }
            }
            catch (Exception ex)
            {
                // if it wasn't serializable, that's ok, we expected as much
                if (!target.IsSerializable())
                {
                    loggingService?.LogMessage("Attempted serialization failed",
                                               "Attempted to serialize " + target.GetType().Name + ", but it's not serializable.",
                                               LogLevel.Warning);
                    if (throwOnFail)
                    {
                        throw;
                    }
                    return(string.Empty);
                }

                loggingService?.LogException(ex, "ObjectExtensions.SerializeJson Exception. Target type - " + target.GetType());
                if (throwOnFail)
                {
                    throw;
                }
                return(string.Empty);
            }
        }