I'm using this library to load and parse a configuration file for the program I'm working on. I like the JSON format and cJSON's API, but I wanted the config file to be able to have comments.
The following patch breaks the JSON standard by adding comment support.
# starts a comment and extends to the end of the line.
This is only for loading. It won't print comments since it skips them during parsing.
--- cJSON.c.bak 2015-01-04 16:58:25.250388258 -0500+++ cJSON.c 2015-01-04 17:14:46.601329765 -0500@@ -258,7 +258,21 @@static char *print_object(cJSON *item,int depth,int fmt);
/* Utility to jump whitespace and cr/lf */
-static const char *skip(const char *in) {while (in && *in && (unsigned char)*in<=32) in++; return in;}+static const char *skip(const char *in) {+ int c = 0;++ while (in && *in) {+ if (*in == '#') c = 1;+ if (*in == '\n') c = 0;++ if (!c && ((unsigned char)*in > 32))+ break;++ in++;+ }++ return in;+}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I'm using this library to load and parse a configuration file for the program I'm working on. I like the JSON format and cJSON's API, but I wanted the config file to be able to have comments.
The following patch breaks the JSON standard by adding comment support.
# starts a comment and extends to the end of the line.
This is only for loading. It won't print comments since it skips them during parsing.