mirror of
https://github.com/json-c/json-c.git
synced 2026-04-02 03:49:06 +08:00
Merge pull request #772 from cosmo-ray/color
add JSON_C_TO_STRING_COLOR option
This commit is contained in:
@@ -38,6 +38,7 @@
|
|||||||
static int formatted_output = JSON_C_TO_STRING_SPACED;
|
static int formatted_output = JSON_C_TO_STRING_SPACED;
|
||||||
static int show_output = 1;
|
static int show_output = 1;
|
||||||
static int strict_mode = 0;
|
static int strict_mode = 0;
|
||||||
|
static int color = 0;
|
||||||
static const char *fname = NULL;
|
static const char *fname = NULL;
|
||||||
|
|
||||||
#ifndef HAVE_JSON_TOKENER_GET_PARSE_END
|
#ifndef HAVE_JSON_TOKENER_GET_PARSE_END
|
||||||
@@ -141,7 +142,7 @@ static int showobj(struct json_object *new_obj)
|
|||||||
if (show_output)
|
if (show_output)
|
||||||
{
|
{
|
||||||
const char *output;
|
const char *output;
|
||||||
output = json_object_to_json_string_ext(new_obj, formatted_output);
|
output = json_object_to_json_string_ext(new_obj, formatted_output | color);
|
||||||
printf("%s\n", output);
|
printf("%s\n", output);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -160,6 +161,7 @@ static void usage(const char *argv0, int exitval, const char *errmsg)
|
|||||||
fprintf(fp, " -f - Format the output to stdout with JSON_C_TO_STRING_PRETTY (default is JSON_C_TO_STRING_SPACED)\n");
|
fprintf(fp, " -f - Format the output to stdout with JSON_C_TO_STRING_PRETTY (default is JSON_C_TO_STRING_SPACED)\n");
|
||||||
fprintf(fp, " -F - Format the output to stdout with <arg>, e.g. 0 for JSON_C_TO_STRING_PLAIN\n");
|
fprintf(fp, " -F - Format the output to stdout with <arg>, e.g. 0 for JSON_C_TO_STRING_PLAIN\n");
|
||||||
fprintf(fp, " -n - No output\n");
|
fprintf(fp, " -n - No output\n");
|
||||||
|
fprintf(fp, " -c - color\n");
|
||||||
fprintf(fp, " -s - Parse in strict mode, flags:\n");
|
fprintf(fp, " -s - Parse in strict mode, flags:\n");
|
||||||
fprintf(fp, " JSON_TOKENER_STRICT|JSON_TOKENER_ALLOW_TRAILING_CHARS\n");
|
fprintf(fp, " JSON_TOKENER_STRICT|JSON_TOKENER_ALLOW_TRAILING_CHARS\n");
|
||||||
fprintf(fp, " Diagnostic information will be emitted to stderr\n");
|
fprintf(fp, " Diagnostic information will be emitted to stderr\n");
|
||||||
@@ -173,7 +175,7 @@ int main(int argc, char **argv)
|
|||||||
{
|
{
|
||||||
int opt;
|
int opt;
|
||||||
|
|
||||||
while ((opt = getopt(argc, argv, "fF:hns")) != -1)
|
while ((opt = getopt(argc, argv, "fF:hnsc")) != -1)
|
||||||
{
|
{
|
||||||
switch (opt)
|
switch (opt)
|
||||||
{
|
{
|
||||||
@@ -181,6 +183,7 @@ int main(int argc, char **argv)
|
|||||||
case 'F': formatted_output = atoi(optarg); break;
|
case 'F': formatted_output = atoi(optarg); break;
|
||||||
case 'n': show_output = 0; break;
|
case 'n': show_output = 0; break;
|
||||||
case 's': strict_mode = 1; break;
|
case 's': strict_mode = 1; break;
|
||||||
|
case 'c': color = JSON_C_TO_STRING_COLOR; break;
|
||||||
case 'h': usage(argv[0], 0, NULL);
|
case 'h': usage(argv[0], 0, NULL);
|
||||||
default: /* '?' */ usage(argv[0], EXIT_FAILURE, "Unknown arguments");
|
default: /* '?' */ usage(argv[0], EXIT_FAILURE, "Unknown arguments");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,6 +64,12 @@ static void json_object_generic_delete(struct json_object *jso);
|
|||||||
#define inline
|
#define inline
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* define colors */
|
||||||
|
#define ANSI_COLOR_RESET "\033[0m"
|
||||||
|
#define ANSI_COLOR_FG_GREEN "\033[0;32m"
|
||||||
|
#define ANSI_COLOR_FG_BLUE "\033[0;34m"
|
||||||
|
#define ANSI_COLOR_FG_MAGENTA "\033[0;35m"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Helper functions to more safely cast to a particular type of json_object
|
* Helper functions to more safely cast to a particular type of json_object
|
||||||
*/
|
*/
|
||||||
@@ -472,15 +478,28 @@ static int json_object_object_to_json_string(struct json_object *jso, struct pri
|
|||||||
if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
|
if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
|
||||||
printbuf_strappend(pb, " ");
|
printbuf_strappend(pb, " ");
|
||||||
indent(pb, level + 1, flags);
|
indent(pb, level + 1, flags);
|
||||||
|
if (flags & JSON_C_TO_STRING_COLOR)
|
||||||
|
printbuf_strappend(pb, ANSI_COLOR_FG_BLUE);
|
||||||
|
|
||||||
printbuf_strappend(pb, "\"");
|
printbuf_strappend(pb, "\"");
|
||||||
json_escape_str(pb, iter.key, strlen(iter.key), flags);
|
json_escape_str(pb, iter.key, strlen(iter.key), flags);
|
||||||
|
printbuf_strappend(pb, "\"");
|
||||||
|
|
||||||
|
if (flags & JSON_C_TO_STRING_COLOR)
|
||||||
|
printbuf_strappend(pb, ANSI_COLOR_RESET);
|
||||||
|
|
||||||
if (flags & JSON_C_TO_STRING_SPACED)
|
if (flags & JSON_C_TO_STRING_SPACED)
|
||||||
printbuf_strappend(pb, "\": ");
|
printbuf_strappend(pb, ": ");
|
||||||
else
|
else
|
||||||
printbuf_strappend(pb, "\":");
|
printbuf_strappend(pb, ":");
|
||||||
if (iter.val == NULL)
|
|
||||||
|
if (iter.val == NULL) {
|
||||||
|
if (flags & JSON_C_TO_STRING_COLOR)
|
||||||
|
printbuf_strappend(pb, ANSI_COLOR_FG_MAGENTA);
|
||||||
printbuf_strappend(pb, "null");
|
printbuf_strappend(pb, "null");
|
||||||
else if (iter.val->_to_json_string(iter.val, pb, level + 1, flags) < 0)
|
if (flags & JSON_C_TO_STRING_COLOR)
|
||||||
|
printbuf_strappend(pb, ANSI_COLOR_RESET);
|
||||||
|
} else if (iter.val->_to_json_string(iter.val, pb, level + 1, flags) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if ((flags & JSON_C_TO_STRING_PRETTY) && had_children)
|
if ((flags & JSON_C_TO_STRING_PRETTY) && had_children)
|
||||||
@@ -626,9 +645,18 @@ void json_object_object_del(struct json_object *jso, const char *key)
|
|||||||
static int json_object_boolean_to_json_string(struct json_object *jso, struct printbuf *pb,
|
static int json_object_boolean_to_json_string(struct json_object *jso, struct printbuf *pb,
|
||||||
int level, int flags)
|
int level, int flags)
|
||||||
{
|
{
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
if (flags & JSON_C_TO_STRING_COLOR)
|
||||||
|
printbuf_strappend(pb, ANSI_COLOR_FG_MAGENTA);
|
||||||
|
|
||||||
if (JC_BOOL(jso)->c_boolean)
|
if (JC_BOOL(jso)->c_boolean)
|
||||||
return printbuf_strappend(pb, "true");
|
ret = printbuf_strappend(pb, "true");
|
||||||
return printbuf_strappend(pb, "false");
|
else
|
||||||
|
ret = printbuf_strappend(pb, "false");
|
||||||
|
if (ret > -1 && flags & JSON_C_TO_STRING_COLOR)
|
||||||
|
return printbuf_strappend(pb, ANSI_COLOR_RESET);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct json_object *json_object_new_boolean(json_bool b)
|
struct json_object *json_object_new_boolean(json_bool b)
|
||||||
@@ -1217,9 +1245,13 @@ static int json_object_string_to_json_string(struct json_object *jso, struct pri
|
|||||||
int level, int flags)
|
int level, int flags)
|
||||||
{
|
{
|
||||||
ssize_t len = JC_STRING(jso)->len;
|
ssize_t len = JC_STRING(jso)->len;
|
||||||
|
if (flags & JSON_C_TO_STRING_COLOR)
|
||||||
|
printbuf_strappend(pb, ANSI_COLOR_FG_GREEN);
|
||||||
printbuf_strappend(pb, "\"");
|
printbuf_strappend(pb, "\"");
|
||||||
json_escape_str(pb, get_string_component(jso), len < 0 ? -(ssize_t)len : len, flags);
|
json_escape_str(pb, get_string_component(jso), len < 0 ? -(ssize_t)len : len, flags);
|
||||||
printbuf_strappend(pb, "\"");
|
printbuf_strappend(pb, "\"");
|
||||||
|
if (flags & JSON_C_TO_STRING_COLOR)
|
||||||
|
printbuf_strappend(pb, ANSI_COLOR_RESET);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1392,9 +1424,15 @@ static int json_object_array_to_json_string(struct json_object *jso, struct prin
|
|||||||
printbuf_strappend(pb, " ");
|
printbuf_strappend(pb, " ");
|
||||||
indent(pb, level + 1, flags);
|
indent(pb, level + 1, flags);
|
||||||
val = json_object_array_get_idx(jso, ii);
|
val = json_object_array_get_idx(jso, ii);
|
||||||
if (val == NULL)
|
if (val == NULL) {
|
||||||
|
|
||||||
|
if (flags & JSON_C_TO_STRING_COLOR)
|
||||||
|
printbuf_strappend(pb, ANSI_COLOR_FG_MAGENTA);
|
||||||
printbuf_strappend(pb, "null");
|
printbuf_strappend(pb, "null");
|
||||||
else if (val->_to_json_string(val, pb, level + 1, flags) < 0)
|
if (flags & JSON_C_TO_STRING_COLOR)
|
||||||
|
printbuf_strappend(pb, ANSI_COLOR_RESET);
|
||||||
|
|
||||||
|
} else if (val->_to_json_string(val, pb, level + 1, flags) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if ((flags & JSON_C_TO_STRING_PRETTY) && had_children)
|
if ((flags & JSON_C_TO_STRING_PRETTY) && had_children)
|
||||||
|
|||||||
@@ -74,6 +74,15 @@ extern "C" {
|
|||||||
*/
|
*/
|
||||||
#define JSON_C_TO_STRING_NOSLASHESCAPE (1 << 4)
|
#define JSON_C_TO_STRING_NOSLASHESCAPE (1 << 4)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A flag for the json_object_to_json_string_ext() and
|
||||||
|
* json_object_to_file_ext() functions which causes
|
||||||
|
* the output to be formatted.
|
||||||
|
*
|
||||||
|
* Use color for printing json.
|
||||||
|
*/
|
||||||
|
#define JSON_C_TO_STRING_COLOR (1 << 5)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A flag for the json_object_object_add_ex function which
|
* A flag for the json_object_object_add_ex function which
|
||||||
* causes the value to be added without a check if it already exists.
|
* causes the value to be added without a check if it already exists.
|
||||||
|
|||||||
Reference in New Issue
Block a user