Subject: | [PATCH] Tolerate literal ASCII TABs in strings in relaxed mode |
[PATCH] Tolerate literal ASCII TABs in strings in relaxed mode
Just encountered a JSON object that was messed up and there doesn't seem to be
a better way to fix it.
Subject: | 0001-Tolerate-literal-ASCII-TABs-in-strings-in-relaxed-mo.patch |
From a6cf4c308da48bc11524c7517958af39654171da Mon Sep 17 00:00:00 2001
From: Lubomir Rintel <lkundrak@v3.sk>
Date: Mon, 28 Jul 2014 14:25:08 +0200
Subject: [PATCH] Tolerate literal ASCII TABs in strings in relaxed mode
Just encountered a JSON object that was messed up and there doesn't seem to be
a better way to fix it.
---
XS.pm | 10 ++++++++++
XS.xs | 4 +++-
t/17_relaxed.t | 4 +++-
3 files changed, 16 insertions(+), 2 deletions(-)
diff --git a/XS.pm b/XS.pm
index d09d9e3..db4893f 100644
--- a/XS.pm
+++ b/XS.pm
@@ -406,6 +406,16 @@ character, after which more white-space and comments are allowed.
# neither this one...
]
+=item * literal ASCII TAB character
+
+Allow literal ASCII TAB in strings despite JSON mandates, that TAB
+character is substituted for "\t" sequence.
+
+ [
+ "Hello\tWorld",
+ "Hello<TAB>World" <- literal <TAB> would not normally be allowed
+ ]
+
=back
=item $json = $json->canonical ([$enable])
diff --git a/XS.xs b/XS.xs
index 05d0c4a..e74aa65 100644
--- a/XS.xs
+++ b/XS.xs
@@ -1106,7 +1106,9 @@ decode_str (dec_t *dec)
utf8 = 1;
}
- else
+ else if (dec->json.flags & F_RELAXED && ch == '\t') {
+ *cur++ = ch;
+ } else
{
--dec_cur;
diff --git a/t/17_relaxed.t b/t/17_relaxed.t
index 9dbcfb7..fc42465 100644
--- a/t/17_relaxed.t
+++ b/t/17_relaxed.t
@@ -1,4 +1,4 @@
-BEGIN { $| = 1; print "1..8\n"; }
+BEGIN { $| = 1; print "1..9\n"; }
use utf8;
use JSON::XS;
@@ -20,3 +20,5 @@ ok ('{"1":2}' eq encode_json $json->decode ('{"1":2,}'));
ok (!eval { $json->decode ('{,}') });
ok ('[1,2]' eq encode_json $json->decode ("[1#,2\n ,2,# ] \n\t]"));
+
+ok ('["Hello\tWorld"]' eq encode_json $json->decode ("[\"Hello\tWorld\"]"));
--
1.8.3.1