Code Examples 🔗

Example programs for the scanner and parser are available in the examples directory of the Judo source distribution. The two examples provided are scanner.c and parser.c which demonstrate how to use the scanner and parser, respectively. The most noteworthy parts of their source code are explored in the following subsections.

The example programs are automatically built as part of Judo’s compilation process unless explicitly excluded at configuration time.

Scanner Example 🔗

The first example program uses the scanner API to print each token in the JSON source text on its own line.

The program begins by reading JSON source text from stdin using the judo_readstdin function. Note that the judo_readstdin function is not part of the Judo API. It is an internal function used by the examples and command-line interface to read from stdin in a portable way. Its source code is available here.

size_t json_len = 0;
const char *json = judo_readstdin(&json_len);

The program finds tokens incrementally using the judo_scan function. This function scans the JSON source text for the next token and returns it on demand. Details about the token can be accessed on the judo_stream structure, which is also responsible for maintaining the scanner’s state.

The judo_scan function is repeatedly called until either an error occurs or until all tokens have been read. The latter is determined when judo_scan returns JUDO_TOKEN_EOF.

struct judo_stream stream = {0};
enum judo_result result;
for (;;)
{
    result = judo_scan(&stream, json, json_len);
    if (result == JUDO_RESULT_SUCCESS)
    {
        if (stream.token == JUDO_TOKEN_EOF)
        {
            break;
        }
        process_token(stream, json);
    }
    else
    {
        fprintf(stderr, "error: %s\n", stream.error);
        return 1;
    }
}

Printing Tokens 🔗

The example program prints each token to stdout. If an error occurs, the tokens printed before the error occurred will have already been printed. This is important to note because, in your own application, you may encounter an error after processing a few tokens, which may require halting or rolling back previous work performed. If this is a concern, then first iterate through all tokens to check for errors (i.e. a pre-pass), then once you know that no errors are present you can iterate them again for processing.

switch (stream.token)
{
case JUDO_TOKEN_NULL: puts("null"); break;
case JUDO_TOKEN_TRUE: puts("true"); break;
case JUDO_TOKEN_FALSE: puts("false"); break;
case JUDO_TOKEN_ARRAY_BEGIN: puts("[push]"); break;
case JUDO_TOKEN_ARRAY_END: puts("[pop]"); break;
case JUDO_TOKEN_OBJECT_BEGIN: puts("{push}"); break;
case JUDO_TOKEN_OBJECT_END: puts("{pop}"); break;
case JUDO_TOKEN_NUMBER:
    printf("number: %.*s\n", stream.where.length, &json[stream.where.offset]);
    break;
case JUDO_TOKEN_STRING:
    printf("string: %.*s\n", stream.where.length, &json[stream.where.offset]);
    break;
case JUDO_TOKEN_OBJECT_NAME:
    printf("{name: %.*s}\n", stream.where.length, &json[stream.where.offset]);
    break;
default:
    break;
}

Parser Example 🔗

The second example program uses the parser API to print the JSON structure to stdout in a compact form.

The program begins by reading JSON source text from stdin. Like the scanner example, it uses the judo_readstdin function to read from stdin in a portable way.

size_t json_len = 0;
const char *json = judo_readstdin(&json_len);

The judo_parse function parses the JSON source text into an in-memory tree structure. Each node of the tree represents a JSON value with the root being the top-level JSON value (often an object or array). Once you’re finished processing the tree, you must free it by passing the root value to the judo_free function.

struct judo_error error = {0};
struct judo_value *root;
enum judo_result result = judo_parse(json, json_len, &root, &error, NULL, memfunc);
if (result == JUDO_RESULT_SUCCESS)
{
    print_tree(json, root);
    judo_free(root, NULL, memfunc);
}
else
{
    fprintf(stderr, "error: %s\n", error.description);
    return 1;
}

Custom Memory Allocator 🔗

Both judo_parse and judo_free require you to provide your own dynamic memory allocator implementation. In Judo, this is a single function that behaves like malloc or free depending on its arguments.

The parser example implements a memory allocator using the C standard memory management routines.

void *memfunc(void *user_data, void *ptr, size_t size)
{
    if (ptr == NULL)
    {
        return malloc(size);
    }
    else
    {
        free(ptr);
        return NULL;
    }
}

Now that the tree is in memory, it’s time to traverse it. In this example, the traversal is handled by the print_tree function. Traversal is implemented recursively.

Printing Basic Values 🔗

The way a JSON value is processed depends on its type (null, boolean, number, string, array, or object). In this example, primitive types are processed by printing their lexemes.

switch (judo_gettype(value))
{
case JUDO_TYPE_NULL:
case JUDO_TYPE_BOOL:
case JUDO_TYPE_NUMBER:
case JUDO_TYPE_STRING:
    span = judo_value2span(value);
    printf("%.*s", span.length, &source[span.offset]);
    break;
// [cont...]

Printing Arrays 🔗

Arrays are processed with the judo_first and judo_next functions, which iterate over each element of the array. In this example, the program passes each element of the array to the print_tree function for recursive printing.

case JUDO_TYPE_ARRAY:
    putchar('[');
    elem = judo_first(value);
    while (elem != NULL)
    {
        print_tree(source, elem);
        if (judo_next(elem) != NULL)
        {
            putchar(',');
        }
        elem = judo_next(elem);
    }
    putchar(']');
    break;
// [cont...]

Printing Objects 🔗

Objects are processed with the judo_membfirst and judo_membnext functions, which iterate over each member of the object.

Each member (or “property” in JavaScript terminology) consists of two parts: the name and the value. The name, or “key” in JavaScript, is the string used to look up the member’s value. The value is a JSON value, represented as a judo_value, which, in this example, is passed to print_tree for recursive printing.

case JUDO_TYPE_OBJECT:
    putchar('{');
    member = judo_membfirst(value);
    while (member != NULL)
    {
        span = judo_name2span(member);
        printf("%.*s:", span.length, &source[span.offset]);
        print_tree(source, judo_membvalue(member));
        if (judo_membnext(member) != NULL)
        {
            putchar(',');
        }
        member = judo_membnext(member);
    }
    putchar('}');
    break;