I am looking for a way to process JSON, which includes JS comments. I know, comments a not legal for JSON, but unfortunately I have requirements to read/write JSON files with comments.
I have found a way to write comments using Jackson. This Code
JsonGenerator gen = factory.createGenerator(System.out);
gen.writeStartObject();
gen.writeStringField("a", "b");
gen.writeRaw("\n/*------------*/\n");
gen.writeStringField("c", "d");
gen.writeEndObject();
gen.close();
generates following JSON:
{"a":"b"
/*------------*/
,"c":"d"}
If I start parsing this JSON
factory.enable(JsonParser.Feature.ALLOW_COMMENTS);
JsonParser parser = factory.createParser("{\"a\":\"b\"/*------------*/,\"c\":\"d\"}");
while (parser.nextToken() != JsonToken.END_OBJECT) {
System.out.println(parser.currentToken() + ":" + parser.getText());
}
comments and all the formatting are just skipped. There is not even a JsonToken of type like "RAW" or "COMMENT".
Is there a way to parse JSON with embedded raw data using Jackson (or other Java library)?