Skip to content

Instantly share code, notes, and snippets.

@waveacme
Forked from alan-mushi/json_parser.c
Created April 7, 2016 09:40
Show Gist options
  • Save waveacme/d0eba4be3f6d8283c10574c94f651d04 to your computer and use it in GitHub Desktop.
Save waveacme/d0eba4be3f6d8283c10574c94f651d04 to your computer and use it in GitHub Desktop.
Examples for the json-c tutorial.
/*
* A simple example of json string parsing with json-c.
*
* clang -Wall -g -I/usr/include/json-c/ -o json_parser json_parser.c -ljson-c
*/
#include <json.h>
#include <stdio.h>
int main() {
struct json_object *jobj;
char *str = "{ \"msg-type\": [ \"0xdeadbeef\", \"irc log\" ], \
\"msg-from\": { \"class\": \"soldier\", \"name\": \"Wixilav\" }, \
\"msg-to\": { \"class\": \"supreme-commander\", \"name\": \"[Redacted]\" }, \
\"msg-log\": [ \
\"soldier: Boss there is a slight problem with the piece offering to humans\", \
\"supreme-commander: Explain yourself soldier!\", \
\"soldier: Well they don't seem to move anymore...\", \
\"supreme-commander: Oh snap, I came here to see them twerk!\" \
] \
}";
printf("str:\n---\n%s\n---\n\n", str);
jobj = json_tokener_parse(str);
printf("jobj from str:\n---\n%s\n---\n", json_object_to_json_string_ext(jobj, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY));
return 0;
}
/*
* Various outputs for flag of json_object_to_json_string_ext().
*
* clang -Wall -I/usr/include/json-c/ -o json_print json_print.c -ljson-c
*/
#include <json.h>
#include <stdio.h>
int main() {
struct json_object *jobj;
const char *question = "Mum, clouds hide alien spaceships don't they ?";
const char *answer = "Of course not! (\"sigh\")";
int i;
struct {
int flag;
const char *flag_str;
} json_flags[] = {
{ JSON_C_TO_STRING_PLAIN, "JSON_C_TO_STRING_PLAIN" },
{ JSON_C_TO_STRING_SPACED, "JSON_C_TO_STRING_SPACED" },
{ JSON_C_TO_STRING_PRETTY, "JSON_C_TO_STRING_PRETTY" },
{ JSON_C_TO_STRING_NOZERO, "JSON_C_TO_STRING_NOZERO" },
{ JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY, "JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY" },
{ -1, NULL }
}; // Create an anonymous struct, instanciate an array and fill it
printf("Using printf(): \"%s\", \"%s\"\n\n", question, answer);
printf("Using json_object_to_json_string_ext():\n");
/*
* The following create an object and add the question and answer to it.
*/
jobj = json_object_new_object();
json_object_object_add(jobj, "question", json_object_new_string(question));
json_object_object_add(jobj, "answer", json_object_new_string(answer));
for (i = 0; json_flags[i].flag_str != NULL; i++) {
printf("\nFlag %s:\n---\n", json_flags[i].flag_str);
printf("%s\n---\n", json_object_to_json_string_ext(jobj, json_flags[i].flag));
}
json_object_put(jobj); // Delete the json object
return 0;
}
/*
* Reference counting example. The idea is to make a json_object res composed from
* parts of obj1 and obj2.
*
* clang -Wall -g -I/usr/include/json-c/ -o json_refcount json_refcount.c -ljson-c
*/
#include <json.h>
#include <stdio.h>
/*
* Just a utility function.
*/
void print_json_object(struct json_object *jobj, const char *msg) {
printf("\n%s: \n", msg);
printf("---\n%s\n---\n", json_object_to_json_string(jobj));
}
/*
* In real life the operations done here are way more complicated, but it's
* only an example.
*/
struct json_object * find_something(struct json_object *jobj, const char *key) {
struct json_object *tmp;
json_object_object_get_ex(jobj, key, &tmp);
return tmp;
}
/*
* Swap the first and second elements of the array received.
* Again, more complicated in real life...
*/
void glitch_in_the_matrix(struct json_object *jobj) {
struct json_object *pos1, *pos2;
print_json_object(jobj, "Before the glitch in the matrix");
pos1 = json_object_array_get_idx(jobj, 0);
pos2 = json_object_array_get_idx(jobj, 1);
// json_object_array_put_idx decrement the refcount if you replace an
// element, but we don't whant that.
json_object_array_put_idx(jobj, 0, json_object_get(pos2));
json_object_array_put_idx(jobj, 1, json_object_get(pos1));
print_json_object(jobj, "After the glitch in the matrix");
}
int main() {
struct json_object *obj1, *obj2, *res, *sub_obj1, *sub_obj2, *tmp;
// Creation and print of obj1 and obj2
obj1 = json_object_new_object();
obj2 = json_object_new_object();
json_object_object_add(obj1, KEY1, json_object_new_int(1234));
json_object_object_add(obj2, KEY2, json_object_new_int(4321));
print_json_object(obj1, "obj1 in plaintext");
print_json_object(obj2, "obj2 in plaintext");
// Extraction of the interesting parts of obj1 and obj2
sub_obj1 = find_something(obj1, "peace-code");
sub_obj2 = find_something(obj2, "death-ray-code");
res = json_object_new_array();
// We are going to use those in res so we need to increment the
// refcount
json_object_array_add(res, json_object_get(sub_obj1));
json_object_array_add(res, json_object_get(sub_obj2));
glitch_in_the_matrix(res);
tmp = json_object_array_get_idx(res, 0);
printf("\n[>] Unlocking peace with code at index 0 of res: %d\n", json_object_get_int(tmp));
// We can safely put all of these objects
json_object_put(obj1);
json_object_put(obj2);
json_object_put(res);
return 0;
}
/*
* Exploring the types of json-c.
*
* clang -Wall -I/usr/include/json-c/ -o json_types json_types.c -ljson-c
*/
#include <json.h>
#include <stdio.h>
int main() {
struct json_object *array, *object, *tmp, *secu_code;
char *val_type_str, *str;
int val_type, i;
array = json_object_new_array();
object = json_object_new_object();
// Fill the array
tmp = json_object_new_int(12345);
json_object_array_add(array, tmp);
tmp = json_object_new_boolean(TRUE);
json_object_array_put_idx(array, 1, tmp);
// Fill the object
tmp = json_object_new_string("We have been made!");
json_object_object_add(object, "message", tmp);
json_object_object_add(object, "security-code", array);
array = tmp = NULL; // "Nothing in the sleeves"
str = NULL;
// key and val don't exist outside of this bloc
json_object_object_foreach(object, key, val) {
printf("key: \"%s\", type of val: ", key);
val_type = json_object_get_type(val);
switch (val_type) {
case json_type_null:
val_type_str = "val is NULL";
break;
case json_type_boolean:
val_type_str = "val is a boolean";
break;
case json_type_double:
val_type_str = "val is a double";
break;
case json_type_int:
val_type_str = "val is an integer";
break;
case json_type_string:
val_type_str = "val is a string";
str = (char *) json_object_get_string(val);
break;
case json_type_object:
val_type_str = "val is an object";
break;
case json_type_array:
val_type_str = "val is an array";
break;
}
printf("%s", val_type_str);
if (str)
printf("\t->\t\"%s\"", str);
printf("\n");
str = NULL;
}
printf("\nDetails of the security code:\n");
// Get the json_object associated to the "security-code" key in secu_code
json_object_object_get_ex(object, "security-code", &secu_code);
// For each case of the secu_code array
for (i = 0; i < json_object_array_length(secu_code); i++) {
// Set in tmp the json_object of the secu_code array at index i
tmp = json_object_array_get_idx(secu_code, i);
printf("security-code[%d] = %s\n", i, json_object_to_json_string(tmp));
}
printf("\nJson in plain text: \n");
printf("---\n%s\n---\n", json_object_to_json_string(object));
json_object_put(object);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment