[banshee] [Hyena.Json] Support integers separately in the JSON parsing



commit 65903eab2d3cee2a47e6ec83e35e19ead8842efe
Author: Sandy Armstrong <sanfordarmstrong gmail com>
Date:   Sat Aug 29 23:12:52 2009 +0200

    [Hyena.Json] Support integers separately in the JSON parsing
    
    Previously, all numbers were stored as doubles, forcing the library user
    to make the type determination.
    This patch comes from the copy of Hyena.Json in the Tomboy source.
    
    Signed-off-by: Bertrand Lorentz <bertrand lorentz gmail com>

 .../Hyena/Hyena.Json/Tests/TokenizerTests.cs       |   34 +++++++++---------
 src/Libraries/Hyena/Hyena.Json/Token.cs            |    5 +++
 src/Libraries/Hyena/Hyena.Json/TokenType.cs        |    7 ++--
 src/Libraries/Hyena/Hyena.Json/Tokenizer.cs        |   38 +++++++++++++-------
 4 files changed, 51 insertions(+), 33 deletions(-)
---
diff --git a/src/Libraries/Hyena/Hyena.Json/Tests/TokenizerTests.cs b/src/Libraries/Hyena/Hyena.Json/Tests/TokenizerTests.cs
index 696fa95..05f0bd0 100644
--- a/src/Libraries/Hyena/Hyena.Json/Tests/TokenizerTests.cs
+++ b/src/Libraries/Hyena/Hyena.Json/Tests/TokenizerTests.cs
@@ -67,18 +67,17 @@ namespace Hyena.Json.Tests
         [Test]
         public void NumberInt ()
         {
-            // Number tests
-            AssertTokenStream ("0", Token.Number (0));
-            AssertTokenStream ("-0", Token.Number (-0));
+            AssertTokenStream ("0", Token.Integer (0));
+            AssertTokenStream ("-0", Token.Integer (-0));
             
-            AssertTokenStream ("9", Token.Number (9));
-            AssertTokenStream ("-9", Token.Number (-9));
+            AssertTokenStream ("9", Token.Integer (9));
+            AssertTokenStream ("-9", Token.Integer (-9));
             
-            AssertTokenStream ("14", Token.Number (14));
-            AssertTokenStream ("-14", Token.Number (-14));
+            AssertTokenStream ("14", Token.Integer (14));
+            AssertTokenStream ("-14", Token.Integer (-14));
             
-            AssertTokenStream ("15309", Token.Number (15309));
-            AssertTokenStream ("-15309", Token.Number (-15309));
+            AssertTokenStream ("15309", Token.Integer (15309));
+            AssertTokenStream ("-15309", Token.Integer (-15309));
         }
         
         [Test]
@@ -148,14 +147,14 @@ namespace Hyena.Json.Tests
         [Test]
         public void Array ()
         {
-            AssertTokenStream ("[1]", Token.ArrayStart, Token.Number (1), Token.ArrayFinish);
-            AssertTokenStream ("[1,0]", Token.ArrayStart, Token.Number (1), Token.Comma, Token.Number (0), Token.ArrayFinish);
-            AssertTokenStream ("[\"a\",true,null]", Token.ArrayStart, Token.String ("a"), Token.Comma, 
+            AssertTokenStream ("[1]", Token.ArrayStart, Token.Integer (1), Token.ArrayFinish);
+            AssertTokenStream ("[1,0]", Token.ArrayStart, Token.Integer (1), Token.Comma, Token.Integer (0), Token.ArrayFinish);
+            AssertTokenStream ("[\"a\",true,null]", Token.ArrayStart, Token.String ("a"), Token.Comma,
                 Token.Bool (true), Token.Comma, Token.Null, Token.ArrayFinish);
-            AssertTokenStream ("[0,1,[[2,[4]],5],6]", Token.ArrayStart, Token.Number (0), Token.Comma, Token.Number (1),
-                 Token.Comma, Token.ArrayStart, Token.ArrayStart, Token.Number (2), Token.Comma, Token.ArrayStart, 
-                 Token.Number (4), Token.ArrayFinish, Token.ArrayFinish, Token.Comma, Token.Number (5), Token.ArrayFinish,
-                 Token.Comma, Token.Number (6), Token.ArrayFinish);
+            AssertTokenStream ("[0,1,[[2,[4]],5],6]", Token.ArrayStart, Token.Integer (0), Token.Comma, Token.Integer (1),
+                 Token.Comma, Token.ArrayStart, Token.ArrayStart, Token.Integer (2), Token.Comma, Token.ArrayStart,
+                 Token.Integer (4), Token.ArrayFinish, Token.ArrayFinish, Token.Comma, Token.Integer (5), Token.ArrayFinish,
+                 Token.Comma, Token.Integer (6), Token.ArrayFinish);
         }
         
         [Test]
@@ -194,7 +193,8 @@ namespace Hyena.Json.Tests
                     continue;
                 }
                 
-                if ((compare.Type == TokenType.Number && (double)compare.Value != (double)token.Value) ||
+                if ((compare.Type == TokenType.Integer && (int)compare.Value != (int)token.Value) ||
+                    (compare.Type == TokenType.Number && (double)compare.Value != (double)token.Value) ||
                     (compare.Type == TokenType.String && (string)compare.Value != (string)token.Value) ||
                     (compare.Type == TokenType.Boolean && (bool)compare.Value != (bool)token.Value)) {
                     throw new ApplicationException ("Token values do not match");
diff --git a/src/Libraries/Hyena/Hyena.Json/Token.cs b/src/Libraries/Hyena/Hyena.Json/Token.cs
index cce60c7..c1ef414 100644
--- a/src/Libraries/Hyena/Hyena.Json/Token.cs
+++ b/src/Libraries/Hyena/Hyena.Json/Token.cs
@@ -98,6 +98,11 @@ namespace Hyena.Json
             return new Token (TokenType.Number, value);
         }
         
+        internal static Token Integer (int value)
+        {
+            return new Token (TokenType.Integer, value);
+        }
+        
         internal static Token String (string value)
         {
             return new Token (TokenType.String, value);
diff --git a/src/Libraries/Hyena/Hyena.Json/TokenType.cs b/src/Libraries/Hyena/Hyena.Json/TokenType.cs
index b24a5ca..cd6ae93 100644
--- a/src/Libraries/Hyena/Hyena.Json/TokenType.cs
+++ b/src/Libraries/Hyena/Hyena.Json/TokenType.cs
@@ -42,10 +42,11 @@ namespace Hyena.Json
         String = 1 << 5,
         Null = 1 << 6,
         Number = 1 << 7,
-        Comma = 1 << 8,
-        Colon = 1 << 9,
+        Integer = 1 << 8,
+        Comma = 1 << 9,
+        Colon = 1 << 10,
         
-        Literal = String | Number | Boolean | Null,
+        Literal = String | Number | Boolean | Null | Integer,
         Value = ObjectStart | ArrayStart | Literal
     }
 }
diff --git a/src/Libraries/Hyena/Hyena.Json/Tokenizer.cs b/src/Libraries/Hyena/Hyena.Json/Tokenizer.cs
index f866c0b..618ed3a 100644
--- a/src/Libraries/Hyena/Hyena.Json/Tokenizer.cs
+++ b/src/Libraries/Hyena/Hyena.Json/Tokenizer.cs
@@ -163,14 +163,14 @@ namespace Hyena.Json
             return buffer.ToString ();
         }
         
-        private double LexInt ()
+        private int LexInt ()
         {
             return LexInt (false, 0);
         }
         
-        private double LexInt (bool hex, int maxDigits)
+        private int LexInt (bool hex, int maxDigits)
         {
-            double value = 0.0;
+            int value = 0;
             int count = 0;
             
             do {
@@ -212,34 +212,38 @@ namespace Hyena.Json
             return fraction;
         }
         
-        private double LexNumber ()
+        private object LexNumber (out bool isDouble)
         {
-            double value = 0.0;
+            isDouble = false;
+            int  intVal = 0;
+            double doubleVal = 0.0;
             bool negate = peek == '-';
             if (negate) {
                 ReadChar ();
             }
             
             if (peek != '0') {
-                value = LexInt ();
+                doubleVal = intVal = LexInt ();
             } else {
                 ReadChar ();
             }
             
             if (peek == '.') {
-                value += LexFraction ();
+                isDouble = true;
+                doubleVal += LexFraction ();
             }
             
             if (peek == 'e' || peek == 'E') {
+                isDouble = true;
                 ReadChar ();
                 if (peek == '-') {
                     ReadChar ();
-                    value /= Math.Pow (10, LexInt ());
+                    doubleVal /= Math.Pow (10, LexInt ());
                 } else if (peek == '+') {
                     ReadChar ();
-                    value *= Math.Pow (10, LexInt ());
+                    doubleVal *= Math.Pow (10, LexInt ());
                 } else if (Char.IsDigit (peek)) {
-                    value *= Math.Pow (10, LexInt ());
+                    doubleVal *= Math.Pow (10, LexInt ());
                 } else {
                     InvalidSyntax ("Malformed exponent");
                 }
@@ -249,8 +253,11 @@ namespace Hyena.Json
                 InvalidSyntax ("Numbers starting with 0 must be followed by a . or not " +
                     "followed by a digit (octal syntax not legal)");
             }
-            
-            return negate ? -1.0 * value : value;
+
+            if (!isDouble)
+                return negate ? -1 * intVal : intVal;
+            else
+                return negate ? -1.0 * doubleVal : doubleVal;
         }
         
         public Token Scan ()
@@ -291,7 +298,12 @@ namespace Hyena.Json
                 case '"': return new Token (TokenType.String, LexString ());
                 default:
                     if (peek == '-' || Char.IsDigit (peek)) {
-                        return new Token (TokenType.Number, LexNumber ());
+                        bool isDouble;
+                        object num = LexNumber (out isDouble);
+                        if (!isDouble)
+                            return new Token (TokenType.Integer, num);
+                        else
+                            return new Token (TokenType.Number, num);
                     } else if (Char.IsLetter (peek)) {
                         string identifier = LexId ();
                         switch (identifier) {



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]