开发者

JSON parsing error with CouchDB design document: unclosed string due to indentation whitespaces?

开发者 https://www.devze.com 2023-04-07 16:01 出处:网络
I use the following CouchDB design document for testing purposes. I use carriage return \\n and tabs \\t to indent my Javascript code. As far as I understand JSON, I think that whitespaces in those pl

I use the following CouchDB design document for testing purposes. I use carriage return \n and tabs \t to indent my Javascript code. As far as I understand JSON, I think that whitespaces in those places are OK. But Ruby couchrest fails parsing that and JSLint too. I have to strip the \n and \t out of the design document before parsing it. Why is that ?

The JSON introduction page on json.org mentions that "Whitespace can be inserted between any pair of tokens. "

It is a little bit difficult to render tabs here so I have included a parsed string with \n and \t.

{
"_id": "_design/test",
"views": {
    "all": {
        "map": "function(doc) {
            if (doc.nom) emit(doc._id, doc);
            }"
        },
    "asin_doc": {
        "map": "function(doc) {
            if (doc.category) emit(doc.asin, doc);
  开发者_运维知识库          }"
        },
    "asin": {
        "map": "function(doc) {
            if (doc.category) emit(doc.asin, null);
            }"
        }
    }
    }

The same document in a long string (Ruby):

"{\n\t\"_id\": \"_design/test\",\n\t\"views\": {\n\t\t\"all\": {\n\t\t\t\"map\": \"function(doc) {\n\t\t\t\tif (doc.nom) emit(doc._id, doc);\n\t\t\t}\"\n\t\t},\n\t\t\"asin_doc\": {\n\t\t\t\"map\": \"function(doc) {\n\t\t\t\tif (doc.category) emit(doc.asin, doc);\n\t\t\t}\"\n\t\t},\n\t\t\"asin\": {\n\t\t\t\"map\": \"function(doc) {\n\t\t\t\tif (doc.category) emit(doc.asin, null);\n\t\t\t}\"\n\t\t}\n\t}\n}\n"

JSLint tells me that there is a problem at line 5 character 20.

Problem at line 5 character 20: Unclosed string.
"map": "function(doc) {
Problem at line 5 character 20: Stopping. (25% scanned).
JSON: bad.

I can't see where the string is not properly closed. Any idea ?


Newline and tab are not allowed in a string. See the JSON RFC, specifically:

unescaped = %x20-21 / %x23-5B / %x5D-10FFFF

EDIT: What json.org is talking about, I think, is this:

Insignificant whitespace is allowed before or after any of the six structural characters.

The "structural characters" are: [ ] { } : ,

0

精彩评论

暂无评论...
验证码 换一张
取 消