Ejemplo n.º 1
0
 public static int GetIntegerValueFromObject(object objectValue)
 {
     if (objectValue != null)
     {
         try
         {
             var valueString = StringUtils.GetStringValueOrEmpty(objectValue)
                               ?.Trim(new char[] { '\"', '\'', ' ', ',' });
             if (valueString.IsNullOrEmpty())
             {
                 return(0);
             }
             var dotIndex = valueString.IndexOf('.');
             if (dotIndex > 0)
             {
                 valueString = valueString.Substring(0, dotIndex);
             }
             var value = int.Parse(valueString);
             return(value);
         }
         catch (Exception ex)
         {
             RestmeLogger.LogDebug(
                 $"Failed converting object [{objectValue.GetType()}] into integer value, 0 will be returned. ",
                 ex);
             return(0);
         }
     }
     else
     {
         return(0);
     }
 }
Ejemplo n.º 2
0
 public static string JsonSerialize(object value,
                                    JsonSerializerSettings serializerSettings = null)
 {
     if (value == null)
     {
         return(string.Empty);
     }
     try
     {
         //return ServiceStack.Text.JsonSerializer.SerializeToString(value, value.GetType());
         return(JsonConvert.SerializeObject(value, serializerSettings
                                            ??
                                            new JsonSerializerSettings
         {
             ContractResolver =
                 new PranamJsonResolver(),
             NullValueHandling = NullValueHandling.Ignore,
             MissingMemberHandling = MissingMemberHandling.Ignore
         }));
     }
     catch (Exception ex)
     {
         RestmeLogger.LogDebug(
             "JSON serializing an object type of " +
             (value != null ? value.GetType().Name : "null :" + ex.Message), ex);
         return(string.Empty);
     }
 }
Ejemplo n.º 3
0
        public static T MustBeGreaterThanZero <T>(dynamic val)
        {
            try
            {
                var result = default(T);
                if (val <= 0)
                {
                    throw new PranamException("value cannot be less than or equal to 0.");
                }
                if (result is int)
                {
                    var varResult = GetIntegerValueFromObject((object)val);
                    return(varResult.ToString().JsonDeserialize <T>());
                }

                if (result is long)
                {
                    return(GetLongIntegerValueFromObject((object)val).ToString().JsonDeserialize <T>());
                }
                return(result is decimal
                    ? GetDecimalValueFromObject((object)val).ToString().JsonDeserialize <T>()
                    : (T)val);
            }
            catch (Exception ex)
            {
                RestmeLogger.LogDebug(ex.Message, ex);
                throw ex;
            }
        }
Ejemplo n.º 4
0
        public static T JsonDeserialize <T>(string value,
                                            JsonSerializerSettings jsonSerializerSettings = null)
        {
            try
            {
                if (!value.IsNotNullOrEmpty())
                {
                    return(default(T));
                }
                if (typeof(T).IsPrimitiveType())
                {
                    return((T)Convert.ChangeType(value, typeof(T)));
                }

                return(JsonConvert.DeserializeObject <T>(value,
                                                         jsonSerializerSettings ??
                                                         new JsonSerializerSettings
                {
                    ContractResolver = new PranamJsonResolver(),
                    NullValueHandling = NullValueHandling.Ignore,
                    MissingMemberHandling = MissingMemberHandling.Ignore
                }));
            }
            catch (Exception ex)
            {
                try
                {
                    //in case it is JSON encoded as a JSON string
                    var token = JToken.Parse(value);
                    return(JObject.Parse((string)token)
                           .ToObject <T>(new JsonSerializer()
                    {
                        ContractResolver = new PranamJsonResolver()
                    }));
                }
                catch (Exception ex2)
                {
                    // ignored
                }

                RestmeLogger.LogDebug(
                    $"Deserializing an object to type {typeof(T).FullName} has failed. The original value was: \n {value}",
                    ex);
            }

            return(default(T));
        }
Ejemplo n.º 5
0
 public static string GetStringFromStream(Stream stream, Encoding encoding)
 {
     if (encoding == null)
     {
         encoding = Encoding.UTF8;
     }
     try
     {
         stream.Position = 0;
         using (var reader = new StreamReader(stream, encoding))
         {
             return(reader.ReadToEnd());
         }
     }
     catch (Exception ex)
     {
         RestmeLogger.LogDebug("Failed Converting Stream value into String value: " + ex.Message, ex);
         return(string.Empty);
     }
 }
Ejemplo n.º 6
0
        public static DateTime GetDateTimeFromObjectValue(object objectValue)
        {
            if (objectValue == null)
            {
                return(DateTime.MinValue);
            }
            try
            {
                var dateTime = Convert.ToDateTime(objectValue);
                return(dateTime);
            }
            catch (Exception ex)
            {
                RestmeLogger.LogDebug(
                    "Failed to convert object with type of [ " + objectValue.GetType() + " ] into a DateTime value",
                    ex);
            }

            return(DateTime.MinValue);
        }
Ejemplo n.º 7
0
 public static decimal GetDecimalValueFromObject(object objectValue)
 {
     if (objectValue != null)
     {
         try
         {
             return(decimal.Parse(objectValue.ToString().Trim(new char[] { '\"', '\'', ' ', ',' })));
         }
         catch (Exception ex)
         {
             RestmeLogger.LogDebug(
                 $"Failed converting object [{objectValue.GetType()}] into decimal value, 0 will be returned. ",
                 ex);
             return(0);
         }
     }
     else
     {
         return(0);
     }
 }
Ejemplo n.º 8
0
        public static bool GetBooleanValueFromObject(object objectValue, bool boolReturnedIfFailed = false)
        {
            if (objectValue != null)
            {
                try
                {
                    if (objectValue.GetType() == typeof(string) &&
                        StringUtils.GetStringValueOrEmpty(objectValue) == "1")
                    {
                        return(true);
                    }
                    return(Convert.ToBoolean(objectValue));
                }
                catch (Exception ex)
                {
                    RestmeLogger.LogDebug(ex.Message, ex);
                    return(boolReturnedIfFailed);
                }
            }

            return(boolReturnedIfFailed);
        }
Ejemplo n.º 9
0
        public static Guid GetGuidOrEmpty(object value)
        {
            if (value == null)
            {
                return(Guid.Empty);
            }
            try
            {
                if (value is string)
                {
                    return(new Guid(StringUtils.GetStringValueOrEmpty(value)));
                }
                return((Guid)value);
            }
            catch (Exception ex)
            {
                RestmeLogger.LogDebug(ex.Message, ex);
                return(Guid.Empty);
            }

            return(Guid.Empty);
        }
Ejemplo n.º 10
0
 public static string GetStringValueOrEmpty(object objectValue, bool trim = true)
 {
     if (objectValue == null)
     {
         return(string.Empty);
     }
     try
     {
         var result = Convert.ToString(objectValue);
         if (result == objectValue.GetType().ToString())
         {
             return(string.Empty);
         }
         return(trim ? result.Trim() : result);
     }
     catch (Exception ex)
     {
         RestmeLogger.LogDebug(
             string.Format("Failed Converting object value [typeof: {0} ] into String value: {1}",
                           objectValue.GetType(), ex.Message), ex);
         return(string.Empty);
     }
 }