diff --git a/doc/examples/output.c b/doc/examples/output.c new file mode 100644 index 0000000..5423544 --- /dev/null +++ b/doc/examples/output.c @@ -0,0 +1,19 @@ +#include +#include + +int main(){ + //create an initial value + RJP_value* root = rjp_new_object(); + + //add a member and set its type + RJP_value* member = rjp_new_member_key_copy(root, "key", 0); + rjp_set_int(member, 5); + + //output into rjp_alloc'd buffer + char* output = rjp_to_json(root, RJP_FORMAT_NONE); + printf("%s\n", output); + + //free memory + rjp_free(output); + rjp_free_value(root); +} diff --git a/doc/examples/parse.c b/doc/examples/parse.c new file mode 100644 index 0000000..4a11873 --- /dev/null +++ b/doc/examples/parse.c @@ -0,0 +1,35 @@ +#include +#include + +int main(int argc, char** argv){ + if(argc != 2){ + fprintf(stderr, "Requires exactly 1 argument\n"); + return 1; + } + + //Read in json argument allowing all RJP extensions (comments, trailing comma) + RJP_value* root = rjp_parse(argv[1], RJP_PARSE_ALL_EXT); + + //returns NULL on error + if(!root){ + fprintf(stderr, "Invalid JSON\n"); + return 1; + } + printf("Valid JSON\n"); + + //check value's type + if(rjp_value_type(root) == rjp_json_object){ + + //Initialize an object iterator for the root value + RJP_object_iterator it; + rjp_init_object_iterator(&it, root); + + //iterate over all members of root, printing their keys + for(RJP_value* curr = rjp_object_iterator_current(&it);curr;curr = rjp_object_iterator_next(&it)){ + printf("Have member with key \"%s\"\n", rjp_member_key(curr)->value); + } + } + + //clean memory + rjp_free_value(root); +}