-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp_wrapper_example.cpp
More file actions
45 lines (35 loc) · 1.13 KB
/
Copy pathcpp_wrapper_example.cpp
File metadata and controls
45 lines (35 loc) · 1.13 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
/*
* file: cpp_wrapper_example.cpp
* description: demonstrates how to use the c++ object-oriented wrapper for cjsonx.
*
* how to compile:
* cjsonx implementation must be compiled as C code, not C++.
* 1. gcc -c -O3 -D_GNU_SOURCE -I../../include ../../src/cjsonx.c
* 2. g++ cpp_wrapper_example.cpp cjsonx.o -I../../include
*/
#include <iostream>
#include <string>
#include "cjsonx.hpp"
int main() {
std::string json_str = R"({
"status": "success",
"data": {
"id": 404,
"message": "not found"
}
})";
std::cout << "--- cjsonx c++ wrapper example ---\n";
cjsonx::Document doc = cjsonx::parse(json_str);
if (!doc.is_valid()) {
std::cerr << "parse error: " << doc.error_message() << "\n";
return 1;
}
/* fluent api style access */
cjsonx::Node root = doc.root();
std::string status(root["status"].as_string());
int id = root["data"]["id"].as_int();
std::string msg(root["data"]["message"].as_string());
std::cout << "status: " << status << "\n";
std::cout << "error " << id << ": " << msg << "\n";
return 0;
}