|
Packit Service |
def718 |
#include <stdio.h>
|
|
Packit Service |
def718 |
#include <stdlib.h>
|
|
Packit Service |
def718 |
#include <stddef.h>
|
|
Packit Service |
def718 |
#include <string.h>
|
|
Packit Service |
def718 |
#include <assert.h>
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
#include "json.h"
|
|
Packit Service |
def718 |
#include "json_tokener.h"
|
|
Packit Service |
def718 |
#include "json_visit.h"
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
static json_c_visit_userfunc emit_object;
|
|
Packit Service |
def718 |
static json_c_visit_userfunc skip_arrays;
|
|
Packit Service |
def718 |
static json_c_visit_userfunc pop_and_stop;
|
|
Packit Service |
def718 |
static json_c_visit_userfunc err_on_subobj2;
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
int main(void)
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
MC_SET_DEBUG(1);
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
const char *input = "{\
|
|
Packit Service |
def718 |
\"obj1\": 123,\
|
|
Packit Service |
def718 |
\"obj2\": {\
|
|
Packit Service |
def718 |
\"subobj1\": \"aaa\",\
|
|
Packit Service |
def718 |
\"subobj2\": \"bbb\",\
|
|
Packit Service |
def718 |
\"subobj3\": [ \"elem1\", \"elem2\", true ],\
|
|
Packit Service |
def718 |
},\
|
|
Packit Service |
def718 |
\"obj3\": 1.234,\
|
|
Packit Service |
def718 |
\"obj4\": [ true, false, null ]\
|
|
Packit Service |
def718 |
}";
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
json_object *jso = json_tokener_parse(input);
|
|
Packit Service |
def718 |
printf("jso.to_string()=%s\n", json_object_to_json_string(jso));
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
int rv;
|
|
Packit Service |
def718 |
rv = json_c_visit(jso, 0, emit_object, NULL);
|
|
Packit Service |
def718 |
printf("json_c_visit(emit_object)=%d\n", rv);
|
|
Packit Service |
def718 |
printf("================================\n\n");
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
rv = json_c_visit(jso, 0, skip_arrays, NULL);
|
|
Packit Service |
def718 |
printf("json_c_visit(skip_arrays)=%d\n", rv);
|
|
Packit Service |
def718 |
printf("================================\n\n");
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
rv = json_c_visit(jso, 0, pop_and_stop, NULL);
|
|
Packit Service |
def718 |
printf("json_c_visit(pop_and_stop)=%d\n", rv);
|
|
Packit Service |
def718 |
printf("================================\n\n");
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
rv = json_c_visit(jso, 0, err_on_subobj2, NULL);
|
|
Packit Service |
def718 |
printf("json_c_visit(err_on_subobj2)=%d\n", rv);
|
|
Packit Service |
def718 |
printf("================================\n\n");
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
json_object_put(jso);
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
static int emit_object(json_object *jso, int flags,
|
|
Packit Service |
def718 |
json_object *parent_jso,
|
|
Packit Service |
def718 |
const char *jso_key,
|
|
Packit Service |
def718 |
size_t *jso_index, void *userarg)
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
printf("flags: 0x%x, key: %s, index: %ld, value: %s\n",
|
|
Packit Service |
def718 |
flags,
|
|
Packit Service |
def718 |
(jso_key ? jso_key : "(null)"),
|
|
Packit Service |
def718 |
(jso_index ? (long)*jso_index : -1L),
|
|
Packit Service |
def718 |
json_object_to_json_string(jso));
|
|
Packit Service |
def718 |
return JSON_C_VISIT_RETURN_CONTINUE;
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
static int skip_arrays(json_object *jso, int flags,
|
|
Packit Service |
def718 |
json_object *parent_jso,
|
|
Packit Service |
def718 |
const char *jso_key,
|
|
Packit Service |
def718 |
size_t *jso_index, void *userarg)
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
(void)emit_object(jso, flags, parent_jso, jso_key, jso_index, userarg);
|
|
Packit Service |
def718 |
if (json_object_get_type(jso) == json_type_array)
|
|
Packit Service |
def718 |
return JSON_C_VISIT_RETURN_SKIP;
|
|
Packit Service |
def718 |
return JSON_C_VISIT_RETURN_CONTINUE;
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
static int pop_and_stop(json_object *jso, int flags,
|
|
Packit Service |
def718 |
json_object *parent_jso,
|
|
Packit Service |
def718 |
const char *jso_key,
|
|
Packit Service |
def718 |
size_t *jso_index, void *userarg)
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
(void)emit_object(jso, flags, parent_jso, jso_key, jso_index, userarg);
|
|
Packit Service |
def718 |
if (jso_key != NULL && strcmp(jso_key, "subobj1") == 0)
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
printf("POP after handling subobj1\n");
|
|
Packit Service |
def718 |
return JSON_C_VISIT_RETURN_POP;
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
if (jso_key != NULL && strcmp(jso_key, "obj3") == 0)
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
printf("STOP after handling obj3\n");
|
|
Packit Service |
def718 |
return JSON_C_VISIT_RETURN_STOP;
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
return JSON_C_VISIT_RETURN_CONTINUE;
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
static int err_on_subobj2(json_object *jso, int flags,
|
|
Packit Service |
def718 |
json_object *parent_jso,
|
|
Packit Service |
def718 |
const char *jso_key,
|
|
Packit Service |
def718 |
size_t *jso_index, void *userarg)
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
(void)emit_object(jso, flags, parent_jso, jso_key, jso_index, userarg);
|
|
Packit Service |
def718 |
if (jso_key != NULL && strcmp(jso_key, "subobj2") == 0)
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
printf("ERROR after handling subobj1\n");
|
|
Packit Service |
def718 |
return JSON_C_VISIT_RETURN_ERROR;
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
return JSON_C_VISIT_RETURN_CONTINUE;
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
|