Examples

Creating a simple MJSON document tree

A MJSON document tree is built by creating all the individual MJSON values and then adding them to their parent. To put it in other words, you simply create the tree nodes and insert them into their parent nodes.

Here's a small example, uncluttered by removing all error checking code:

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>

#include "json.h"

int
main (void)
{
	char *text;
	json_t *root, *entry, *label, *value;
	setlocale (LC_ALL, "");

	// creates the root node
	root = json_new_object();

	// create an entry node
	entry = json_new_object();

	// insert the first label-value pair
	label = json_new_string("name");
	value = json_new_string("Andew");
	json_insert_child(label, value);
	json_insert_child(entry, label);

	// insert the second label-value pair
	label = json_new_string("phone");
	value = json_new_string("555 123 456");
	json_insert_child(label, value);
	json_insert_child(entry, label);

	// inserts that object as a value in a label-value pair
	label = json_new_string("entry");
	json_insert_child(label, entry);

	// inserts that label-value pair into the root object
	json_insert_child(root, label);

	// print the result
	json_tree_to_string(root, &text);
	printf("%ls\n",text);

	// clean up
	free(text);
	json_free_value(&root);
	return EXIT_SUCCESS;
}

The MJSON routines themselves perform tree integrity checks in order to make sure that all MJSON document trees are valid. Nonetheless, you must always make sure that you always follow the integrity rules that define the MJSON document tree structure.

Having to create all individual nodes and then having to insert them one by one may be bit troublesome. That isn't exactly what is expected out of an application which uses the MJSON library. The expected usage, which is a good way to save some time and efford, is to write small routines which create custom MJSON document sub-trees that you can simply insert in your document tree.

Here is a small example to give a better idea:

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>

#include "json.h"

json_t *new_entry(char *name, char *phone)
{
	json_t *entry, *label, *value;

	// create an entry node
	entry = json_new_object();
	
	// insert the first label-value pair
	label = json_new_string("name");
	value = json_new_string("Andew");
	json_insert_child(label, value);
	json_insert_child(entry, label);
	
	// insert the second label-value pair
	label = json_new_string("phone");
	value = json_new_string("555 123 456");
	json_insert_child(label, value);
	json_insert_child(entry, label);

	// inserts that object as a value in a label-value pair
	label = json_new_string("entry");
	json_insert_child(label, entry);

	return label;
}

int
main (void)
{
	setlocale (LC_ALL, "");

	json_t *root, *subtree;

	// creates the root node
	root = json_new_object();

	// creates the desired MJSON document subtree
	subtree = new_entry("Andrew", "555 123 456");

	// inserts the subtree into the root object
	json_insert_child(root, subtree);

	// print the result
	char *text;
	json_tree_to_string(root, &text);
	printf("%ls\n",text);

	// clean up
	free(text);
	json_free_value(&root);
	return EXIT_SUCCESS;
}

MJSON also offers a couple of higher-level routines which may simplify this process a bit

Generating a MJSON document tree from a JSON document: part 1

In order to generate a MJSON document tree from a JSON document, basically you need to store the document in a char string and feed that string to one of the available parser functions.

Here is a small example

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>

#include "json.h"

int
main (void)
{
	setlocale (LC_ALL, "");
	char *document = "{\"entry\":{\"name\":\"Andew\",\"phone\":\"555 123 456\"}}";

	json_t *root;

	printf("Parsing the document...\n");
	root = json_parse_document(document);

	printf("Printing the document tree...\n");
	json_tree_to_string(root, &document);
	wprintf("%ls\n", document);

	// clean up
	json_free_value(&root);
	return EXIT_SUCCESS;
}

There. Simple as that. If there happens to be something wrong with the string's markup then the parser function will throw an error and return NULL.

Generating a MJSON document tree from a JSON document: part 2

The function used to parse the document from the previous example is nothing more than a wrapper for a slightly more powerful function which is capable of parsing documents sequentially, by being fed only portions of the whole document. That feature comes in handy if you need to read large JSON documents from files or even through a network connection. The buffer must be a nul-terminated c-string.

To give an example, here is a small piece of code which reads a JSON document from a file into a small buffer and proceeds to parse the buffered portion of the JSON document:

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>

#include "json.h"
#include "json_helper.h"

#define BUFFER_SIZE 1024
int
main (void)
{
	char buffer[BUFFER_SIZE];
	char *temp = NULL;
	unsigned int error = JSON_INCOMPLETE_DOCUMENT;

	struct json_parsing_info state;

	json_jpi_init(&state);

	while  ( (error == JSON_WAITING_FOR_EOF) || (error == JSON_INCOMPLETE_DOCUMENT) )
	{
		if(fgets (buffer, BUFFER_SIZE, stdin) != NULL)
		{
			switch(error = json_parse_string( &state, buffer))
			{
				case JSON_OK:
					printf("complete\n");
					json_tree_to_string(state.cursor, &temp);
					printf("%s\n",temp);
					break;

				case JSON_WAITING_FOR_EOF:
				case JSON_INCOMPLETE_DOCUMENT:
					break;

				default:
					printf("Some error occurred: %d\n", error);
			}
		}
		else
		{
			if(error == JSON_WAITING_FOR_EOF)
				error = JSON_OK;
			else
				error = JSON_UNKNOWN_PROBLEM;
		}
	}
	if(error == JSON_OK)
	{
		json_render_tree(state.cursor);
	}
	else
	{
		printf("Document wasn't valid.\n");
	}
	/* perform cleanup */
	json_free_value(&state.cursor);
	return 0;
}