Add 2 basic examples
This commit is contained in:
parent
4c94e11ff3
commit
c42ee430c9
19
doc/examples/output.c
Normal file
19
doc/examples/output.c
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#include <rjp.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
35
doc/examples/parse.c
Normal file
35
doc/examples/parse.c
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
#include <rjp.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user