Can comments be used in JSON?

后端 未结 30 1509
别跟我提以往
别跟我提以往 2020-11-22 02:16

Can I use comments inside a JSON file? If so, how?

相关标签:
30条回答
  • 2020-11-22 02:41

    Disclaimer: This is silly

    There is actually a way to add comments, and stay within the specification (no additional parser needed). It will not result into human-readable comments without any sort of parsing though.

    You could abuse the following:

    Insignificant whitespace is allowed before or after any token. Whitespace is any sequence of one or more of the following code points: character tabulation (U+0009), line feed (U+000A), carriage return (U+000D), and space (U+0020).

    In a hacky way, you can abuse this to add a comment. For instance: start and end your comment with a tab. Encode the comment in base3 and use the other whitespace characters to represent them. For instance.

    010212 010202 011000 011000 011010 001012 010122 010121 011021 010202 001012 011022 010212 011020 010202 010202
    

    (hello base three in ASCII) But instead of 0 use space, for 1 use line feed and for 2 use carriage return.

    This will just leave you with a lot of unreadable whitespace (unless you make an IDE plugin to encode/decode it on the fly).

    I never even tried this, for obvious reasons and neither should you.

    0 讨论(0)
  • 2020-11-22 02:42

    We are using strip-json-comments for our project. It supports something like:

    /*
     * Description 
    */
    {
        // rainbows
        "unicorn": /* ❤ */ "cake"
    }
    

    Simply npm install --save strip-json-comments to install and use it like:

    var strip_json_comments = require('strip-json-comments')
    var json = '{/*rainbows*/"unicorn":"cake"}';
    JSON.parse(strip_json_comments(json));
    //=> {unicorn: 'cake'}
    
    0 讨论(0)
  • 2020-11-22 02:43

    If you use JSON5 you can include comments.


    JSON5 is a proposed extension to JSON that aims to make it easier for humans to write and maintain by hand. It does this by adding some minimal syntax features directly from ECMAScript 5.

    0 讨论(0)
  • 2020-11-22 02:45

    No, comments of the form //… or /*…*/ are not allowed in JSON. This answer is based on:

    • https://www.json.org
    • RFC 4627: The application/json Media Type for JavaScript Object Notation (JSON)
    • RFC 8259 The JavaScript Object Notation (JSON) Data Interchange Format (supercedes RFCs 4627, 7158, 7159)
    0 讨论(0)
  • 2020-11-22 02:45

    JSON does not support comments. It was also never intended to be used for configuration files where comments would be needed.

    Hjson is a configuration file format for humans. Relaxed syntax, fewer mistakes, more comments.

    See hjson.github.io for JavaScript, Java, Python, PHP, Rust, Go, Ruby, C++ and C# libraries.

    0 讨论(0)
  • 2020-11-22 02:45

    This is a "can you" question. And here is a "yes" answer.

    No, you shouldn't use duplicative object members to stuff side channel data into a JSON encoding. (See "The names within an object SHOULD be unique" in the RFC).

    And yes, you could insert comments around the JSON, which you could parse out.

    But if you want a way of inserting and extracting arbitrary side-channel data to a valid JSON, here is an answer. We take advantage of the non-unique representation of data in a JSON encoding. This is allowed* in section two of the RFC under "whitespace is allowed before or after any of the six structural characters".

    *The RFC only states "whitespace is allowed before or after any of the six structural characters", not explicitly mentioning strings, numbers, "false", "true", and "null". This omission is ignored in ALL implementations.


    First, canonicalize your JSON by minifying it:

    $jsonMin = json_encode(json_decode($json));
    

    Then encode your comment in binary:

    $hex = unpack('H*', $comment);
    $commentBinary = base_convert($hex[1], 16, 2);
    

    Then steg your binary:

    $steg = str_replace('0', ' ', $commentBinary);
    $steg = str_replace('1', "\t", $steg);
    

    Here is your output:

    $jsonWithComment = $steg . $jsonMin;
    
    0 讨论(0)
提交回复
热议问题