Skip to content
This repository
Browse code

Fixes handling of windows line-endings.

  • Loading branch information...
commit d5ff3c8e2438ff78ec357d535bf5fbe65ad61423 1 parent 4451213
Erik Osheim authored January 11, 2013
8  json/src/main/scala/blueeyes/json/Parser.scala
@@ -229,6 +229,7 @@ private[json] trait Parser {
229 229
     // ignore whitespace
230 230
     case ' ' => parse(i + 1)
231 231
     case '\t' => parse(i + 1)
  232
+    case '\r' => parse(i + 1)
232 233
     case '\n' => newline(i); parse(i + 1)
233 234
 
234 235
     // if we have a recursive top-level structure, we'll delegate the parsing
@@ -276,6 +277,7 @@ private[json] trait Parser {
276 277
       case DATA => (at(i): @switch) match {
277 278
         case ' ' => rparse(state, i + 1, stack)
278 279
         case '\t' => rparse(state, i + 1, stack)
  280
+        case '\r' => rparse(state, i + 1, stack)
279 281
         case '\n' => newline(i); rparse(state, i + 1, stack)
280 282
 
281 283
         case '[' => rparse(ARRBEG, i + 1, new ArrContext :: stack)
@@ -314,6 +316,7 @@ private[json] trait Parser {
314 316
       case KEY => (at(i): @switch) match {
315 317
         case ' ' => rparse(state, i + 1, stack)
316 318
         case '\t' => rparse(state, i + 1, stack)
  319
+        case '\r' => rparse(state, i + 1, stack)
317 320
         case '\n' => newline(i); rparse(state, i + 1, stack)
318 321
 
319 322
         case '"' =>
@@ -327,6 +330,7 @@ private[json] trait Parser {
327 330
       case ARRBEG => (at(i): @switch) match {
328 331
         case ' ' => rparse(state, i + 1, stack)
329 332
         case '\t' => rparse(state, i + 1, stack)
  333
+        case '\r' => rparse(state, i + 1, stack)
330 334
         case '\n' => newline(i); rparse(state, i + 1, stack)
331 335
 
332 336
         case ']' => stack match {
@@ -346,6 +350,7 @@ private[json] trait Parser {
346 350
       case OBJBEG => (at(i): @switch) match {
347 351
         case ' ' => rparse(state, i + 1, stack)
348 352
         case '\t' => rparse(state, i + 1, stack)
  353
+        case '\r' => rparse(state, i + 1, stack)
349 354
         case '\n' => newline(i); rparse(state, i + 1, stack)
350 355
 
351 356
         case '}' => stack match {
@@ -365,6 +370,7 @@ private[json] trait Parser {
365 370
       case SEP => (at(i): @switch) match {
366 371
         case ' ' => rparse(state, i + 1, stack)
367 372
         case '\t' => rparse(state, i + 1, stack)
  373
+        case '\r' => rparse(state, i + 1, stack)
368 374
         case '\n' => newline(i); rparse(state, i + 1, stack)
369 375
 
370 376
         case ':' => rparse(DATA, i + 1, stack)
@@ -377,6 +383,7 @@ private[json] trait Parser {
377 383
       case ARREND => (at(i): @switch) match {
378 384
         case ' ' => rparse(state, i + 1, stack)
379 385
         case '\t' => rparse(state, i + 1, stack)
  386
+        case '\r' => rparse(state, i + 1, stack)
380 387
         case '\n' => newline(i); rparse(state, i + 1, stack)
381 388
 
382 389
         case ',' => rparse(DATA, i + 1, stack)
@@ -399,6 +406,7 @@ private[json] trait Parser {
399 406
       case OBJEND => (at(i): @switch) match {
400 407
         case ' ' => rparse(state, i + 1, stack)
401 408
         case '\t' => rparse(state, i + 1, stack)
  409
+        case '\r' => rparse(state, i + 1, stack)
402 410
         case '\n' => newline(i); rparse(state, i + 1, stack)
403 411
 
404 412
         case ',' => rparse(KEY, i + 1, stack)
9  json/src/test/scala/blueeyes/json/JsonParserSpec.scala
@@ -275,4 +275,13 @@ xyz
275 275
     confirm(es(2), 9, 22)
276 276
     confirm(es(3), 12, 1)
277 277
   }
  278
+
  279
+  "Handles whitespace correctly" in {
  280
+    def ja(ns: Int*) = JArray(ns.map(n => JNum(n)):_*)
  281
+
  282
+    JParser.parseFromString("[1, 2,\t3,\n4,\r5]").toOption must_== Some(ja(1, 2, 3, 4, 5))
  283
+    JParser.parseManyFromString("[1,\r\n2]\r\n[3,\r\n4]").toOption must_== Some(Seq(ja(1, 2), ja(3, 4)))
  284
+    JParser.parseFromString("[1, 2,\t3,\n4,\0 5]").toOption must_== None
  285
+    JParser.parseManyFromString("[1,\r\n2]\0[3,\r\n4]").toOption must_== None
  286
+  }
278 287
 }

0 notes on commit d5ff3c8

Please sign in to comment.
Something went wrong with that request. Please try again.