1# Comments 2 3This library does not support comments *by default*. It does so for three reasons: 4 51. Comments are not part of the [JSON specification](https://tools.ietf.org/html/rfc8259). You may argue that `//` or `/* */` are allowed in JavaScript, but JSON is not JavaScript. 62. This was not an oversight: Douglas Crockford [wrote on this](https://plus.google.com/118095276221607585885/posts/RK8qyGVaGSr) in May 2012: 7 8 > I removed comments from JSON because I saw people were using them to hold parsing directives, a practice which would have destroyed interoperability. I know that the lack of comments makes some people sad, but it shouldn't. 9 10 > Suppose you are using JSON to keep configuration files, which you would like to annotate. Go ahead and insert all the comments you like. Then pipe it through JSMin before handing it to your JSON parser. 11 123. It is dangerous for interoperability if some libraries would add comment support while others don't. Please check [The Harmful Consequences of the Robustness Principle](https://tools.ietf.org/html/draft-iab-protocol-maintenance-01) on this. 13 14However, you can pass set parameter `ignore_comments` to `#!c true` in the parse function to ignore `//` or `/* */` comments. Comments will then be treated as whitespace. 15 16!!! example 17 18 Consider the following JSON with comments. 19 20 ```json 21 { 22 // update in 2006: removed Pluto 23 "planets": ["Mercury", "Venus", "Earth", "Mars", 24 "Jupiter", "Uranus", "Neptune" /*, "Pluto" */] 25 } 26 ``` 27 28 When calling `parse` without additional argument, a parse error exception is thrown. If `skip_comments` is set to `#! true`, the comments are skipped during parsing: 29 30 ```cpp 31 #include <iostream> 32 #include "json.hpp" 33 34 using json = nlohmann::json; 35 36 int main() 37 { 38 std::string s = R"( 39 { 40 // update in 2006: removed Pluto 41 "planets": ["Mercury", "Venus", "Earth", "Mars", 42 "Jupiter", "Uranus", "Neptune" /*, "Pluto" */] 43 } 44 )"; 45 46 try 47 { 48 json j = json::parse(s); 49 } 50 catch (json::exception &e) 51 { 52 std::cout << e.what() << std::endl; 53 } 54 55 json j = json::parse(s, 56 /* callback */ nullptr, 57 /* allow exceptions */ true, 58 /* skip_comments */ true); 59 std::cout << j.dump(2) << '\n'; 60 } 61 ``` 62 63 Output: 64 65 ``` 66 [json.exception.parse_error.101] parse error at line 3, column 9: 67 syntax error while parsing object key - invalid literal; 68 last read: '<U+000A> {<U+000A> /'; expected string literal 69 ``` 70 71 ```json 72 { 73 "planets": [ 74 "Mercury", 75 "Venus", 76 "Earth", 77 "Mars", 78 "Jupiter", 79 "Uranus", 80 "Neptune" 81 ] 82 } 83 ```