-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_print_tree.c
130 lines (107 loc) · 3.47 KB
/
json_print_tree.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* Copyright (c) 2024 G.E. Eidsness.
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See COPYING for details.
*
*/
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "cJSON.h" //Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
/**
gcc -o xyz .\json_print_tree.c cJSON.c -I.
.\xyz .\yourfile.json // no default file
*/
#define MAX_FILE_SIZE (10 * 10 * 1024) // 100KB
#define MIN_FILE_SIZE 2
bool is_json_file(FILE *file) {
char first_char;
if (!file) return false;
first_char = fgetc(file);
ungetc(first_char, file);
// Check if file starts with { or [ which indicates JSON
return (first_char == '{' || first_char == '[');
}
void print_json_array_tree(cJSON *item, int depth) {
char prefix[256] = {0};
for (int i = 0; i < depth; i++) {
strcat(prefix, " ");
}
if (item->string) {
printf("%s|__ %s", prefix, item->string); // graphics component
}
switch (item->type) {
case cJSON_Array:
printf(" [Array]:\n");
for (cJSON *child = item->child; child; child = child->next) {
printf("%s ", prefix);
if (cJSON_IsNumber(child)) {
printf("%g\n", child->valuedouble);
} else {
print_json_array_tree(child, depth + 1);
}
}
break;
case cJSON_Object:
printf(" {Object}:\n");
for (cJSON *child = item->child; child; child = child->next) {
print_json_array_tree(child, depth + 1);
}
break;
case cJSON_String:
printf(": \"%s\"\n", item->valuestring);
break;
case cJSON_Number:
printf(": %g\n", item->valuedouble);
break;
case cJSON_NULL:
printf(": null\n");
break;
default:
printf("\n");
}
}
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: %s <json_file>\n", argv[0]);
return 1;
}
FILE *file = fopen(argv[1], "r");
if (!file || !is_json_file(file)) {
printf("Unable to open file: %s\n", argv[1]);
return 1;
}
fseek(file, 0, SEEK_END);
long file_size = ftell(file);
fseek(file, 0, SEEK_SET);
if (file_size > MAX_FILE_SIZE || file_size < MIN_FILE_SIZE) {
printf("Invalid file size: %ld bytes\n", file_size);
fclose(file);
return 1;
}
char *json_string = malloc(file_size + 1);
if (!json_string) {
printf("Memory allocation failed\n");
fclose(file);
return 1;
}
size_t bytes_read = fread(json_string, 1, file_size, file);
json_string[bytes_read] = '\0';
fclose(file);
cJSON *json = cJSON_Parse(json_string);
if (!json) {
const char *error_ptr = cJSON_GetErrorPtr();
printf("JSON parsing error near: %.20s\n", error_ptr ? error_ptr : "unknown");
free(json_string);
return 1;
}
printf("\nJSON Array Structure for: %s\n", argv[1]);
printf("---------------------------------------\n");
print_json_array_tree(json, 0);
printf("\n");
cJSON_Delete(json);
free(json_string);
return 0;
}