-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_parser_test.cpp
More file actions
117 lines (81 loc) · 2.72 KB
/
simple_parser_test.cpp
File metadata and controls
117 lines (81 loc) · 2.72 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
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
//============================================================================
// Author : Smirnov Arkady
//============================================================================
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include "SimpleJsonParser.h"
using namespace std;
json_parser_t jparser;
void parse_dump() {
cout << endl << "TokenData: " << jparser.data << endl << endl;
for (int i=0; i<JSON_MAX_TOKENS; i++ ) {
json_token_t *token = &jparser.tokens[i];
if ( !token->left ) break;
size_t size = json_token_size(&jparser, &jparser.tokens[i]);
char *value = (char*) malloc(size);
json_get_token(&jparser, &jparser.tokens[i], value, size);
cout << "left: " << (size_t*) token->left << setw(10);
cout << "right: " << (size_t*) token->right << setw(10);
cout << "size: " << size << setw(10);
cout << "value: " << value;
cout << endl;
free (value);
}
cout << endl;
}
void recv_data(char chr) {
json_token_t *token;
size_t size;
char *value;
if ( json_parse(&jparser, chr) > 0 ) {
cout << "Parse OK";
//
parse_dump();
//
if ( (token = json_find_tag_value_token(&jparser, "event")) ) {
size = json_token_size(&jparser, token);
value = (char*) malloc(size);
json_get_token(&jparser, token, value, size);
cout << "get_value:" << value << endl;
free(value);
}
value = json_get_tag_value(&jparser, "event");
cout << value << endl;
free(value);
value = json_get_tag_value(&jparser, "data");
cout << value << endl;
free(value);
// clean all
json_clean_tokens(&jparser);
}
}
/***********************************************************/
int main() {
/*
{"event":"pusher:connection_established","data":"{\"socket_id\":\"11796.609249\"}"}�
{"event":"pusher:error","data":"{\"code\":null,\"message\":\"Auth info required to subscribe to private-cmd\"}"}�
*/
//const char json[] = "{\"event\":\"pusher:error\",\"data\":\"{\"code\":null,\"message\":\"Auth info required to subscribe to private-cmd\"}\"}";
const char json[] = "{\"event\":\"pusher:error\",\"data\":\"{\\\"code\\\":null,\\\"message\\\":\\\"Auth info required to subscribe to private-cmd\\\"}\"}";
const char *pt = json;
json_init(&jparser);
while (*pt) {
recv_data(*pt);
pt++;
}
json_clean_tokens(&jparser);
pt = " {\"event\":\\\"pusher:error\\\",\"data\":\"{\\\"code\\\":null,\\\"message\\\":\\\"Auth info required to subscribe to private-cmd\\\"}\"}";
while (*pt) {
recv_data(*pt);
pt++;
}
json_clean_tokens(&jparser);
pt = " {\"event\":\"pusher:error\"}";
while (*pt) {
recv_data(*pt);
pt++;
}
return 0;
}