Certain comments raise exceptions

Feb 13, 2011 at 12:07 PM

First, thanks for your work building this tool and making it freely available!

I noticed that comments only work in some contexts. (I'm using Json40r1.zip.) For example, deserializing this raises an exception:

{"indexed":true, "startYear":1939, "values":
                            [  3000,   3000,   3000,
                               3000,   3000,   3000,   3000,   3000,   3000,   3000,   3000,   3000,   3000,  /* 1940-1949 */
                               3000,   3600,   3600,   3600,   3600,   4200,   4200,   4200,   4200,   4800,  /* 1950-1959 */
                               4800,   4800,   4800,   4800,   4800,   4800,   6600,   6600,   7800,   7800,  /* 1960-1969 */
                               7800,   7800,   9000,  10800,  13200,  14100,  15300,  16500,  17700,  22900,  /* 1970-1979 */
                              25900,  29700,  32400,  35700,  37800,  39600,  42000,  43800,  45000,  48000,  /* 1980-1989 */
                              51300,  53400,  55500,  57600,  60600,  61200,  62700,  65400,  68400,  72600,  /* 1990-1999 */
                              76200,  80400,  84900,  87000,  87900,  90000,  94200,  97500, 102000, 106800,  /* 2000-2009 */
                             106800, 106800]  /* 2010-2011 */
                                },

Here is a patch file that corrects for the trouble spots I encountered. (Send me an email if you'd prefer it as an attachment; that doesn't seem possible here.) In looking over the code for JsonTextReader.cs, I noticed there seem to be several other methods that need comment-awareness, e.g. ReadAsDateTimeOffset, but I thought I should leave that up to you.

diff -ru json.net.orig/Source/Src/Newtonsoft.Json/JsonTextReader.cs my.json.net/Source/Src/Newtonsoft.Json/JsonTextReader.cs
--- json.net.orig/Source/Src/Newtonsoft.Json/JsonTextReader.cs	2010-09-29 20:14:32.000000000 -0400
+++ my.json.net/Source/Src/Newtonsoft.Json/JsonTextReader.cs	2011-02-12 21:58:56.204600000 -0500
@@ -347,6 +347,8 @@
         return null;
       if (TokenType == JsonToken.Float)
         return (decimal?)Value;
+      if (TokenType == JsonToken.Comment)
+          return ReadAsDecimal();
 
       throw CreateJsonReaderException("Unexpected token when reading decimal: {0}. Line {1}, position {2}.", TokenType, _currentLineNumber, _currentLinePosition);
     }
diff -ru json.net.orig/Source/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs my.json.net/Source/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs
--- json.net.orig/Source/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs	2011-01-03 18:19:58.000000000 -0500
+++ my.json.net/Source/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs	2011-02-12 21:47:12.997600000 -0500
@@ -638,6 +638,9 @@
             contract.InvokeOnDeserialized(dictionary.UnderlyingDictionary, Serializer.Context);
             
             return dictionary.UnderlyingDictionary;
+          case JsonToken.Comment:
+            // do nothing
+            break;
           default:
             throw new JsonSerializationException("Unexpected token when deserializing object: " + reader.TokenType);
         }
@@ -745,6 +748,9 @@
           case JsonToken.EndObject:
             exit = true;
             break;
+          case JsonToken.Comment:
+            // do nothing
+            break;
           default:
             throw new JsonSerializationException("Unexpected token when deserializing object: " + reader.TokenType);
         }
@@ -888,6 +894,9 @@
           case JsonToken.EndObject:
             exit = true;
             break;
+          case JsonToken.Comment:
+            // do nothing
+            break;
           default:
             throw new JsonSerializationException("Unexpected token when deserializing object: " + reader.TokenType);
         }

Coordinator
Mar 26, 2011 at 2:23 AM

I've fixed up comments causing errors in some places. The fixes are in the latest source code.