2010-01-27 Atsushi Enomoto <atsushi@ximian.com>
authorAtsushi Eno <atsushieno@gmail.com>
Wed, 27 Jan 2010 10:28:56 +0000 (10:28 -0000)
committerAtsushi Eno <atsushieno@gmail.com>
Wed, 27 Jan 2010 10:28:56 +0000 (10:28 -0000)
* JsonReader.cs : It seems it can either return int, long or decimal
  depending on the value. Users cannot really predict what type of
  the primitive value can be returned and casts to specific types
  very likely fail. doh.

svn path=/trunk/mcs/; revision=150285

mcs/class/System.Json/System.Json/ChangeLog
mcs/class/System.Json/System.Json/JsonReader.cs

index 4fca25e92dbfc80da6a4326fe14c92fb7a64abaf..6eeef5cef9f2dd660d2ccc468466b60d3dc4ce33 100644 (file)
@@ -1,3 +1,10 @@
+2010-01-27  Atsushi Enomoto  <atsushi@ximian.com>
+
+       * JsonReader.cs : It seems it can either return int, long or decimal
+         depending on the value. Users cannot really predict what type of
+         the primitive value can be returned and casts to specific types
+         very likely fail. doh.
+
 2010-01-27  Atsushi Enomoto  <atsushi@ximian.com>
 
        * JsonReader.cs : use decimal instead of int to parse decimal part
index 2e111c184487f3a25f6c54b7d7f4ed15a2e7275e..60c173c63e924268d199cd266e382b58e83d8280 100644 (file)
@@ -144,6 +144,7 @@ namespace System.Json
                        }
                }
 
+               // It could return either int, long or decimal, depending on the parsed value.
                JsonPrimitive ReadNumericLiteral ()
                {
                        bool negative = false;
@@ -198,6 +199,14 @@ namespace System.Json
                                if (!hasFrac)
                                        return new JsonPrimitive (negative ? -val : val);
                                var v = val + frac;
+                               if (frac == 0) {
+                                       if (negative && int.MinValue <= -v ||
+                                           !negative && v <= int.MaxValue)
+                                               return new JsonPrimitive ((int) (negative ? -v : v));
+                                       if (negative && long.MinValue <= -v ||
+                                           !negative && v <= long.MaxValue)
+                                               return new JsonPrimitive ((long) (negative ? -v : v));
+                               }
                                return new JsonPrimitive (negative ? -v : v);
                        }