-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.cpp
More file actions
78 lines (61 loc) · 2.09 KB
/
demo.cpp
File metadata and controls
78 lines (61 loc) · 2.09 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
77
78
/**
@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 <iostream>
#include <string>
#include "json_parser.h"
int main()
{
// Use raw string literal to avoid escaping characters
const std::string json = R"({"name":"John\u00D0e","age":30,"scores":[90.5,80.0]})";
json_parser_t parser;
json_parser_init(&parser, json.data(), json.size());
const json_error_t error = json_parser_parse(&parser);
if(error != JSON_ERROR_NONE)
{
std::cerr << "Error: " << json_error_string(error) << "\n";
json_parser_free(&parser);
return 1;
}
size_t token_count = 0;
const json_token_t *tokens = json_get_tokens(&parser, &token_count);
for(size_t i = 0; i < token_count; ++i)
{
const auto &token = tokens[i];
std::cout << "Token " << i << ": ";
switch(token.type)
{
case JSON_TOKEN_OBJECT:
std::cout << "Object\n";
break;
case JSON_TOKEN_ARRAY:
std::cout << "Array\n";
break;
case JSON_TOKEN_STRING:
std::cout << "String: " << token.value.string << "\n";
break;
case JSON_TOKEN_NUMBER:
std::cout << "Number: " << token.value.number << "\n";
break;
case JSON_TOKEN_TRUE:
std::cout << "Boolean: true\n";
break;
case JSON_TOKEN_FALSE:
std::cout << "Boolean: false\n";
break;
case JSON_TOKEN_NULL:
std::cout << "Null\n";
break;
default:
std::cout << "Unknown\n";
}
}
json_parser_free(&parser);
return 0;
}