C Collections

cjson · Section 3

cjson_type_str

cjson_type_str - name a DOM node's type as a printable string

C Collections (libccollections, -lccollections)

#include <ccollections/cjson.h>
 
static inline const char *cjson_type_str(cjson node);

Returns the name of node's type tag as a string, for diagnostics
such as a message saying that a value did not have the type the
caller expected.  It only produces the text, so use cjson_type(3) to
read the tag itself.
 
The returned text is the enumerator's own name, one of:
 
    CJSON_NULL         JSON null literal
    CJSON_BOOL         JSON true or false
    CJSON_INTEGER      JSON number with no decimal point or exponent
    CJSON_FLOAT        JSON number with a decimal point or an exponent
    CJSON_STRING       JSON string
    CJSON_LIST         JSON array
    CJSON_DICTIONARY   JSON object
 
A NULL node gives "CJSON_NULL", since cjson_type(3) reports
CJSON_NULL for it.  A tag that is not in cjson_node_type gives
"CJSON_UNKNOWN", which a valid node never carries.

node  The node to describe, which may be NULL.

A pointer to a string literal that names the node's type, never
NULL.

The result has static storage duration and lives as long as the
program, so the caller can keep it and print it later.  The caller
must not change it or free it.
 
The text alone doesn't tell a NULL node from a real JSON null node,
so compare the handle with NULL first when that difference matters.

cjson doc = cjson_parse("{\"k\":1}", NULL);
cjson k = cjson_get(doc, "k");
 
if (cjson_type(k) != CJSON_STRING) {
    fprintf(stderr, "k is %s, expected CJSON_STRING\n",
            cjson_type_str(k));
}
 
cjson_destroy(doc);

cjson_type(3), cjson_bool_val(3), cjson_int_val(3), cjson_double_val(3), cjson_str_val(3)