Skip to content
Snippets Groups Projects
Commit d7a33125 authored by Suru Dissanaike's avatar Suru Dissanaike
Browse files

generic json parsing

parent f5902301
Branches
No related tags found
No related merge requests found
{
"files.associations": {
"type_traits": "c",
"mman.h": "c"
"mman.h": "c",
"typeinfo": "c"
}
}
\ No newline at end of file
......@@ -22,7 +22,6 @@
#include "json_parsing.h"
long open_file(char *file_name, char **buf, bool add_nul) {
FILE *fp;
......@@ -47,7 +46,7 @@ long open_file(char *file_name, char **buf, bool add_nul) {
}
// Printing position of pointer
printf("The bytes to read: %ld \n", ftell(fp));
// printf("The bytes to read: %ld \n", ftell(fp));
/* Byte offset to the end of the file (size) */
if (0 > (off_end = ftell(fp))) {
......@@ -64,37 +63,54 @@ long open_file(char *file_name, char **buf, bool add_nul) {
/* Rewind file pointer to start of file */
rewind(fp);
/* Slurp file into buffer */
if( fsz != fread(*buf, 1, fsz, fp) ) {
free(*buf);
return -1L;
}
/* Close the file */
if( EOF == fclose(fp) ) {
free(*buf);
return -1L;
}
if( add_nul ) {
/* Make sure the buffer is NUL-terminated, just in case */
buf[fsz] = '\0';
}
/* Return the file size */
return (long)fsz;
/* Slurp file into buffer */
if (fsz != fread(*buf, 1, fsz, fp)) {
free(*buf);
return -1L;
}
/* Close the file */
if (EOF == fclose(fp)) {
free(*buf);
return -1L;
}
if (add_nul) {
/* Make sure the buffer is NUL-terminated, just in case */
buf[fsz] = '\0';
}
/* Return the file size */
return (long)fsz;
}
int parse_json_file(char *buf) {
struct json_object *jobj;
json_object *jobj, *tmp;
jobj = json_tokener_parse(buf);
/*Now printing the json object*/
printf("\n json object: %s \n", json_object_to_json_string(jobj));
int i = 0;
for (i = 0; i < json_object_array_length(jobj); i++) {
struct json_object *ubus_object;
struct json_object *ubus_call;
// Set in tmp the json_object of the secu_code array at index i
tmp = json_object_array_get_idx(jobj, i);
printf("security-code[%d] = %s\n", i, json_object_to_json_string(tmp));
json_object_object_get_ex(tmp, "object", &ubus_object);
json_object_object_get_ex(tmp, "call", &ubus_call);
printf("object: %s\n", json_object_get_string(ubus_object));
printf("call: %s\n", json_object_get_string(ubus_call));
}
return 0;
}
......
No preview for this file type
......@@ -27,8 +27,8 @@
#include <stdlib.h>
#include <unistd.h>
static char *ubus_json_file_path ="/home/sudi/Workspace/git-userspace/ubus-api-validator/ubus.json";
static char *ubus_json_file_path =
"/home/sudi/Workspace/git-userspace/ubus-api-validator/ubus.json";
static void show_usage(char *application_name) {
......@@ -65,21 +65,18 @@ static void parse_command_line_args(int argc, char *argv[]) {
int main(int argc, char *argv[]) {
char *buf;
long file_size;
long file_size;
parse_command_line_args(argc, argv);
printf("\n%s\n", ubus_json_file_path);
file_size = open_file(ubus_json_file_path, &buf, false);
/* Bail if we get a negative file size back from slurp() */
if( file_size < 0L ) {
perror("File read failed");
}
printf("\n The file contains: \n %s \n", buf);
parse_json_file(buf);
if (file_size < 0L) {
perror("File read failed");
}
//printf("\n The file contains: \n %s \n", buf);
parse_json_file(buf);
}
No preview for this file type
No preview for this file type
[
{
"ubusObject": "demo",
"ubusMethod": "unixtime"
"object": "demo",
"call": "unixtime"
},
{
"ubusObject": "demo",
"ubusMethod": "unixtime"
"object": "demo",
"call": "unixtime"
}
]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment