title: Processing shell-style comments in JSON
body: <p>I'm dealing with systems which manipulate "relaxed" JSON data which includes shell-style <code>#</code> line comments:</p>
  
  <pre><code>[
    {
      # Batman
      "first-name": "Bruce",
      "last-name": "Wayne"
    },
    {
      # Superman
      "first-name": "Clark",
      "last-name": "Kent"
    }
  ]
  </code></pre>
  
  <p>The part of the system I'm working on uses <a href="http://json-lib.sourceforge.net/">json-lib</a> - which I'm surprised to discover is tolerant of the shell-style comments - to parse the JSON input.</p>
  
  <p>I need to extract some additional annotation from those comments, but json-lib seems to just discard them without providing an API for reading them:</p>
  
  <pre><code>JSONObject map = (JSONObject)JSONSerializer.toJSON("{\n"+
                                                     "    # Batman\n" + // note the shell-style # comment
                                                     "    \"first-name\": \"Bruce\",\n" +
                                                     "    \"last-name\": \"Wayne\"\n" +
                                                     "}");
  System.out.println(map.toString());
  /* &lt;&lt;'OUTPUT'
   * {"first-name":"Bruce","last-name":"Wayne"}
   * OUTPUT
   * note the absence of the shell-style comment
   */
  </code></pre>
  
  <p>This makes sense since comments aren't part of the JSON spec and I'm lucky json-lib doesn't just choke when parsing them in the first place.</p>
  
  <p>Of note:</p>
  
  <ul>
  <li>other systems consume this same JSON and the annotations need to be transparent to them, so the JSON structure can't be modified by adding properties for the comments instead.</li>
  <li>not all the components and objects in my system have access to the raw JSON source: one component reads the file and parses it using JSONlib and passes de-serialized maps etc around.</li>
  </ul>
  
  <p>How can I read and parse these comments while processing the JSON input? Is there a library which will allow me to read them and relate them to their position in the JSON - can I easily connect the <code>Batman</code> comment to the "Bruce Wayne" entry?</p>
  
  <p>I'm currently using json-lib, but I'm open to investigating other JSON libraries and equally open to using other languages which extend JSON, such as YAML - but I'm not sure those tools will allow me to read and process the comments in my input.</p>