Can comments be used in JSON?

后端 未结 30 1642
别跟我提以往
别跟我提以往 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.

提交回复
热议问题