-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.c
More file actions
76 lines (60 loc) · 1.93 KB
/
demo.c
File metadata and controls
76 lines (60 loc) · 1.93 KB
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
/**
@brief JSON Parser Library
A lightweight, single-header C library for parsing JSON data. Designed for simplicity and portability, this parser provides a low-footprint solution to decode JSON-formatted strings into structured tokens while adhering to core JSON specifications.
@date 2025-05-03
@version 1.0
@author Eray Ozturk | erayozturk1@gmail.com
@url github.com/diffstorm
@license MIT License
*/
#include <stdio.h>
#include <string.h>
#define JSON_PARSER_IMPLEMENTATION
#include "json_parser.h"
int main()
{
const char *json = "{\"name\":\"John\\u00D0e\",\"age\":30,\"scores\":[90.5,80.0]}";
json_parser_t parser;
json_parser_init(&parser, json, strlen(json));
json_error_t error = json_parser_parse(&parser);
if(error != JSON_ERROR_NONE)
{
printf("Error: %s\n", json_error_string(error));
return 1;
}
size_t count;
const json_token_t *tokens = json_get_tokens(&parser, &count);
for(size_t i = 0; i < count; i++)
{
const json_token_t *t = &tokens[i];
printf("Token %zu: ", i);
switch(t->type)
{
case JSON_TOKEN_OBJECT:
printf("Object\n");
break;
case JSON_TOKEN_ARRAY:
printf("Array\n");
break;
case JSON_TOKEN_STRING:
printf("String: %s\n", t->value.string);
break;
case JSON_TOKEN_NUMBER:
printf("Number: %g\n", t->value.number);
break;
case JSON_TOKEN_TRUE:
printf("Boolean: true\n");
break;
case JSON_TOKEN_FALSE:
printf("Boolean: false\n");
break;
case JSON_TOKEN_NULL:
printf("Null\n");
break;
default:
printf("Unknown\n");
}
}
json_parser_free(&parser);
return 0;
}