1. Inside Zhou
  2. Inside.Util

Commits

Inside Zhou  committed 59a8bba

json字符串中的数字默认解析为decimal类型。

  • Participants
  • Parent commits e2f8db2
  • Branches default

Comments (0)

Files changed (5)

File Json/ByteArrayConverter.cs

View file
  • Ignore whitespace
 				return StrToObj(reader.Value.ToString());
 			}
 			else {
-				throw new ArgumentException("Unexpected token parsing date. Expected String, got {0}.".Fmt(CultureInfo.InvariantCulture, reader.TokenType));
+				throw new ArgumentException("Unexpected token parsing byte array. Expected String, got {0}.".Fmt(CultureInfo.InvariantCulture, reader.TokenType));
 			}
 		}
 

File Json/DBNullConverter.cs

View file
  • Ignore whitespace
 				return DBNull.Value;
 			}
 			else {
-				throw new ArgumentException("Unexpected token parsing date. Expected String, got {0}.".Fmt(CultureInfo.InvariantCulture, reader.TokenType));
+				throw new ArgumentException("Unexpected token parsing DBNull. Expected String, got {0}.".Fmt(CultureInfo.InvariantCulture, reader.TokenType));
 			}
 		}
 

File Json/DecimalConverter.cs

View file
  • Ignore whitespace
 				return StrToObj(reader.Value.ToString());
 			}
 			else {
-				throw new ArgumentException("Unexpected token parsing date. Expected String, got {0}.".Fmt(CultureInfo.InvariantCulture, reader.TokenType));
+				throw new ArgumentException("Unexpected token parsing decimal. Expected String, got {0}.".Fmt(CultureInfo.InvariantCulture, reader.TokenType));
 			}
 		}
 

File Json/JsonHelper.cs

View file
  • Ignore whitespace
 		/// 将json字符串反序列化为对象。
 		/// </summary>
 		/// <remarks>
-		/// 如果Json字符串未包含类型细节,则Javascript Object将生成为DyObj,Javascript Array生成为List&lt;dynamic&gt;(dynamic为DyObj)。
+		/// 如果Json字符串未包含类型细节,则Javascript Object将生成为DyObj,Javascript Array生成为List&lt;dynamic&gt;(dynamic为DyObj);不带类型的数字默认被解析为decimal类型
 		/// </remarks>
 		public static object Deserialize(string jsonStr, JsonSerializerSettings settings = null) {
 			if(string.IsNullOrEmpty(jsonStr)) {
 			if(obj.Type == JTokenType.String) {
 				str = obj.Value as string;
 			}
+			else if(obj.Type == JTokenType.Integer || obj.Type == JTokenType.Float) {
+				return Convert.ToDecimal(obj.Value);
+			}
 			else {
 				return obj.Value;
 			}

File Json/SimpleDateTimeConverter.cs

View file
  • Ignore whitespace
 namespace Inside.Util.Json {
 	public class SimpleDateTimeConverter : JsonConverter {
 		internal static bool CanConvert(string str) {
-			var dt = DateTime.Now;
+			DateTime dt;
 			return !string.IsNullOrEmpty(str) && (DateTime.TryParse(str, out dt)) || (str.StartsWith(@"datetime(", StringComparison.Ordinal) && str.EndsWith(@")", StringComparison.Ordinal));
 		}
 
 		internal static DateTime StrToObj(string str) {
-			var dt = DateTime.Now;
+			DateTime dt;
 			if(DateTime.TryParse(str, out dt)) {
-				return DateTime.Parse(str);
+				return dt;
 			}
 
 			if(!CanConvert(str)) {