Giter Club home page Giter Club logo

json-c-parser's Introduction

-Wl,-Bstatic -ljson-c-parser -Wl,-Bdynamic -ljson-c

#include <stdio.h>
#include <stdlib.h>
#include <json-c/json.h>
#include <json-c-parser.h>

static void do_exit(struct json_object *j, void *arg)
{
	(void) arg;
	if (j) {
		printf("got these params: %s\n", json_object_to_json_string(j));
		return;
	}

	exit(EXIT_FAILURE);
}

static json_parser_t command_parser[] = {
	{ "exit", json_type_null, do_exit, NULL },
	{ "exit", json_type_object, do_exit, NULL },
};
static json_parser_table(command_parser);

void main(void)
{
	const char json_string[] = "{ \"commands\":{\"exit\":{},\"ignored\":{}}, \"id\": 1, \"retry\":\"true\" }";
	struct json_object *j = json_tokener_parse(json_string);
	printf("parsing this: %s\n", json_object_to_json_string(j));

	bool id_exists = false; int64_t id = 0;
	bool retry = false; const char *comment = NULL;
	json_parser_t public_parser[] = {
		{ "commands", json_nest(&command_parser_table) },
		{ "id",       json_extract_int64(&id) },
		{ "id",       json_exists(json_type_int64, &id_exists) },
		// the string will live as long as the json_object
		{ "comment",  json_extract_string(&comment) },
		// retry won't be parsed because it's not a boolean
		{ "retry",    json_extract_boolean(&retry) },
		// this, however, will try using json_object_get_boolean
		{ "retry",    json_type_any, json_parser_extract_boolean, &retry },
	};
	json_parser_table(public_parser);

	json_parser_nest(j, &public_parser_table);

	printf("id %s (%ld) retry %d comment >%s<\n", id_exists ? "present" : "not present", id, (int) retry, comment);
	json_object_put(j);
}

json-c-parser's People

Contributors

yogo1212 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.