Processing shell-style comments in JSON Processing shell-style comments in JSON json json

Processing shell-style comments in JSON


EDIT (May 27, 2021):

What I chose to do is write a custom JSON parser for this nonstandard version of JSON. It supports the shell comments given in your question, but only before the first key of a JSON object:

(The preceding is in C#; a Java version is expected to be available as well. It relies on my Concise Binary Object Representation library, called PeterO.Cbor in NuGet or com.upokecenter/cbor in the Central Repository.)

Indeed, one of your requirements is that "the JSON structure can't be modified by adding propertiesfor the comments instead." That means the comments must be associated to the JSON objectsin some other way. Fortunately, a specification called JSON Pointer was recently publishedas RFC 6901. JSON Pointer is a string that refers to a JSON object within another JSON object. This is why the parser includes a way to get the comment and its associated JSON pointer via a method called JSONWithComments.FromJSONStringWithPointers. JSONPointer.cs is my own implementation of the JSON Pointer specification.

Example of use:

      dict=new Dictionary<string, string>();      str="{\"f\":[\n {\n # B\t \tA C\n # Dm\n\"a\":1,\n\"b\":2\n},{\n #" +"\u0020Sm\n\"a\":3,\n\"b\":4\n}\n]}";       obj = JSONWithComments.FromJSONString(str);      Console.WriteLine(obj);       obj = JSONWithComments.FromJSONStringWithPointers(str, dict);       // Get the comment and its associated JSON pointer       foreach(string key in dict.Keys) {         Console.WriteLine(key);         Console.WriteLine(dict[key]);         // Get the pointed-to object         Console.WriteLine(JSONPointer.GetObject(obj,dict[key]));       }       // Output the object      Console.WriteLine(obj);


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

Using comments in messages to pass data between systems doesn't seem a good practice. . E.g. XML wouldn't support that.

Why not simply incorporate the important "comments" as data? That's what it is if the other system is using it. :^)